crate2nix icon indicating copy to clipboard operation
crate2nix copied to clipboard

How to run clippy in a check

Open jeffa5 opened this issue 3 years ago • 2 comments

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.

jeffa5 avatar May 16 '21 15:05 jeffa5

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 avatar Mar 05 '23 16:03 apoelstra

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.

apoelstra avatar Mar 05 '23 17:03 apoelstra