Skip to content

Checks (tests, clippy, rustfmt and your own)

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.

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, …).

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
'';
};
in
cargoNix.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 ...
'';
};
in
cargoNix.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.

  • 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) with clippy-driver. Build scripts are compiled with rustc, as with cargo clippy. The default arguments are [ "-Dwarnings" ]; set clippyArgs to change them. Lint levels from [lints] in Cargo.toml apply, as with cargo clippy.
  • "fmt" runs rustfmt --check with the crate’s edition on all Rust files of the crate’s targets and build.rs, using rustfmt.toml from 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 uses pkgs.clippy and pkgs.rustfmt; for a custom toolchain, pass them to buildRustCrate.
  • Checks that need a change to buildRustCrate ("doc", "clippy" and rustcWrapper) need crate2nix’s own variant of buildRustCrate, which is opt-in via tools.nix and 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.nix gets the same arguments.

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
'';
  • Name: checks as the general list next to runTests, or one list under runTests that 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 rustcWrapper general enough to be public, or should custom checks be limited to script?
  • Should crate2nix ship a separate library of optional checks (nextest, cargo-machete, …) outside Cargo.nix?