Cargo build fails inside nix shell

when i run cargo build outside of nix shell, directly on my debian system, it builds perfectly fine.

in my nix shell i installed the latest cargo pkg, and when i do cargo build, i get tons of errors like this:

  Compiling rskafka v0.5.0
error[E0786]: found invalid metadata files for crate `tracing`
 --> /home/pdeva/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rskafka-0.5.0/src/throttle.rs:5:5
  |
5 | use tracing::warn;
  |     ^^^^^^^
  |
  = note: invalid metadata version found: /home/pdeva/code/monorepo/target/debug/deps/libtracing-6684a9250d3ea111.rmeta

Iā€™m encountering the same thing :frowning: Did you find a solution?

i also had problems with this when first starting out with nix development environments. some points from my experience:

  • run cargo clean always after switching toolchain version/distribution so the target dir is emptied which may contain build artifacts from a different rust version which are incompatible with your current toolchain. this is also necessary when using rustup, not nix-specific.

  • make sure that all parts of your toolchain are from one version. builds will break if e.g. you have cargo 1.72 from the nix shell but rustc 1.74 from the global environment. same goes for clippy, rustfmt, rust-analyzer (wrapped to use a specific rust-src version in nixpkgs), etc.

1 Like

Right, my problem was clippy with a different version than cargo. Thanks!