nix-community / crate2nix

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

How to run clippy in a check #195

Open jeffa5 opened 3 years ago

jeffa5 commented 3 years ago

Making a derivation that runs cargo clippy fails due to trying to download the dependencies.

Looking at the clippy page it seems it can be run as rustc using clippy-driver. Is there a way with the crate2nix logic to follow this through or does something need to be added to buildRustCrate in nixpkgs?

If there is another nice way to run clippy in a derivation (looking at it for a flake check) then that would also be great.

apoelstra commented 1 year ago

This is a bit of a hack, but one way to do it is:

  1. Rather than calling ${generated}/default.nix, copy your source tree somewhere writeable and ${generated}/cargo/config into .cargo/config.toml in that directory.
  2. If there isn't a Cargo.lock file in your source directory, copy that into place too (you'd need to be running #276 for this to be the case, I think)
  3. Add ${rustc}/bin to your $PATH. I also had to add ${gcc}/bin for one of my crates which had cc as a dev-dependency.
  4. Run cargo clippy --locked in the source directory.

The first and last steps are the real magic. The rest are just fixing things up. I think, in the absense of worktrees, you may also be able to do this in a Cargo.nix testPostRun block without needing to do any source code copying or other shenanigans.

apoelstra commented 1 year ago

Oh, I found an even more straightforward way!

  1. cd into ${generated}/crate
  2. Run CARGO_TARGET_DIR=<somewhere writeable> CARGO_HOME=../cargo/ cargo clippy --locked.

This avoids copying and ensures you're using the same lockfile that crate2nix used to produce the cargo config. You may still need to patch up your $PATH.