Checks (tests, clippy, rustfmt and your own)
What checks have in common
Section titled “What checks have in common”Running the unit tests, the integration tests, the doctests, clippy or rustfmt on a crate are all the same kind of thing: a derivation that looks at one crate and either succeeds, with a log, or fails the build. They differ only in what they need from the crate:
| Check | Needs | Runs |
|---|---|---|
"lib" |
the test executables (testBinaries) |
the library’s unit tests |
"bins" |
the test executables (testBinaries) |
the binaries’ unit tests |
"tests" |
the test executables (testBinaries) |
the integration tests |
"doc" |
the compiled library and its build script output | rustdoc --test on the library |
"clippy" |
the crate’s compilation, with clippy-driver as rustc |
clippy’s lints, -Dwarnings by default |
"fmt" |
only the crate’s source | rustfmt --check on the crate’s targets |
crate2nix defines all of them with the same building block, a check, and you can define your own with it.
Selecting checks
Section titled “Selecting checks”checks takes a list, like runTests. Strings name the built-in checks:
cargoNix.rootCrate.build.override { checks = [ "lib" "bins" "tests" "clippy" "fmt" ];}The selected checks run in their own derivations, and the build depends on
them, as with runTests today: building the result runs the checks, and the
crate itself is the normal build output, so nothing is built twice.
runTests stays as it is: runTests = [ "lib" "bins" ] is the same as having
"lib" and "bins" in checks, and runTests = true adds
[ "lib" "bins" "tests" ].
Every check is also available as a derivation of its own, without overriding, so each can be a separate flake check:
let member = cargoNix.workspaceMembers.my_server;in{ checks = { my_server-tests = member.checks.tests; # "lib", "bins" and "tests" my_server-doc = member.checks.doc; my_server-clippy = member.checks.clippy; my_server-fmt = member.checks.fmt; };}member.checks.<name> takes the same overrides as build (features,
crateOverrides, …).
Your own checks
Section titled “Your own checks”A list entry that isn’t a string is a check you define. Built-in checks are always strings, your own never are, so the two can’t be confused, and crate2nix can add built-in checks later without clashing with yours.
crate2nix.lib.mkCheck creates one:
let helpSmokeTest = crate2nix.lib.mkCheck { name = "help"; # Run the binary that this crate builds. script = { build, ... }: '' ${build}/bin/my-cli --help ''; };incargoNix.rootCrate.build.override { checks = [ "lib" "bins" helpSmokeTest ];}script is called with the check context and returns a shell snippet. It
runs in a derivation with checkInputs in PATH. A failing command fails the
check, and its output ends up in the log, which is the check’s output.
The context is what the built-in checks use, too:
| Attribute | Content |
|---|---|
crate |
{ crateName, version, packageId, features, relativePath, ... }, as for localCratesFileset |
src |
the crate’s filtered source |
build |
the crate’s normal build (bin/, and the library in lib) |
testBinaries |
the test executables with tests.json |
toolchain |
rustc, cargo, rustdoc, clippy, rustfmt of buildRustCrateForPkgs, where available |
pkgs |
the package set the crate is built with |
Only what the script refers to is built: a check that only uses src doesn’t
wait for the crate’s compilation, which is how "fmt" reports formatting
errors before anything is compiled.
A check that reruns the compilation with a different compiler, as "clippy"
does with clippy-driver, sets rustcWrapper instead of script:
crate2nix.lib.mkCheck { name = "clippy-pedantic"; rustcWrapper = { toolchain, ... }: "${toolchain.clippy}/bin/clippy-driver -Wclippy::pedantic -Dwarnings";}The crate is then compiled once more with the wrapper in place of rustc; the
build script and the dependencies are taken from the normal build.
mkCheck also takes checkInputs and env. testInputs, testPreRun and
testPostRun keep working for the test checks.
Example: running the tests with cargo-nextest
Section titled “Example: running the tests with cargo-nextest”cargo-nextest isn’t built in, but it fits a check: it only needs the test
executables and their kinds from tests.json.
let nextest = crate2nix.lib.mkCheck { name = "nextest"; checkInputs = { pkgs, ... }: [ pkgs.cargo-nextest pkgs.jq ]; script = { testBinaries, ... }: '' # Write the binaries metadata that nextest expects, from # ${testBinaries}/tests.json, then run the executables in # ${testBinaries}/tests with cargo nextest run --binaries-metadata binaries.json ... ''; };incargoNix.rootCrate.build.override { checks = [ nextest "doc" ];}Since nextest’s metadata formats change with nextest, such a check is best
kept next to your project, or in a separate library of checks, rather than in
Cargo.nix.
Semantics
Section titled “Semantics”- Checks only check the selected crate, not its dependencies, which are built normally and shared with the regular build.
"clippy"compiles the crate’s library and binaries (and its test targets if a test check is selected) withclippy-driver. Build scripts are compiled withrustc, as withcargo clippy. The default arguments are[ "-Dwarnings" ]; setclippyArgsto change them. Lint levels from[lints]inCargo.tomlapply, as withcargo clippy."fmt"runsrustfmt --checkwith the crate’s edition on all Rust files of the crate’s targets andbuild.rs, usingrustfmt.tomlfrom the crate or the workspace root if it is part of the source. Generated code (OUT_DIR) isn’t checked.- clippy and rustfmt must come from the same toolchain as
rustc. With nixpkgs’ toolchain, crate2nix usespkgs.clippyandpkgs.rustfmt; for a custom toolchain, pass them tobuildRustCrate. - Checks that need a change to
buildRustCrate("doc","clippy"andrustcWrapper) need crate2nix’s own variant ofbuildRustCrate, which is opt-in viatools.nixand the flake at first. With nixpkgs’buildRustCrate, the other checks work, and these are an evaluation error that explains how to switch. - Cross-compilation: checks that run executables of the host platform are skipped if the build platform can’t run them; clippy and rustfmt always run.
- JSON mode:
lib/build-from-json.nixgets the same arguments.
Workspace-wide checks
Section titled “Workspace-wide checks”Some checks are about the whole workspace, not single crates: cargo fmt --all
once for the repository, cargo deny or cargo audit on Cargo.lock. They
don’t need crate2nix and are simplest as plain flake checks:
checks.fmt = pkgs.runCommand "cargo-fmt-check" { nativeBuildInputs = [ pkgs.cargo pkgs.rustfmt ];} '' cd ${./.} cargo fmt --all --check touch $out'';Open questions / alternatives
Section titled “Open questions / alternatives”- Name:
checksas the general list next torunTests, or one list underrunTeststhat also takes"clippy"and"fmt"(simpler, but they aren’t tests)? - Should
"clippy"replace the normal compilation, so that the checked crate is the deployed crate (PR #445), instead of compiling the crate a second time? That saves a compilation, but a lint then blocks the build output. - Is
rustcWrappergeneral enough to be public, or should custom checks be limited toscript? - Should crate2nix ship a separate library of optional checks (nextest,
cargo-machete, …) outside
Cargo.nix?