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

@ -10,10 +10,7 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
lib = {
collectDenoCaches = import ./nix/deno/collect_caches.nix;
readDenoScroll = import ./nix/deno/read_scroll.nix;
};
lib = import ./nix/deno;
shelf = import ./shelf {
inherit pkgs system lib;
};

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;
}

View file

@ -0,0 +1,14 @@
{ self }:
{ pkgs, system, scrollsDir, subDirs }:
let
emptyDenoCache = pkgs.runCommand "empty-deno-cache" {} ''
mkdir -p $out
'';
in
builtins.foldl' (accCache: subDir:
self.buildDenoCache {
inherit pkgs system scrollsDir subDir;
baseCache = accCache;
}
) emptyDenoCache subDirs

View file

@ -1,42 +0,0 @@
{ pkgs, denoCacheDrvs }:
let
mergeOneCache = cache: ''
if [ -d "${cache}" ]; then
echo " Merging cache from ${cache}"
# Use cp with fallback, handle overlapping files gracefully
cp -rL "${cache}"/* "$out/" 2>/dev/null || true
else
echo " Warning: ${cache} is not a directory, skipping"
fi
'';
mergeCacheCmd = pkgs.runCommand "deno-shelf-shared-cache"
{
buildInputs = denoCacheDrvs;
} ''
mkdir -p $out
echo "Merging ${builtins.toString (builtins.length denoCacheDrvs)} Deno caches..."
${pkgs.lib.concatMapStringsSep "\n" mergeOneCache denoCacheDrvs}
echo "Deno caches merged. Final size: $(du -sh $out | cut -f1)"
'';
in
pkgs.writeShellScript "deno-shelf-setup" ''
export DENO_DIR=$PWD/.deno_cache
if [ ! -d "$DENO_DIR" ] || [ "${mergeCacheCmd}" -nt "$DENO_DIR/.cache_timestamp" ]; then
echo "Setting up mutable Deno cache..."
rm -rf "$DENO_DIR"
cp -r "${mergeCacheCmd}" "$DENO_DIR"
chmod -R u+w "$DENO_DIR"
touch "$DENO_DIR/.cache_timestamp"
echo "Deno cache initialized"
else
echo "Deno cache already up to date"
fi
''

9
nix/deno/default.nix Normal file
View file

@ -0,0 +1,9 @@
let
lib = {
buildDenoCache = import ./buildDenoCache.nix { self = lib; };
readDenoScroll = import ./readDenoScroll.nix { self = lib; };
buildSharedDenoCache = import ./buildSharedDenoCache.nix { self = lib; };
makeDenoShelfSetup = import ./makeDenoShelfSetup.nix { self = lib; };
};
in
lib

View file

@ -0,0 +1,19 @@
{ self }:
{ pkgs, sharedCache }:
pkgs.writeShellScript "deno-shelf-setup" ''
export DENO_DIR=$PWD/.deno_cache
if [ ! -d "$DENO_DIR" ] || [ "${sharedCache}" -nt "$DENO_DIR/.cache_timestamp" ]; then
echo "Setting up shared Deno cache..."
rm -rf "$DENO_DIR"
cp -r "${sharedCache}" "$DENO_DIR"
chmod -R u+w "$DENO_DIR"
touch "$DENO_DIR/.cache_timestamp"
echo "Shared Deno cache initialized"
else
echo "Deno cache is up to date"
fi
''

View file

@ -0,0 +1,11 @@
{ self }:
{ pkgs, system, scrollsDir, subDir }:
let
scroll = import "${scrollsDir}/${subDir}/scroll.nix";
in
{
env = self.buildDenoCache {
inherit pkgs system scrollsDir subDir;
};
meta = builtins.removeAttrs scroll [ "build" ];
}

View file

@ -1,64 +0,0 @@
{ pkgs, system, scrollsDir, subDir }:
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
{
env = pkgs.stdenv.mkDerivation {
name = "${scroll.name}-scroll-env";
src = scrollsDir;
nativeBuildInputs = [ pkgs.deno pkgs.jq ];
dontPatchShebangs = true;
buildPhase = ''
export DENO_DIR=$out
cd ${subDir}
${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;
};
meta = builtins.removeAttrs scroll [ "build" ];
}

View file

@ -7,19 +7,28 @@ let
scrollsDir = denoScrollsDir;
};
scrolls = builtins.map mkScroll [
scrollDirs = [
"hjq"
"uses-hjq"
];
scrolls = builtins.map mkScroll scrollDirs;
shelfSetup =
lib.collectDenoCaches {
inherit pkgs;
denoCacheDrvs = builtins.map (s: s.env) scrolls;
};
# shelfSetup =
# lib.collectDenoCaches {
# inherit pkgs;
# denoCacheDrvs = builtins.map (s: s.env) scrolls;
# };
sharedCache = lib.buildSharedDenoCache {
inherit pkgs system;
scrollsDir = denoScrollsDir;
subDirs = scrollDirs;
};
in
{
setupScript = shelfSetup;
setupScript = lib.makeDenoShelfSetup {
inherit pkgs sharedCache;
};
scrolls = builtins.map (s: s.meta) scrolls;
}