nix-community / crate2nix

rebuild only changed crates in CI with crate2nix and nix
https://nix-community.github.io/crate2nix/
Apache License 2.0
385 stars 89 forks source link

Some crates.io hashes cannot be found in Cargo.lock #370

Open YPares opened 3 weeks ago

YPares commented 3 weeks ago

I'm running into this:

Running phase: unpackPhase
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking source archive /nix/store/qc74kg08frlgdjb5fjmwgvb0snpf45lx-unarchiver
source root is unarchiver
Running phase: buildPhase
@nix { "action": "setPhase", "phase": "buildPhase" }
++ crate2nix generate -f ./Cargo.toml -o Cargo-generated.nix -h /nix/store/2mwh4sn153gar2r2ay6x43l7752pr3v3-rustnix-crate2nix/crate-hashes.json
Did not find all crates.io hashes in Cargo.lock. Hashes for e.g. addr2line 0.24.2, adler 1.0.2, adler2 2.0.0, aho-corasick 1.1.3, android-tzdata 0.1.1, android_system_properties 0.1.5, async-channel 2.3.1, async-stream 0.3.6, async-stream-impl 0.3.6, async-trait 0.1.83 are missing.
This is probably a bug.
Prefetching    1/379: https://static.crates.io/crates/encoding_rs/encoding_rs-0.8.35.crate
Error: while prefetching crates for calculating sha256: While spawning 'nix-prefetch-url https://static.crates.io/crates/encoding_rs/encoding_rs-0.8.35.crate --name encoding_rs-0.8.35': No such file or directory (os error 2)
crate2nix failed.

It's difficult to make a minimal reproduction but just in case is it a known error? I tried running cargo update but that doesn't change anything.

danieldk commented 5 days ago

You are bumping into this issue because, most likely, you have git dependency in Cargo.toml. crate2nix will try to determine the crate's hash by downloading the crate using nix-prefetch-git. However, nix-prefetch-git is not added as a dependency to the generatedCargoNix derivation. However, adding it is not going to help you much, since crate2nix runs in a sandbox, so it cannot fetch the crate over the network anyway.

The solution is to run crate2nix in your Rust source folder once. This will create a crate-hashes.json file that contains the fixed-output derivation hashes for the Rust crates that are fetched using git. Add this file to your Nix repo/flake and crate2nix will use it (and not attempt to call nix-prefetch-git).

YPares commented 5 days ago

@danieldk Thanks! I'll try that. I suppose then that I should re-run crate2nix manually everytime a git-provided crate has to change? I'm not sure it's part of crate2nix documentation, could it be added? I think notable here: https://nix-community.github.io/crate2nix/00_guides/31_auto_generating/