Skip to content

Examples and benchmarks

By default, crate2nix builds the library and the binaries of a crate, like cargo build. Cargo knows more kinds of targets: examples (examples/, [[example]]) and benchmarks (benches/, [[bench]]). Like runTests selects the tests to run, buildTargets selects the targets to build, with the names of cargo’s target selection flags:

Kind Like cargo’s Output
"lib" cargo build --lib the library, in lib
"bins" cargo build --bins bin/
"examples" cargo build --examples examples/
"benches" cargo bench --no-run (--benches) benches/

The default is [ "lib" "bins" ], i.e. cargo build, so build doesn’t change. Test executables are built by buildTests, as with cargo test --no-run.

cargoNix.rootCrate.build.override {
buildTargets = [ "lib" "bins" "examples" ];
}
Terminal window
nix build --impure --expr '(import ./Cargo.nix { }).rootCrate.build.override { buildTargets = [ "examples" ]; }'
./result/examples/simple_client

Each list is its own derivation, so you choose whether a broken example may block the binaries: list them together to build everything in one go, like cargo build --all-targets, or use separate overrides to keep them apart. In a flake:

let
my_lib = cargoNix.workspaceMembers.my_lib.build;
in
{
packages.default = my_lib; # [ "lib" "bins" ]
packages.examples = my_lib.override { buildTargets = [ "examples" ]; };
# Make sure the examples keep compiling, without blocking the package.
checks.examples = my_lib.override { buildTargets = [ "examples" ]; };
}

Examples with required-features are only built if those features are enabled, as with cargo:

cargoNix.rootCrate.build.override {
buildTargets = [ "examples" ];
features = [ "default" "tls" ];
}

"benches" builds the benchmark executables. Running benchmarks inside a Nix build rarely gives meaningful numbers, so crate2nix doesn’t run them for you. Run the executables yourself:

benches = cargoNix.rootCrate.build.override { buildTargets = [ "benches" ]; };
Terminal window
./result/benches/parse --bench

Benchmarks with harness = false, such as criterion benchmarks, are built as plain executables, those with the default harness with rustc --test like cargo does. Both end up in benches/.

To check in CI that the benchmarks still work, run them in test mode, which executes every benchmark once without measuring:

pkgs.runCommand "benches-smoke-test" { } ''
for b in ${benches}/benches/*; do
"$b" --test
done
touch $out
''
  • Target discovery follows cargo: crate2nix records the examples and benchmarks that cargo metadata reports (auto-discovered files as well as explicit [[example]] and [[bench]] sections, including path, crate-type and required-features) in Cargo.nix, like it does for binaries today. autoexamples = false and autobenches = false are honoured.
  • An unknown kind is an evaluation error. A kind without targets is not: [ "examples" ] on a crate without examples builds an empty examples/.
  • Like tests, examples and benchmarks can use the crate’s dev-dependencies, and they link against the crate’s own library. They are built with the same feature set as the test build, so with featureUnification = "workspace" they share the dependency builds of all members’ tests.
  • The library that examples and benchmarks link against is built without --test and without the dev-dependencies, as with cargo, and shared with the normal build: [ "examples" ] doesn’t rebuild the library.
  • Examples with crate-type = ["cdylib"] or ["staticlib"] end up in examples/ as libraries.
  • Only the examples and benchmarks of the selected crate are built, not those of its dependencies, whose buildTargets stay [ "lib" ].
  • With profiles, "benches" uses the bench profile, the other kinds the selected profile, as cargo does.
  • Cross-compilation: the executables are built for the host platform.
  • JSON mode: lib/build-from-json.nix gets the same argument.
  • Implementation: buildRustCrate in nixpkgs doesn’t support examples or benchmarks. crate2nix will ship its own, upstreamable variant of buildRustCrate (opt-in via tools.nix and the flake at first) that adds a buildTargets argument, similar to buildKinds in NixOS/nixpkgs#221440. Cargo.nix keeps working with nixpkgs’ buildRustCrate; requesting "examples" or "benches" with it is an evaluation error that explains how to use crate2nix’s.
  • "tests" is not a build kind, because cargo build --tests (test executables of the library, binaries and integration tests) differs from "tests" in runTests (integration tests only). buildTests covers it.
  • Should runTests = true also build (but not run) the examples, like cargo test does? That would catch broken examples in existing checks but makes them slower.
  • Convenience attributes (buildExamples, buildBenches) next to build and buildTests?