handle registry files in shared cache derivation

This commit is contained in:
EatThePooh 2025-10-08 14:29:38 +07:00
parent 26f60eb96b
commit 9f290b4a88
2 changed files with 41 additions and 22 deletions

View file

@ -65,8 +65,9 @@ in
match = builtins.match "(@?[^@]+)@([^_]+).*" key; match = builtins.match "(@?[^@]+)@([^_]+).*" key;
name = builtins.elemAt match 0; name = builtins.elemAt match 0;
version = builtins.elemAt match 1; version = builtins.elemAt match 1;
in cachePath = "npm/${registryHostname}/${name}";
pkgs.stdenv.mkDerivation { in {
drv = pkgs.stdenv.mkDerivation {
pname = "deno-npm-${builtins.replaceStrings ["@" "/"] ["" "-"] name}"; pname = "deno-npm-${builtins.replaceStrings ["@" "/"] ["" "-"] name}";
version = version; version = version;
src = mkTarballDrv { src = mkTarballDrv {
@ -77,28 +78,16 @@ in
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
export OUT_PATH="$out/npm/${registryHostname}/${name}/${version}" export OUT_PATH="$out/${cachePath}/${version}"
mkdir -p $OUT_PATH mkdir -p $OUT_PATH
tar -xzf $src -C $OUT_PATH --strip-components=1 tar -xzf $src -C $OUT_PATH --strip-components=1
SUBSET_FILTER='to_entries | map(select(.key == "version" or .key == "bin" or .key == "dependencies" or .key == "peerDependencies")) | from_entries'
PACKAGE_SUBSET=$(${pkgs.jq}/bin/jq "$SUBSET_FILTER" $OUT_PATH/package.json)
cat > $OUT_PATH/../registry.json <<EOF
{
"name": "${name}",
"dist-tags": {},
"versions": {
"${version}": $PACKAGE_SUBSET
}
}
EOF
runHook postInstall runHook postInstall
''; '';
}; };
inherit name cachePath;
};
in in
pkgs.lib.mapAttrsToList mkNpmDep npmDeps; pkgs.lib.mapAttrsToList mkNpmDep npmDeps;
@ -109,11 +98,41 @@ in
inherit pkgs lockfile registryHostname; inherit pkgs lockfile registryHostname;
}) })
lockfiles; lockfiles;
in sharedCacheBase = pkgs.symlinkJoin {
pkgs.symlinkJoin { name = "deno-shared-cache-base";
name = "deno-shared-cache"; paths = map (d: d.drv) allNpmDeps;
paths = allNpmDeps;
}; };
in
pkgs.runCommand "deno-shared-cache" {} ''
runHook preInstall
mkdir -p $out
chmod -R +w $out
cp -rs ${sharedCacheBase}/* $out
while IFS='|' read -r dep name; do
chmod +w $out/$dep
cat > $out/$dep/registry.json <<EOF
{
"name": "$name",
"dist-tags": {},
"versions": {}
}
EOF
while IFS= read -r ver; do
SUBSET_FILTER='to_entries | map(select(.key == "version" or .key == "bin" or .key == "dependencies" or .key == "peerDependencies")) | from_entries'
PACKAGE_SUBSET=$(${pkgs.jq}/bin/jq "$SUBSET_FILTER" $out/$dep/$ver/package.json)
${pkgs.jq}/bin/jq --arg version "$ver" --argjson subset "$PACKAGE_SUBSET" \
'.versions[$version] = $subset' \
$out/$dep/registry.json > $out/$dep/registry.json.tmp
mv $out/$dep/registry.json.tmp $out/$dep/registry.json
done < <(find $out/$dep -mindepth 1 -maxdepth 1 -type d | xargs -n1 basename)
done < <(echo "${builtins.concatStringsSep "\n" (map (d: "${d.cachePath}|${d.name}") allNpmDeps)}" | sort -u)
runHook postInstall
'';
denoWithCache = { pkgs, baseDeno, sharedCache }: denoWithCache = { pkgs, baseDeno, sharedCache }:
pkgs.writeShellScriptBin "deno" '' pkgs.writeShellScriptBin "deno" ''

View file

@ -1,5 +1,5 @@
{ {
description = "Builds a shared cache for Deno packages based on lock files"; description = "Provides a Deno executable wrapper pointing to pre-built NPM dependency cache based on lock files";
outputs = { ... }: { outputs = { ... }: {
flakeModule = ./flake-module.nix; flakeModule = ./flake-module.nix;