buildRustCrate: make --cap-lints configurable (#502704)

This commit is contained in:
Bernardo Meurer
2026-03-23 17:03:15 +00:00
committed by GitHub
5 changed files with 33 additions and 3 deletions
+11
View File
@@ -842,6 +842,17 @@ general. A number of other parameters can be overridden:
(hello { }).override { extraRustcOpts = "-Z debuginfo=2"; }
```
- The lint level cap passed to `rustc` (`allow` by default, which
silences all lints). Because `rustc` only honours the first
`--cap-lints` it receives, this cannot be changed via
`extraRustcOpts`; use this attribute instead. Useful when overriding
the `rust` attribute to point at `clippy-driver`, since clippy lints
are also capped by this flag:
```nix
(hello { }).override { capLints = "warn"; }
```
- Phases, just like in any other derivation, can be specified using
the following attributes: `preUnpack`, `postUnpack`, `prePatch`,
`patches`, `postPatch`, `preConfigure` (in the case of a Rust crate,
@@ -24,6 +24,7 @@
colors,
buildTests,
codegenUnits,
capLints,
}:
let
@@ -73,6 +74,7 @@ in
LIB_EXT="${stdenv.hostPlatform.extensions.library}"
LIB_PATH="${libPath}"
LIB_NAME="${libName}"
CAP_LINTS="${capLints}"
CRATE_NAME='${lib.replaceStrings [ "-" ] [ "_" ] libName}'
@@ -29,6 +29,7 @@
crateVersion,
extraLinkFlags,
extraRustcOptsForBuildRs,
capLints,
libName,
libPath,
release,
@@ -195,7 +196,7 @@ in
fi
noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
${mkRustcFeatureArgs crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \
-L dependency=target/buildDeps ${buildDeps} --cap-lints allow $EXTRA_BUILD_FLAGS --color ${colors}
-L dependency=target/buildDeps ${buildDeps} --cap-lints ${capLints} $EXTRA_BUILD_FLAGS --color ${colors}
mkdir -p target/build/${crateName}.out
export RUST_BACKTRACE=1
@@ -196,6 +196,16 @@ lib.makeOverridable
# Example: [ "-Z debuginfo=2" ]
# Default: []
extraRustcOptsForBuildRs,
# The lint level cap passed to rustc via `--cap-lints`.
# See <https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints>.
#
# rustc honours only the first `--cap-lints` it sees, so appending a
# second one via `extraRustcOpts` has no effect. Use this parameter
# instead if you need lints to fire (e.g. when running clippy).
#
# Example: "warn"
# Default: "allow"
capLints,
# Whether to enable building tests.
# Use true to enable.
# Default: false
@@ -249,12 +259,14 @@ lib.makeOverridable
"buildTests"
"codegenUnits"
"links"
"capLints"
];
extraDerivationAttrs = removeAttrs crate processedAttrs;
nativeBuildInputs_ = nativeBuildInputs;
buildInputs_ = buildInputs;
extraRustcOpts_ = extraRustcOpts;
extraRustcOptsForBuildRs_ = extraRustcOptsForBuildRs;
capLints_ = capLints;
buildTests_ = buildTests;
# crate2nix has a hack for the old bash based build script that did split
@@ -383,6 +395,7 @@ lib.makeOverridable
lib.optionals (crate ? extraRustcOptsForBuildRs) crate.extraRustcOptsForBuildRs
++ extraRustcOptsForBuildRs_
++ (lib.optional (edition != null) "--edition ${edition}");
capLints = crate.capLints or capLints_;
configurePhase = configureCrate {
inherit
@@ -403,6 +416,7 @@ lib.makeOverridable
crateLinks
extraLinkFlags
extraRustcOptsForBuildRs
capLints
crateLicense
crateLicenseFile
crateReadme
@@ -433,6 +447,7 @@ lib.makeOverridable
extraRustcOpts
buildTests
codegenUnits
capLints
;
};
dontStrip = !release;
@@ -472,6 +487,7 @@ lib.makeOverridable
verbose = crate_.verbose or true;
extraRustcOpts = [ ];
extraRustcOptsForBuildRs = [ ];
capLints = "allow";
features = [ ];
nativeBuildInputs = [ ];
buildInputs = [ ];
@@ -15,7 +15,7 @@ build_lib() {
$lib_src \
--out-dir target/lib \
-L dependency=target/deps \
--cap-lints allow \
--cap-lints $CAP_LINTS \
$LINK \
$EXTRA_LINK_ARGS \
$EXTRA_LINK_ARGS_LIB \
@@ -52,7 +52,7 @@ build_bin() {
$EXTRA_LINK_ARGS \
$EXTRA_LINK_ARGS_BINS \
$EXTRA_LIB \
--cap-lints allow \
--cap-lints $CAP_LINTS \
$BUILD_OUT_DIR \
$EXTRA_BUILD \
$EXTRA_FEATURES \