reuse intermediate deno caches

This commit is contained in:
EatThePooh 2025-08-03 14:09:30 +07:00
parent f459ec8b78
commit 60b5164a8a
9 changed files with 146 additions and 117 deletions

View file

@ -0,0 +1,76 @@
{ self }:
{ pkgs, system, scrollsDir, subDir, baseCache ? null }:
let
target =
let
arch = builtins.head (builtins.split "-" system);
os = builtins.elemAt (builtins.split "-" system) 1;
vendor = if os == "darwin" then "apple" else "unknown";
sys = if os == "darwin" then "darwin" else "linux-gnu";
in "${arch}-${vendor}-${sys}";
scroll = import "${scrollsDir}/${subDir}/scroll.nix";
hashFilesCat = builtins.concatStringsSep ""
(builtins.map builtins.readFile scroll.build.hashFiles);
sourceContentHash_ = builtins.convertHash {
hash = "sha1:${builtins.hashString "sha1" hashFilesCat}";
toHashFormat = "base64";
};
sourceContentHash = "sha1-${sourceContentHash_}";
outputHash =
scroll.build.knownHashes.${sourceContentHash}
or pkgs.lib.fakeSha256;
in
pkgs.stdenv.mkDerivation {
name = if baseCache == null
then "${scroll.name}-scroll-env"
else "${scroll.name}-incremental-cache";
src = scrollsDir;
nativeBuildInputs = [ pkgs.deno pkgs.jq ];
dontPatchShebangs = true;
buildPhase = ''
export DENO_DIR=$out
mkdir -p $DENO_DIR
# Start with base cache if provided
${if baseCache != null then ''
if [ -d "${baseCache}" ]; then
cp -r "${baseCache}"/* "$out/" 2>/dev/null || true
find "$out" -type f -exec chmod u+w {} \;
find "$out" -type d -exec chmod u+w {} \;
fi
'' else ""}
cd ${subDir}
echo "Building cache for ${scroll.name}..."
${scroll.build.cacheCommand}
cd ..
echo 'Go fuck yourself, SQLite!'
find $out -name "*-wal" -delete
find $out -name "*-shm" -delete
echo 'Go fuck yourself, JSON!'
find $out/npm -name "*.json" -type f 2>/dev/null | while read -r file; do
if ${pkgs.jq}/bin/jq empty "$file" 2>/dev/null; then
${pkgs.jq}/bin/jq -S . "$file" > "$file.tmp" && mv "$file.tmp" "$file"
fi
done
echo 'sourceContentHash: ${sourceContentHash}'
echo ""
echo "add me to knownHashes if you see a fake sha256 error ^"
echo "DON'T USE JSR DEPS UNLESS YOU MAKE THEM REPRODUCIBLE SOMEHOW"
echo ""
'';
installPhase = "true";
outputHashMode = "recursive";
outputHashAlgo = "sha256";
inherit outputHash;
}