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.
Building examples
Section titled “Building examples”cargoNix.rootCrate.build.override { buildTargets = [ "lib" "bins" "examples" ];}nix build --impure --expr '(import ./Cargo.nix { }).rootCrate.build.override { buildTargets = [ "examples" ]; }'./result/examples/simple_clientEach 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" ];}Building and running benchmarks
Section titled “Building and running benchmarks”"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" ]; };./result/benches/parse --benchBenchmarks 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''Semantics
Section titled “Semantics”- Target discovery follows cargo: crate2nix records the examples and benchmarks
that
cargo metadatareports (auto-discovered files as well as explicit[[example]]and[[bench]]sections, includingpath,crate-typeandrequired-features) inCargo.nix, like it does for binaries today.autoexamples = falseandautobenches = falseare honoured. - An unknown kind is an evaluation error. A kind without targets is not:
[ "examples" ]on a crate without examples builds an emptyexamples/. - 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 withfeatureUnification = "workspace"they share the dependency builds of all members’ tests. - The library that examples and benchmarks link against is built without
--testand 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 inexamples/as libraries. - Only the examples and benchmarks of the selected crate are built, not those of
its dependencies, whose
buildTargetsstay[ "lib" ]. - With profiles,
"benches"uses thebenchprofile, 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.nixgets the same argument.
Open questions / alternatives
Section titled “Open questions / alternatives”- Implementation:
buildRustCratein nixpkgs doesn’t support examples or benchmarks. crate2nix will ship its own, upstreamable variant ofbuildRustCrate(opt-in viatools.nixand the flake at first) that adds abuildTargetsargument, similar tobuildKindsin NixOS/nixpkgs#221440.Cargo.nixkeeps 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, becausecargo build --tests(test executables of the library, binaries and integration tests) differs from"tests"inrunTests(integration tests only).buildTestscovers it.- Should
runTests = truealso build (but not run) the examples, likecargo testdoes? That would catch broken examples in existing checks but makes them slower. - Convenience attributes (
buildExamples,buildBenches) next tobuildandbuildTests?