diff --git a/pkgs/build-support/rust/build-rust-crate/default.nix b/pkgs/build-support/rust/build-rust-crate/default.nix index 7a5b9f8c988e..574af45c0575 100644 --- a/pkgs/build-support/rust/build-rust-crate/default.nix +++ b/pkgs/build-support/rust/build-rust-crate/default.nix @@ -222,6 +222,13 @@ lib.makeOverridable # Rust build dependencies, i.e. other libraries that were built # with buildRustCrate and are used by a build script. buildDependencies, + # Rust dev-dependencies, i.e. other libraries that were built + # with buildRustCrate and are linked only when `buildTests = true`. + # Mirrors Cargo's `[dev-dependencies]`: ignored for the regular + # lib/bin build, appended to `dependencies` for the test build. + # + # Default: [] + devDependencies, # Specify the "extern" name of a library if it differs from the library target. # See above for an extended explanation. # @@ -353,6 +360,7 @@ lib.makeOverridable crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: { }) crateOverrides crate_); dependencies_ = dependencies; buildDependencies_ = buildDependencies; + devDependencies_ = devDependencies; processedAttrs = [ "src" "propagatedBuildInputs" @@ -364,6 +372,7 @@ lib.makeOverridable "libPath" "buildDependencies" "dependencies" + "devDependencies" "features" "crateRenames" "crateName" @@ -466,7 +475,10 @@ lib.makeOverridable ++ (crate.buildInputs or [ ]) ++ buildInputs_ ++ completePropagatedBuildInputs; - dependencies = map lib.getLib dependencies_; + # Dev-dependencies are only linked when building tests, mirroring + # Cargo. When buildTests is false this is a no-op, so the metadata + # hash and store path of normal lib/bin builds are unchanged. + dependencies = map lib.getLib (dependencies_ ++ lib.optionals buildTests_ devDependencies_); buildDependencies = map lib.getLib buildDependencies_; completeDeps = lib.unique (dependencies ++ lib.concatMap (dep: dep.completeDeps) dependencies); @@ -667,6 +679,7 @@ lib.makeOverridable postInstall = crate_.postInstall or ""; dependencies = crate_.dependencies or [ ]; buildDependencies = crate_.buildDependencies or [ ]; + devDependencies = crate_.devDependencies or [ ]; crateRenames = crate_.crateRenames or { }; buildTests = crate_.buildTests or false; } diff --git a/pkgs/build-support/rust/build-rust-crate/test/default.nix b/pkgs/build-support/rust/build-rust-crate/test/default.nix index b8144250c7e9..c7fe6f461d01 100644 --- a/pkgs/build-support/rust/build-rust-crate/test/default.nix +++ b/pkgs/build-support/rust/build-rust-crate/test/default.nix @@ -404,6 +404,27 @@ rec { "test something ... ok" ]; }; + rustLibTestsWithDevDependency = + let + devDep = mkHostCrate { + crateName = "dev-dep"; + src = mkLib "src/lib.rs"; + }; + in + { + src = mkFile "src/lib.rs" '' + #[cfg(test)] + mod tests { + #[test] + fn uses_dev_dep() { + assert_eq!(dev_dep::test(), 23); + } + } + ''; + devDependencies = [ devDep ]; + buildTests = true; + expectedTestOutputs = [ "test tests::uses_dev_dep ... ok" ]; + }; rustBinTestsCombined = { src = symlinkJoin { name = "rust-bin-tests-combined";