Skip to content

prefetch-npm-deps: add cacheVersion for packument support - #470517

Merged
philiptaron merged 11 commits into
NixOS:stagingfrom
Mic92:npm-prefetch
Jan 19, 2026
Merged

philiptaron merged 11 commits into
NixOS:stagingfrom
Mic92:npm-prefetch

Conversation

@Mic92

@Mic92 Mic92 commented Dec 13, 2025 •

Copy link
Copy Markdown
Member

Add a cacheVersion parameter to fetchNpmDeps and npmDepsCacheVersion to buildNpmPackage. When set to 2, prefetch-npm-deps will also fetch and cache packuments (package metadata) in addition to tarballs.

npm can request packuments with two different Accept headers:

  • corgiDoc: abbreviated metadata (default)
  • fullDoc: full metadata (used for workspaces)

npm's cache policy requires headers to match, so we cache both versions.

This is opt-in via cacheVersion to avoid breaking existing hashes. Set npmDepsCacheVersion = 2 for projects using npm workspaces.

Also fix cacache index format to properly separate multiple entries with newlines, and update map_cache() to parse multi-line index files.

This change allows me to build qwen-code.

Fixes #474535

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

Add a 👍 reaction to pull requests you find important.

@Mic92
Mic92 marked this pull request as draft December 13, 2025 18:32
@Mic92

Mic92 commented Dec 13, 2025

Copy link
Copy Markdown
Member Author

Okay. Looks like the error came back.

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 6.topic: nodejs Node.js is a free, open-source, cross-platform JavaScript runtime environment labels Dec 13, 2025
@Mic92

Mic92 commented Dec 13, 2025

Copy link
Copy Markdown
Member Author

now it works. Tested in numtide/llm-agents.nix#1364
also briefly tested that it doesn't break the old hash.

@Mic92
Mic92 marked this pull request as ready for review December 13, 2025 18:42
@nixpkgs-ci
nixpkgs-ci Bot requested a review from winterqt December 13, 2025 18:45
@Mic92

Mic92 commented Dec 13, 2025

Copy link
Copy Markdown
Member Author

This issue that is fixed by this PR:

lock.json
       > Setting npm_config_cache to /nix/store/xyhk6dqpw5pgd4haarlgpp28gmwqi166-qwen-code-0.4.1-npm-deps
       > Installing dependencies
       > npm error code ENOTCACHED
       > npm error request to https://registry.npmjs.org/@types%2freact-dom failed: cache mode is 'only-if-cached' but no cached response is available.
       > npm error Log files were not written due to an error writing to the directory: /nix/store/xyhk6dqpw5pgd4haarlgpp28gmwqi166-qwen-code-0.4.1-npm-deps/_logs
       > npm error You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
       >
       > ERROR: npm failed to install dependencies
       >
       > Here are a few things you can try, depending on the error:
       > 1. Set `makeCacheWritable = true`
       >   Note that this won't help if npm is complaining about not being able to write to the logs directory -- look above that for the actual error.
       > 2. Set `npmFlags = [ "--legacy-peer-deps" ]`
       >

Mic92 added a commit to numtide/llm-agents.nix that referenced this pull request Dec 14, 2025
@Mic92

Mic92 commented Dec 23, 2025

Copy link
Copy Markdown
Member Author

cc @K900 @philiptaron @SuperSandro2000 @balsoft who do node stuff more regulary.

@philiptaron
philiptaron changed the base branch from master to staging December 24, 2025 23:27
@nixpkgs-ci nixpkgs-ci Bot closed this Dec 24, 2025
@nixpkgs-ci nixpkgs-ci Bot reopened this Dec 24, 2025
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 24, 2025
@nixpkgs-ci nixpkgs-ci Bot added 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation and removed 2.status: merge conflict This PR has merge conflicts with the target branch labels Dec 25, 2025
@winterqt

Copy link
Copy Markdown
Member

Working through this now, there are some issues with the version of npm included in Node 24 I'm trying to work through myself before passing it off :P

Mic92 and others added 5 commits December 31, 2025 00:37
Add a cacheVersion parameter to fetchNpmDeps and npmDepsCacheVersion to
buildNpmPackage. When set to 2, prefetch-npm-deps will also fetch and
cache packuments (package metadata) in addition to tarballs.

npm can request packuments with two different Accept headers:
- corgiDoc: abbreviated metadata (default)
- fullDoc: full metadata (used for workspaces)

npm's cache policy requires headers to match, so we cache both versions.

This is opt-in via cacheVersion to avoid breaking existing hashes.
Set npmDepsCacheVersion = 2 for projects using npm workspaces.

Also fix cacache index format to properly separate multiple entries
with newlines, and update map_cache() to parse multi-line index files.
Document the new npmDepsCacheVersion option that enables packument
caching for npm workspaces support.
Fixed-output derivations for npm dependencies fail with hash mismatches
when rebuilt on different days. The root cause is that npm registry
metadata (packuments) contain volatile fields that change whenever
upstream publishes a new version.

For example, the TypeScript packument changes daily due to nightly
releases, even though the lockfile pins a specific version. The cached
packument includes _rev, time, modified, and the full versions list -
all of which drift over time.

Strip these volatile fields and filter the versions map to only include
versions actually referenced in the lockfile.

Signed-off-by: Jörg Thalheim <[email protected]>
@Mic92

Mic92 commented Jan 4, 2026 •

Copy link
Copy Markdown
Member Author

Found & fixed another reproducibility issue. The new URLs would add new versions into the cache when those are uploaded to NPM. Now it only includes the version that are actually needed by the build. If we want to be sure, we can also wait for a couple of days to make sure there are not more issues. I have 8 packages now with this fetcher version on 3 different platforms that are up-dated every 1-2 days with ~1000 unique users.

@winterqt

winterqt commented Jan 4, 2026

Copy link
Copy Markdown
Member

Is it possible to exclude the FODs from your cache so we can make sure that the reproducibility is sane by having all of the users build it?

@Mic92

Mic92 commented Jan 7, 2026 •

Copy link
Copy Markdown
Member Author

Is it possible to exclude the FODs from your cache so we can make sure that the reproducibility is sane by having all of the users build it?

There are not included in the cache. The final package is though. Based on past experience there are plenty users that do not use the cache as for most packages the build time is not that high. The ci that updates hashes doesn't write to cache and the second one that verify the build will reproduce - this works ensures fod hashes that are generally not reproducible, it also catched Plattform-depending hashes in the past.

Now the only thing that this doesn't help with is if the api doesn't give us immutable data over time.

@Mic92

Mic92 commented Jan 7, 2026

Copy link
Copy Markdown
Member Author

Looks like there are more volatile fields in this api response. The readme field seems to return sometimes content and sometimes not. Now i am considering whitelisting fields, which i think will also make it more robust, if the npm api gets extended in the future.

@Mic92

Mic92 commented Jan 7, 2026 •

Copy link
Copy Markdown
Member Author

I am now testing this variant: numtide/llm-agents.nix#1754

This now has the only fields I could find in npm install. Does our fetcher also needs to work with other package manager that I would need to check?

Switch from stripping known volatile fields to using an explicit whitelist
of allowed fields. This is more robust against upstream changes that add
new fields which could affect hash stability.

Top-level: only name and versions (dist-tags and time not needed for
lockfile installs where versions are already resolved)

Version-level: identity, all dependency types, dist, bin, platform
constraints (engines/os/cpu), scripts, and deprecated flag.

Based on analysis of pacote, npm-pick-manifest, npm-install-checks, and
arborist - only fields actually read during npm install are included.
@Mic92

Mic92 commented Jan 11, 2026

Copy link
Copy Markdown
Member Author

Seems to be overall more robust now. No reported issues anymore.

@Mic92

Mic92 commented Jan 14, 2026

Copy link
Copy Markdown
Member Author

Still no more issues since a week.

@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Jan 14, 2026
///
/// npm's cache policy checks that the Accept header matches between the cached
/// request and the new request. npm can request packuments with two different headers:
/// 1. "corgiDoc" (abbreviated metadata) - used initially

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hilarious.

@philiptaron

Copy link
Copy Markdown
Contributor

@nixpkgs-ci nixpkgs-ci Bot added 12.approvals: 2 This PR was reviewed and approved by two persons. and removed 12.approvals: 1 This PR was reviewed and approved by one person. labels Jan 14, 2026
@philiptaron
philiptaron added this pull request to the merge queue Jan 19, 2026
Merged via the queue into NixOS:staging with commit 963873d Jan 19, 2026
@Mic92
Mic92 deleted the npm-prefetch branch January 19, 2026 15:48
@Mic92

Mic92 commented Jan 20, 2026

Copy link
Copy Markdown
Member Author

I found one more field that is mutable unfortunately. Follow-up fix here: #481866

Comment thread doc/release-notes/rl-2605.section.md Resolved
Mic92 added a commit to Mic92/nixpkgs that referenced this pull request Feb 12, 2026
Use the correct attribute names after the cacheVersion -> fetcherVersion
rename and mention fetchNpmDeps's fetcherVersion parameter as well.

Addresses review feedback from NixOS#470517.
github-merge-queue Bot pushed a commit that referenced this pull request Feb 13, 2026
Use the correct attribute names after the cacheVersion -> fetcherVersion
rename and mention fetchNpmDeps's fetcherVersion parameter as well.

Addresses review feedback from #470517.
@niklaskorz

Copy link
Copy Markdown
Member

Do you think this would be acceptable to backport to 25.11? We have several security updates in a package that do not build without the new fetcher version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nodejs Node.js is a free, open-source, cross-platform JavaScript runtime environment 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 12.approvals: 2 This PR was reviewed and approved by two persons.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants