Skip to content

rustPlatform.fetchCargoVendor: set a custom User-Agent - #512735

Merged
emilazy merged 3 commits into
NixOS:masterfrom
andresilva:rust-fetch-cargo-vendor-user-agent
Apr 26, 2026
Merged

emilazy merged 3 commits into
NixOS:masterfrom
andresilva:rust-fetch-cargo-vendor-user-agent

Conversation

@andresilva

@andresilva andresilva commented Apr 23, 2026 •

Copy link
Copy Markdown
Member

Rust package builds using fetchCargoVendor have started failing intermittently with HTTP 403 responses when downloading crate tarballs from https://crates.io/api/v1/crates/<name>/<version>/download. The failures are not caused by any recent change in nixpkgs, they're triggered server-side by crates.io, which has begun blocklisting specific python-requests/<version> User-Agent strings. Testing directly against crates.io confirms this: python-requests/2.32.5 and python-requests/2.33.1 both return 403, while older versions, empty UAs, and any custom string return 302 as expected.

fetch-cargo-vendor-util.py builds its requests.Session without overriding the default User-Agent, so every tarball request goes out as python-requests/<version> and gets rejected. This also lines up with crates.io's long-standing published policy asking clients to identify themselves with a contact URL (https://crates.io/data-access), which the default UA doesn't satisfy.

The fix is a one-line change in create_http_session() to set User-Agent: nixpkgs fetchCargoVendor/2 (https://github.com/NixOS/nixpkgs) on the session. This identifies the client per crates.io's guidance and sidesteps the version-pattern blocklist. The PR also switches the download URL to https://static.crates.io/crates/<name>/<version>/download to bypass the 1 req/s rate limit on the API servers, as recommended by the crates.io maintainer in rust-lang/crates.io#13482.

Landing this naively would change fetch-cargo-vendor-util.py, which is an input to both the FOD vendorStaging stage (where HTTP downloads happen and the UA matters) and the input-addressed -vendor runCommand stage (where it doesn't). Even though vendorStaging is an FOD and its output path is stable, touching the util referenced by the -vendor stage rewrites every downstream buildRustPackage derivation in the tree, a mass rebuild that has to go through staging. To avoid that, fetch-cargo-vendor-util.py is left untouched and the fix is applied in a parallel copy fetch-cargo-vendor-util-v2.py that's wired only into the FOD. Since the FOD output is content-addressed by declared hash, the rest of the world sees no change. The duplication is deliberate and temporary, it can be collapsed back into a single file on the next staging cycle.

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.

github-actions[bot]

This comment was marked as outdated.

@andresilva

Copy link
Copy Markdown
Member Author

This is a mass rebuild but right now I can't build any rust package, so not sure whether this should still go through staging. Maybe this is also targeted by geo IP and other people aren't getting 403?

@nixpkgs-ci
nixpkgs-ci Bot requested a review from TomaSajt April 23, 2026 10:26
@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. labels Apr 23, 2026
@lunagl

lunagl commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Can confirm this is an issue for me too (thank you so much for submitting this PR, I just spent a while desperately trying to figure out what's going on lol)

For some reason it doesn't block all requests, maybe it's still being rolled out or something somewhere is doing caching?

Oh just so people can find here when searching for the error message:

 Exception: Failed to fetch file from https://crates.io/api/v1/crates/<name>/<version>/download. Status code: 403

@B4dM4n

B4dM4n commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

I also experienced this today. Changing the user-agent fixed the issue for me.

While testing, I added the response.text to the raised exception, which revealed the following message (manually formatted):

{
  "errors": [
    {
      "detail": "We are unable to process your request at this time. This usually means that you are
        in violation of our API data access policy (https://crates.io/data-access). Please email
        [email protected] and provide the request id b874fda9-5523-3db7-3c1a-f84d3db0a664"
    }
  ]
}

The https://crates.io/data-access#api page states:

  1. A maximum of 1 request per second, and
  2. A user-agent header that identifies your application. We strongly suggest providing a way for us to contact you (whether through a repository, or an e-mail address, or whatever is appropriate) so that we can reach out to work with you should there be issues.

Setting the user-agent would conform to 2., which sounds like the correct solution. It seems 1. is not actually enforced while downloading crates or the existing retry logic handles rate limits.

@NickCao

NickCao commented Apr 23, 2026

Copy link
Copy Markdown
Member

Why would this create a mass rebuild? These are all FODs?

@andresilva

andresilva commented Apr 23, 2026 •

Copy link
Copy Markdown
Member Author

@NickCao The tarball downloads themselves are FOD-cached, but fetch-cargo-vendor.nix has a second stage right after:

runCommand "${name}-vendor" {
  inherit vendorStaging;
  nativeBuildInputs = [ fetchCargoVendorUtil cargo replaceWorkspaceValues ];
} ''
  fetch-cargo-vendor-util create-vendor "$vendorStaging" "$out"
''

That -vendor derivation is the one that's actually consumed as cargoDeps by buildRustPackage and has no outputHash, so it's input-addressed. Its store path depends on the util's store path, which this PR changes. At least that's my understanding.

@NickCao

NickCao commented Apr 23, 2026

Copy link
Copy Markdown
Member

but fetch-cargo-vendor.nix has a second stage right after:

That's unfortunate, I hope crates.io can grant us an exception for now.

@TomaSajt

Copy link
Copy Markdown
Contributor

The staging part is a FOD, but there is a non-staging part, which is not a FOD.
And since both use the same file for their logic, it leads to a rebuild...
I have proposed some solutions to this rebuild issue, but have not pushed for them too strong.

(e.g. separating files or ditching the non-staging part completely and moving it into the setup hook)

@emilazy

emilazy commented Apr 23, 2026

Copy link
Copy Markdown
Member

For now we can copy the Python file, fix the user agent in only one of them, and use that one in the FOD. That should avoid rebuilds and can be cleaned up on staging.

@niklaskorz niklaskorz mentioned this pull request Apr 23, 2026
3 tasks done
)
session = requests.Session()
# crates.io blocks the default python-requests/<version> User-Agent
session.headers["User-Agent"] = "nixpkgs fetchCargoVendor (https://github.com/NixOS/nixpkgs)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nixpkgs issues can easily go unnoticed if they don't ping anyone. I'm wondering if we should include a suggestion to ping here or perhaps an email address (Nixpkgs core would be fine for this although it would likely be pure indirection since I expect we'd just end up opening an issue ourselves).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also I think it would be a good idea to include some kind of version information here. That could be a manual integer specific to fetchCargoVendor for now as including the Nixpkgs version info would require the split Toma mentioned (so that release manager tasks don't go to staging).

@emilazy

emilazy commented Apr 23, 2026

Copy link
Copy Markdown
Member

From the upstream issue:

I've temporarily disabled the block for the python-requests UAs again.

I would also recommend that you download the files directly from the CDN instead of going through our API servers: e.g. https://static.crates.io/crates/libc/0.2.185/download or https://static.crates.io/crates/libc/libc-0.2.185.crate. That addresses the rate limit concern mentioned in NixOS/nixpkgs#512735 (comment) since the 1 req/sec limit only applies to our API servers.

for completeness, the technically correct path for downloading crates is: load https://index.crates.io/config.json, look at the dl key in the JSON file, build the download URL via {dl}/{name}/{version}/download.

Since it sounds like the rate limit isn't an immediate problem for us, I suggest we do the file-copying hack now on unstable and 25.11 to get a UA set so the crates.io admins can reinstate the block, and if it would be feasible when cleaning up on staging, adjust the fetcher to follow the config.json protocol and possibly split up FOD-relevant parts of the fetcher cleanly. That should avoid hitting their API server at all and I wouldn't be surprised if it means faster downloads.

(importCargoLock will presumably need similar adjustment if it doesn't do this already? But is rarely used within Nixpkgs therefore less urgent.)

@andresilva

Copy link
Copy Markdown
Member Author

@emilazy Will push in a bit, thanks for the suggestions 🙏

@andresilva
andresilva force-pushed the rust-fetch-cargo-vendor-user-agent branch from ff2edda to 5a4b429 Compare April 23, 2026 14:33
@@ -0,0 +1,416 @@
import functools

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

diff pkgs/build-support/rust/fetch-cargo-vendor-util.py pkgs/build-support/rust/fetch-cargo-vendor-util-v2.py
42a43
>     session.headers["User-Agent"] = "nixpkgs fetchCargoVendor/2 (https://github.com/NixOS/nixpkgs)"
71c72,74
<     return f"https://crates.io/api/v1/crates/{pkg["name"]}/{pkg["version"]}/download"
---
>     # Use static.crates.io (CDN) instead of crates.io/api to avoid the 1 req/sec
>     # rate limit on the API servers.
>     return f"https://static.crates.io/crates/{pkg["name"]}/{pkg["version"]}/download"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we shouldn’t hardcode this:

for completeness, the technically correct path for downloading crates is: load https://index.crates.io/config.json, look at the dl key in the JSON file, build the download URL via {dl}/{name}/{version}/download.

Let’s perhaps stick to the minimal change for now to reduce risk on unstable and 25.11 and unblock crates.io amins ASAP, and follow up with further improvements on staging? (Also, please split the copy into a separate commit to the other changes so that the diff is readable directly.)

@andresilva andresilva Apr 24, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My thinking was that the current approach is already wrong and it already hardcodes an URL, so we might as well hardcode the URL that doesn't have rate limiting.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I separated the changes into separate commits as requested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough.

status_forcelist=[500, 502, 503, 504]
)
session = requests.Session()
session.headers["User-Agent"] = "nixpkgs fetchCargoVendor/2 (https://github.com/NixOS/nixpkgs)"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe we can create an issue on nixpkgs for reports and just link to it here? WDYT @emilazy?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We also don't want to make the UA a giant string, so maybe just an email might be the way to go?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The UA is technically valid but parses oddly against RFC 9110 section 10.1.5.

The current format is parsed as two separate products: nixpkgs with no version, and fetchCargoVendor/2, rather than one. Replacing the space with a dash would combine them into a single product token, something like this: nixpkgs-fetchCargoVendor/2 (...).

We should revisit this when we do the cleanup on staging with proper separation between the FOD stage and the input-addressed stage.

This is just a suggestion though, and not meant to block if this needs to land fast (since rust builds are currently broken).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated with your suggestion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Replacing the repository URL with an issue URL for reports that the relevant parties can subscribe to and reporters can link their new issues in seems like a good idea to me.

@andresilva

Copy link
Copy Markdown
Member Author

Feel free to push directly to this branch to fix any remaining issues (I may be offline for the next hours).

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. and removed 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. labels Apr 23, 2026
GirardR1006 pushed a commit to GirardR1006/nixpkgs that referenced this pull request May 29, 2026
The crates.io API server's 1 req/sec rate limit currently surfaces as
intermittent HTTP 403 errors when vendoring lockfiles. Switch to the CDN
endpoint as recommended by upstream (rust-lang/crates.io#13482), mirroring
the fix already applied to fetchCargoVendor in NixOS#512735.

fetchurl is content-addressed by sha256, so the URL change does not affect
any downstream store paths.

Fixes NixOS#524979
achandrasekar pushed a commit to kubernetes-sigs/inference-perf that referenced this pull request May 29, 2026
Fixed #514

FYI: NixOS/nixpkgs#512735

Track a stable channel rather than an unstable one: fewer "HEAD is
broken" regressions.
25.11 has the fetchCargoVendor User-Agent fix (needed so crates.io
doesn't 403 libtokenizers' crate downloads)

---------

Signed-off-by: ChengHao Yang <[email protected]>
ChristopherJMiller added a commit to ChristopherJMiller/hearth that referenced this pull request May 31, 2026
- Bump nixpkgs (2026-04-14 → 2026-05-23) for the fetchCargoVendor
  User-Agent fix (NixOS/nixpkgs#512735) so kanidm-cli and other Cargo
  builds stop hitting crates.io 403s.
- Bump home-manager (2026-03-20 → 2026-05-30) past the neovim
  `extraConfig` null-list regression that was failing admin/developer
  per-user closure builds.
- Pin Kanidm 1.9 → 1.10.1 (1.9 EOL 2026-05-31): flake.nix overlay,
  nix/kanidm-cli.nix (version + hashes), docker-compose image tag,
  CLAUDE.md key conventions.
- Switch kanidm-unixd `home_attr` from `spn` to `name` and align
  `lib.buildUserEnv` so `/home/testuser` is the on-disk path. Keeps
  `@` out of paths where Nextcloud Desktop and similar clients fail;
  SPN login still works via `home_alias = spn`.
- Wire `hearth-office-oxt` into the home-manager LibreOffice module
  when `enableExtensions` is true, and pass `libreofficeExtensions =
  true` in `dev/fleet-vm.nix` so the Rust UNO bundle ships with the
  dev VM closure.
- Add sqlx PgPool keepalive (`test_before_acquire`, `idle_timeout`,
  `max_lifetime`) on both hearth-api and hearth-build-worker — the
  worker was silently losing its DB connection on idle and stalling
  the job queue.
- Pre-fetch Stalwart's `webadmin.zip` in `just setup`, bind-mount it
  into the container, and point `[webadmin] resource` at the local
  file. The container's GitHub download silently fails.
- Qualify all attic CLI calls with the `dev:` server prefix so pushes
  don't leak to a developer's personal default-server.
- Pin pnpm's `verifyDepsBeforeRun: false` in pnpm-workspace.yaml so
  `pnpm build` under `nix develop -c` doesn't abort on a missing TTY.
- Add `renovate.json` configuring weekly dependency-update PRs across
  nix flake inputs, Cargo, pnpm, Helm, and docker-compose tags.
- Document the recurring follow-ups and recent fixes in a new
  Operational Backlog section of ROADMAP.md.
alexandru-savinov added a commit to alexandru-savinov/nixos-config that referenced this pull request Jun 3, 2026
…s.io 403s python-requests UA) (#461)

The "Build sancta-claw" CI job has failed on every push since the
agent-browser-cli 0.14.0 vendor FOD fell out of the binary cache:
crates.io returns HTTP 403 to rustPlatform.fetchCargoVendor because its
Python `requests`-based fetcher sends a `python-requests/*` User-Agent,
which crates.io's crawler policy now blocks (rust-lang/crates.io#13482).

Switch the single agent-browser-cli buildRustPackage call from cargoHash
to cargoLock (importCargoLock), which fetches each crate via Nix's
libcurl fetchurl — not blocked by crates.io. The CLI lockfile
(cli/Cargo.lock, v4) has only crates.io-registry deps and zero git
sources, so no cargoLock.outputHashes are needed and there is one fewer
hash to maintain than cargoHash.

Upstream fix is NixOS/nixpkgs#512735 (set a descriptive User-Agent),
backported to release-25.11 on 2026-04-26 but not yet in this flake's
nixpkgs pin (2026-03-18); revert to cargoHash once the pin carries it.

Verified locally on aarch64-darwin: sancta-claw evaluates to a valid
drvPath, and the importCargoLock vendor dir builds end-to-end (all 43
crates fetched via libcurl, zero 403). The x86_64-linux compile/link is
exercised by the previously-failing "Build sancta-claw" CI job.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
danisharora099 added a commit to danisharora099/nixpkgs that referenced this pull request Jul 29, 2026
…g fetch

crates.io now rejects python-requests' default User-Agent with HTTP 403,
breaking every fetchCargoVendor fixed-output fetch at this pin.

Functionally this is the upstream fix
(8209ba2, NixOS#512735) adapted
to the pre-v2-split script at this rev, with one refinement: the UA-fixed
util is used ONLY by the fixed-output vendorStaging derivation (the only
step that talks to the network). Because fixed-output derivations are
absorbed by hashDerivationModulo, no input-addressed derivation changes:
every package keeps its exact store path, cache.nixos.org substitution is
unaffected, and existing cargoHash values remain valid. The naive
whole-script patch would have cascaded through the input-addressed
*-vendor step into python3Packages.cryptography and from there into Qt
rebuilds.

Applied on top of e9f00bd (the rev
logos-co pins via logos-nix) so the package set is byte-identical.

Context: logos-co/logos-module-builder#159

Co-Authored-By: Claude Fable 5 <[email protected]>
danisharora099 added a commit to logos-co/lez-faucet that referenced this pull request Aug 3, 2026
No Nix build of this module has ever got past vendoring. Every leg of
every run, on every platform, dies on its first crate:

    Failed to fetch file from
    https://crates.io/api/v1/crates/adler2/2.0.1/download. Status code: 403

darwin-arm64 fails the same way, so this is not something the Linux port
introduced -- it is what blocks today's macOS releases too, and it is the
only thing between PR #6 and six green legs.

crates.io answers 403 to the User-Agent nixpkgs' cargo vendor fetcher
sends. The fetcher sets none, so requests sends `python-requests/*`;
measured against the failing URL, that is a 403, `curl/*` is a 403, and a
descriptive one is a 200. Upstream fixed it in NixOS/nixpkgs#512735
(2026-04-26) and later moved the download off the API endpoint entirely.
The nixpkgs this flake follows through logos-module-builder is e9f00bd8
(2025-09), which predates both, and repinning it is not available: it is
this module's whole Qt/C++ toolchain.

So the fix is local. fetch-cargo-vendor.nix is re-expressed in the flake,
its two helper scripts rebuilt from the pinned tree with `readFile`, and
three substitutions applied to the fetching one.

The third is the one that actually resolves it: fetch tarballs from
`https://static.crates.io/crates/<name>/<name>-<version>.crate` instead
of `https://crates.io/api/v1/crates/<name>/<version>/download`. That is
the CDN cargo itself downloads from -- the registry's `dl` key -- and
where upstream moved after #512735. It applies neither the User-Agent
policy that produces the 403 nor the per-IP rate limit that produces the
429 six concurrent legs would hit next; measured, it returns 200 for
every user-agent tried, `python-requests/2.32.3` and `curl/*` included,
on the exact URLs that 403.

The other two are belt. A descriptive User-Agent, so the request is not
one crates.io has a policy against wherever it lands. And 429 added to
the urllib3 `Retry` upstream already mounts but scopes to 5xx only -- so
the one status that means "back off and come back" becomes the one it
retries -- with jitter so parallel legs do not retry in lockstep,
`respect_retry_after_header` pinned so a `Retry-After` beats our own
curve, and a bound of 12 attempts / 60s so a genuinely down registry
fails in minutes rather than riding a 90-minute timeout. 403 is
deliberately not retried: that is a policy answer, and retrying it would
only reach the same error slower. Retry alone was measured insufficient
in the sibling swap module -- legs rode the full budget and still came
out with `RetryError: ... too many 429 error responses` -- which is why
the CDN switch is the fix and this is only the belt.

Scoping matters as much as the substitutions. This patches a private
copy via `replaceStrings`, not the shared `fetch-cargo-vendor-util` via
`applyPatches`. Patching the real one moves the `outPath` of every Rust
package in nixpkgs -- `qt6.qtdeclarative`,
`python3Packages.cryptography` and `cargo-auditable` all shift -- which
would cost this build its binary-cache hits on exactly the cold CI
runners it is meant to help, and could mean hours of Qt rebuilds. The
scoped form leaves all three at their stock paths and moves nothing but
lez-faucet-ffi; nixpkgs' own `rustPlatform` is untouched. The scripts are
re-expressed rather than generated with `toFile` because a generated file
embeds `${nixpkgs}` store paths as text, and registering those as
references makes evaluation die with `path '...-source' is not valid` on
CI's Determinate Nix while evaluating clean on stock Nix.

The substitutions are asserted, so a nixpkgs bump that reshapes the
fetcher fails evaluation loudly with the string it could not find,
instead of silently reverting to a 403. All of it deletes once
logos-module-builder's nixpkgs carries the upstream fix
(logos-co/logos-module-builder#173) -- but nothing here waits on that.

cargoHash is unchanged, and now proven rather than asserted. It becomes
`cargoDeps.hash` so vendoring goes through the patched fetcher, but it
covers only the fixed-output vendor-staging tree -- checksum-verified
crate tarballs, git checkouts and Cargo.lock -- so it tracks the
lockfile, not the nixpkgs building it or the host serving the bytes. The
script checksums every tarball against Cargo.lock, so a changed download
host cannot quietly vendor something else; it would fail loudly.

Verified on an aarch64 macOS host:

- `lez-faucet-ffi-0.2.0-vendor-staging` builds cold and lands on the
  committed sha256-bynsEIeov27VyVr8NdHVcpPwEQr8Kbmd93o+B61XWEM= -- a
  real download of the full crate set through static.crates.io, matching
  a hash measured against the API endpoint. Same bytes, different host.
- `nix build .#packages.aarch64-darwin.lez-faucet-ffi` succeeds, the
  first time any Nix build of this module has. 9.3M dylib, install name
  `@rpath/liblez_faucet_ffi.dylib`, exporting exactly the ten locked ABI
  entry points and the header beside it.
- `nix eval .#packages.<sys>.lgx-portable.drvPath` instantiates the full
  derivation graph for aarch64-darwin, x86_64-linux and aarch64-linux,
  forcing every input including the vendor FOD.
- qt6.qtdeclarative, python3Packages.cryptography and cargo-auditable
  evaluate to their stock outPaths, confirming the scoping.

Co-Authored-By: Claude <[email protected]>
danisharora099 added a commit to logos-co/lez-faucet that referenced this pull request Aug 3, 2026
No Nix build of this module has ever got past vendoring. Every leg of
every run, on every platform, dies on its first crate:

    Failed to fetch file from
    https://crates.io/api/v1/crates/adler2/2.0.1/download. Status code: 403

darwin-arm64 fails the same way, so this is not something the Linux port
introduced -- it is what blocks today's macOS releases too, and it is the
only thing between PR #6 and six green legs.

crates.io answers 403 to the User-Agent nixpkgs' cargo vendor fetcher
sends. The fetcher sets none, so requests sends `python-requests/*`;
measured against the failing URL, that is a 403, `curl/*` is a 403, and a
descriptive one is a 200. Upstream fixed it in NixOS/nixpkgs#512735
(2026-04-26) and later moved the download off the API endpoint entirely.
The nixpkgs this flake follows through logos-module-builder is e9f00bd8
(2025-09), which predates both, and repinning it is not available: it is
this module's whole Qt/C++ toolchain.

So the fix is local. fetch-cargo-vendor.nix is re-expressed in the flake,
its two helper scripts rebuilt from the pinned tree with `readFile`, and
three substitutions applied to the fetching one.

The third is the one that actually resolves it: fetch tarballs from
`https://static.crates.io/crates/<name>/<name>-<version>.crate` instead
of `https://crates.io/api/v1/crates/<name>/<version>/download`. That is
the CDN cargo itself downloads from -- the registry's `dl` key -- and
where upstream moved after #512735. It applies neither the User-Agent
policy that produces the 403 nor the per-IP rate limit that produces the
429 six concurrent legs would hit next; measured, it returns 200 for
every user-agent tried, `python-requests/2.32.3` and `curl/*` included,
on the exact URLs that 403.

The other two are belt. A descriptive User-Agent, so the request is not
one crates.io has a policy against wherever it lands. And 429 added to
the urllib3 `Retry` upstream already mounts but scopes to 5xx only -- so
the one status that means "back off and come back" becomes the one it
retries -- with jitter so parallel legs do not retry in lockstep,
`respect_retry_after_header` pinned so a `Retry-After` beats our own
curve, and a bound of 12 attempts / 60s so a genuinely down registry
fails in minutes rather than riding a 90-minute timeout. 403 is
deliberately not retried: that is a policy answer, and retrying it would
only reach the same error slower. Retry alone was measured insufficient
in the sibling swap module -- legs rode the full budget and still came
out with `RetryError: ... too many 429 error responses` -- which is why
the CDN switch is the fix and this is only the belt.

Scoping matters as much as the substitutions. This patches a private
copy via `replaceStrings`, not the shared `fetch-cargo-vendor-util` via
`applyPatches`. Patching the real one moves the `outPath` of every Rust
package in nixpkgs -- `qt6.qtdeclarative`,
`python3Packages.cryptography` and `cargo-auditable` all shift -- which
would cost this build its binary-cache hits on exactly the cold CI
runners it is meant to help, and could mean hours of Qt rebuilds. The
scoped form leaves all three at their stock paths and moves nothing but
lez-faucet-ffi; nixpkgs' own `rustPlatform` is untouched. The scripts are
re-expressed rather than generated with `toFile` because a generated file
embeds `${nixpkgs}` store paths as text, and registering those as
references makes evaluation die with `path '...-source' is not valid` on
CI's Determinate Nix while evaluating clean on stock Nix.

The substitutions are asserted, so a nixpkgs bump that reshapes the
fetcher fails evaluation loudly with the string it could not find,
instead of silently reverting to a 403. All of it deletes once
logos-module-builder's nixpkgs carries the upstream fix
(logos-co/logos-module-builder#173) -- but nothing here waits on that.

cargoHash is unchanged, and now proven rather than asserted. It becomes
`cargoDeps.hash` so vendoring goes through the patched fetcher, but it
covers only the fixed-output vendor-staging tree -- checksum-verified
crate tarballs, git checkouts and Cargo.lock -- so it tracks the
lockfile, not the nixpkgs building it or the host serving the bytes. The
script checksums every tarball against Cargo.lock, so a changed download
host cannot quietly vendor something else; it would fail loudly.

Verified on an aarch64 macOS host:

- `lez-faucet-ffi-0.2.0-vendor-staging` builds cold and lands on the
  committed sha256-bynsEIeov27VyVr8NdHVcpPwEQr8Kbmd93o+B61XWEM= -- a
  real download of the full crate set through static.crates.io, matching
  a hash measured against the API endpoint. Same bytes, different host.
- `nix build .#packages.aarch64-darwin.lez-faucet-ffi` succeeds, the
  first time any Nix build of this module has. 9.3M dylib, install name
  `@rpath/liblez_faucet_ffi.dylib`, exporting exactly the ten locked ABI
  entry points and the header beside it.
- `nix eval .#packages.<sys>.lgx-portable.drvPath` instantiates the full
  derivation graph for aarch64-darwin, x86_64-linux and aarch64-linux,
  forcing every input including the vendor FOD.
- qt6.qtdeclarative, python3Packages.cryptography and cargo-auditable
  evaluate to their stock outPaths, confirming the scoping.

Co-Authored-By: Claude <[email protected]>
mkenigs added a commit to flox/flox-build-examples that referenced this pull request Aug 27, 2026
The rust example used a fairly old catalog page, on which the nixpkgs
rust builder doesnt provide a user-agent for cargo.io, which causes
strict rate limiting, leading to 403 errors in CI. Bumping the env and
the associated nixpkgs should avoid this going forward.

See Also: NixOS/nixpkgs#512735
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/403-error-when-fetching-packages-from-crates-io/79803/1

stephenjudkins pushed a commit to MercuryTechnologies/nixpkgs that referenced this pull request Sep 1, 2026
The crates.io API server's 1 req/sec rate limit currently surfaces as
intermittent HTTP 403 errors when vendoring lockfiles. Switch to the CDN
endpoint as recommended by upstream (rust-lang/crates.io#13482), mirroring
the fix already applied to fetchCargoVendor in NixOS#512735.

fetchurl is content-addressed by sha256, so the URL change does not affect
any downstream store paths.

Fixes NixOS#524979

# Conflicts:
#	pkgs/build-support/rust/import-cargo-lock.nix
dlipicar pushed a commit to logos-co/logos-nix that referenced this pull request Sep 3, 2026
…t bump)

crates.io returns 403 to python-requests' default User-Agent. The pinned
nixpkgs (e9f00bd8) predates NixOS/nixpkgs#512735, so its
fetch-cargo-vendor-util.py sets none and every *-vendor-staging FOD fails
(logos-co/logos-module-builder#159).

Add an overlay that wraps makeRustPlatform and, inside the scope, re-points
fetchCargoVendor's vendor-staging FOD at a copy of the pin's helper with the
two network-facing hunks of #512735 applied (User-Agent header,
static.crates.io download URL). Only the FOD changes; consumer cargoHash
values and every downstream outPath are unchanged. The overlay is the
identity on a pin that already sends a User-Agent and fails evaluation if
the helper drifted in any other way.

Applied to the native package sets (forAllSystems, forAllTargets,
legacyPackages) and exported as lib.overlays.fetchCargoVendorUserAgent.
Not applied to the Windows set: nixpkgs-windows (b5aa0fbd) already contains
the upstream fix. The nixpkgs pin and Qt are untouched; the Qt bump stays
in #5.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5ByEqD8ff6pK1fJwL4EFV
mlavrinenko added a commit to mlavrinenko/flibrarian that referenced this pull request Sep 5, 2026
crates.io started 403ing the python-requests default User-Agent, which
is what the old fetch-cargo-vendor-util sent, so buildRustPackage could
not vendor anything not already in the store. Fixed upstream in
NixOS/nixpkgs#512735: the fetcher now sends an identifying UA and pulls
from static.crates.io. Our lock predated it.

Carries duckdb 1.4.4 -> 1.5.5 (fts hash already pinned) and a newer
clippy, whose question_mark lint flags the operator chain in
parse_file_size_filter; rewritten as a table so >= still wins over >.
brainrake added a commit to supabase/postgres that referenced this pull request Sep 14, 2026
…ing paths

crates.io started 403'ing python-requests' default UA and rate-limiting
its API endpoint (NixOS/nixpkgs#512735), breaking fetchCargoVendor,
importCargoLock, and fetchCrate alike. Redirects each to
static.crates.io / swaps in the fixed util script, scoped to
cargo-pgrx's rustPlatform construction.

Verified against Raminder's pg_graphql 1.6.2 bump (rs/bump-pg-graphql):
builds clean end-to-end with these fixes applied.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
pull Bot pushed a commit to Spencerx/postgres that referenced this pull request Sep 14, 2026
* fix(cargo-pgrx): work around crates.io UA blocklist across all vendoring paths

crates.io started 403'ing python-requests' default UA and rate-limiting
its API endpoint (NixOS/nixpkgs#512735), breaking fetchCargoVendor,
importCargoLock, and fetchCrate alike. Redirects each to
static.crates.io / swaps in the fixed util script, scoped to
cargo-pgrx's rustPlatform construction.

Verified against Raminder's pg_graphql 1.6.2 bump (rs/bump-pg-graphql):
builds clean end-to-end with these fixes applied.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* chore(cargo-pgrx): rename fix-fetch-cargo.nix to fix-cargo.nix

It fixes fetchCrate too, not just cargo vendoring.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 8.has: port to stable This PR already has a backport to the stable release. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants