Skill v1.0.0
currentLLM-judged scan95/100name: npm-deps-hash-replication description: Use when computing npmDepsHash without a Nix build. version: 1.0.0 author: Hermes Agent license: MIT platforms: [linux] metadata: hermes: tags: [nix, npm, packaging, buildNpmPackage, fetchNpmDeps, prefetch-npm-deps] related_skills: [declarative-hermes-config, nixos-flake-verify-in-sandbox]
npm-deps hash replication (no Nix build required)
When to Use
- You need
npmDepsHashfor abuildNpmPackagederivation but cannot run a
real Nix build (no daemon, sandbox store, or no substitute coverage for the pinned nixpkgs rev).
- Packaging a new npm-distributed CLI into a Nix config from a sandbox.
- Bumping a vendored npm package whose lockfile changed.
Replicate what prefetch-npm-deps would produce and hash the tree yourself.
Method
- Produce a clean lockfile: lockfile v2/v3 whose production graph is
registry tarballs only (for a published npm package, vendor the shipped lockfile with devDependencies stripped; a file: or git ref in the prod graph means the approach must change).
- Fetch every tarball to a content-addressed disk cache:
scripts/fetch-lock-tarballs.py package-lock.json cache-dir (resumable; verifies every SRI digest; safe to re-run).
- Assemble the prefetch tree:
scripts/assemble-cacache.py package-lock.json cache-dir outdir
- Hash it:
nix hash path outdir→ that is yournpmDepsHash
(the FOD uses outputHashMode = recursive).
- Always validate against a golden control before pinning. Find any
realized *-npm-deps FOD already in a store, run this pipeline on the same lockfile, and require a byte-for-byte identical tree (diff -r <golden> <replica> must be empty; nix hash path both sides). Only a golden reproduction makes the computed hash trustworthy.
Output format (prefetch-npm-deps 0.1.0, fetcherVersion=1)
- Tree root:
package-lock.json(bytes identical to input) +_cacache/. - Tarball bodies:
_cacache/content-v2/<algo>/<h[0:2]>/<h[2:4]>/<h[4:]>
where h = hex of the lockfile SRI digest.
- Index:
_cacache/index-v5/<sha256(key)[0:2]>/<[2:4]>/<[4:]>; each line is
sha1(json)\tjson with NO trailing newline (prefix "\n" only when appending to a non-empty file); json is the compact serialization of {key,integrity,time:0,size,metadata:{url,options:{compress:true}}}; key = "make-fetch-happen:request-cache:" + url.
- Selection: non-empty
packageskeys only; drop entries whoseresolved
is missing/not URL-shaped or starts with file:; refuse git/ssh; per duplicate URL keep the strongest integrity (sha512 > sha1).
- The derivation's env carries
NPM_FETCHER_VERSION=1— check the generated
FOD drv contains it together with your hash.
Pitfalls
- Dev deps must be stripped from the vendored lockfile: dev nodes often
point at file:/workspace refs that prefetch refuses, and npm ci --ignore-scripts in buildNpmPackage only needs the prod graph.
- A one-shot downloader that buffers bodies in RAM dies on large lockfiles;
use the fetch→cache→assemble two-phase scripts (resumable by design).
- Keep
os/cpufields on platform-specific packages — they are part of
the graph and the fetch set.
- A near-miss in index-line formatting (newline handling, json key order,
separator spacing) shifts the whole tree hash; the golden comparison is what catches it, not eyeballing.
- URL normalization: registry URLs are already canonical; only lowercase
scheme/host and drop default ports if a non-registry URL ever appears.