initial commit

This commit is contained in:
EatThePooh 2025-08-05 20:59:34 +07:00
commit 1a55a11500
8 changed files with 270 additions and 0 deletions

34
lib/install-cache.nix Normal file
View file

@ -0,0 +1,34 @@
{ pkgs, cache }:
pkgs.writeShellScriptBin "install-deno-cache" ''
set -euo pipefail
if [ -z "''${DENO_DIR:-}" ]; then
echo "Error: DENO_DIR environment variable must be set" >&2
exit 1
fi
echo "Installing Deno cache to: $DENO_DIR"
# Remove existing cache if it exists
if [ -d "$DENO_DIR" ]; then
echo "Removing existing cache..."
rm -rf "$DENO_DIR"
fi
# Copy the shared cache, dereferencing symlinks
if [ -d "${cache}" ]; then
echo "Copying shared cache..."
mkdir -p "$(dirname "$DENO_DIR")"
cp -rL "${cache}" "$DENO_DIR"
chmod -R u+w "$DENO_DIR"
echo "Cache installed successfully!"
else
echo "No shared cache found at ${cache}"
mkdir -p "$DENO_DIR"
fi
if [ -d "$DENO_DIR/npm" ]; then
echo "NPM packages in cache: $(find "$DENO_DIR/npm" -name "package.json" | wc -l)"
fi
''

View file

@ -0,0 +1,56 @@
{ self }:
{ pkgs, lockfile }:
let
registry = self.denoNpmRegistryHostname;
# Extract npm dependencies from a single lockfile
extractNpmDeps = lockfile:
let
lockJSON = builtins.fromJSON (builtins.readFile lockfile);
in
lockJSON.npm or {};
npmDeps = extractNpmDeps lockfile;
# Create tarball derivation for a package
mkTarballDrv = { name, version, integrity }:
let
tarballName =
if builtins.substring 0 1 name == "@"
then builtins.baseNameOf name # "@types/node" → "node"
else name;
in
pkgs.fetchurl {
url = "https://${registry}/${name}/-/${tarballName}-${version}.tgz";
hash = integrity;
};
# Create derivation for a single npm package
mkNpmDep = key: value:
let
match = builtins.match "(.+)@(.+)" key;
name = builtins.elemAt match 0;
version = builtins.elemAt match 1;
in
pkgs.stdenv.mkDerivation {
pname = "deno-npm-${builtins.replaceStrings ["@" "/"] ["" "-"] name}";
version = version;
src = mkTarballDrv {
inherit name version;
inherit (value) integrity;
};
installPhase = ''
runHook preInstall
export OUT_PATH="$out/npm/${registry}/${name}/${version}"
mkdir -p $OUT_PATH
tar -xzf $src -C $OUT_PATH --strip-components=1
runHook postInstall
'';
};
in
pkgs.lib.mapAttrsToList mkNpmDep npmDeps

13
lib/shared-cache.nix Normal file
View file

@ -0,0 +1,13 @@
{ self }:
{ pkgs, lockfiles }:
let
allNpmDeps = builtins.concatMap
(lockfile:
self.denoLockfileToNpmDeps { inherit pkgs lockfile; })
lockfiles;
in
pkgs.symlinkJoin {
name = "deno-shared-cache";
paths = allNpmDeps;
}