buildRustCrate: make --cap-lints configurable

lib.sh previously hardcoded `--cap-lints allow` in both build_lib and
build_bin. Since rustc only honours the first `--cap-lints` it sees,
appending a different level via `extraRustcOpts` had no effect, making
it impossible to run clippy (or any lint-based tooling) through
buildRustCrate without wrapper scripts that strip the argument.

This adds a `capLints` parameter (default `"allow"`, preserving current
behaviour) that can be set per-crate or via `.override`:

    (myCrate { }).override { capLints = "warn"; }
This commit is contained in:
Bernardo Meurer Costa 2026-03-23 16:10:51 +00:00
commit a523dd74ac
No known key found for this signature in database
5 changed files with 33 additions and 3 deletions

View file

@ -842,6 +842,17 @@ general. A number of other parameters can be overridden:
(hello { }).override { extraRustcOpts = "-Z debuginfo=2"; } (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 - Phases, just like in any other derivation, can be specified using
the following attributes: `preUnpack`, `postUnpack`, `prePatch`, the following attributes: `preUnpack`, `postUnpack`, `prePatch`,
`patches`, `postPatch`, `preConfigure` (in the case of a Rust crate, `patches`, `postPatch`, `preConfigure` (in the case of a Rust crate,

View file

@ -24,6 +24,7 @@
colors, colors,
buildTests, buildTests,
codegenUnits, codegenUnits,
capLints,
}: }:
let let
@ -73,6 +74,7 @@ in
LIB_EXT="${stdenv.hostPlatform.extensions.library}" LIB_EXT="${stdenv.hostPlatform.extensions.library}"
LIB_PATH="${libPath}" LIB_PATH="${libPath}"
LIB_NAME="${libName}" LIB_NAME="${libName}"
CAP_LINTS="${capLints}"
CRATE_NAME='${lib.replaceStrings [ "-" ] [ "_" ] libName}' CRATE_NAME='${lib.replaceStrings [ "-" ] [ "_" ] libName}'

View file

@ -29,6 +29,7 @@
crateVersion, crateVersion,
extraLinkFlags, extraLinkFlags,
extraRustcOptsForBuildRs, extraRustcOptsForBuildRs,
capLints,
libName, libName,
libPath, libPath,
release, release,
@ -195,7 +196,7 @@ in
fi fi
noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \ noisily rustc --crate-name build_script_build $BUILD --crate-type bin ${rustcOpts} \
${mkRustcFeatureArgs crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link \ ${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 mkdir -p target/build/${crateName}.out
export RUST_BACKTRACE=1 export RUST_BACKTRACE=1

View file

@ -196,6 +196,16 @@ lib.makeOverridable
# Example: [ "-Z debuginfo=2" ] # Example: [ "-Z debuginfo=2" ]
# Default: [] # Default: []
extraRustcOptsForBuildRs, 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. # Whether to enable building tests.
# Use true to enable. # Use true to enable.
# Default: false # Default: false
@ -249,12 +259,14 @@ lib.makeOverridable
"buildTests" "buildTests"
"codegenUnits" "codegenUnits"
"links" "links"
"capLints"
]; ];
extraDerivationAttrs = removeAttrs crate processedAttrs; extraDerivationAttrs = removeAttrs crate processedAttrs;
nativeBuildInputs_ = nativeBuildInputs; nativeBuildInputs_ = nativeBuildInputs;
buildInputs_ = buildInputs; buildInputs_ = buildInputs;
extraRustcOpts_ = extraRustcOpts; extraRustcOpts_ = extraRustcOpts;
extraRustcOptsForBuildRs_ = extraRustcOptsForBuildRs; extraRustcOptsForBuildRs_ = extraRustcOptsForBuildRs;
capLints_ = capLints;
buildTests_ = buildTests; buildTests_ = buildTests;
# crate2nix has a hack for the old bash based build script that did split # 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 lib.optionals (crate ? extraRustcOptsForBuildRs) crate.extraRustcOptsForBuildRs
++ extraRustcOptsForBuildRs_ ++ extraRustcOptsForBuildRs_
++ (lib.optional (edition != null) "--edition ${edition}"); ++ (lib.optional (edition != null) "--edition ${edition}");
capLints = crate.capLints or capLints_;
configurePhase = configureCrate { configurePhase = configureCrate {
inherit inherit
@ -403,6 +416,7 @@ lib.makeOverridable
crateLinks crateLinks
extraLinkFlags extraLinkFlags
extraRustcOptsForBuildRs extraRustcOptsForBuildRs
capLints
crateLicense crateLicense
crateLicenseFile crateLicenseFile
crateReadme crateReadme
@ -433,6 +447,7 @@ lib.makeOverridable
extraRustcOpts extraRustcOpts
buildTests buildTests
codegenUnits codegenUnits
capLints
; ;
}; };
dontStrip = !release; dontStrip = !release;
@ -472,6 +487,7 @@ lib.makeOverridable
verbose = crate_.verbose or true; verbose = crate_.verbose or true;
extraRustcOpts = [ ]; extraRustcOpts = [ ];
extraRustcOptsForBuildRs = [ ]; extraRustcOptsForBuildRs = [ ];
capLints = "allow";
features = [ ]; features = [ ];
nativeBuildInputs = [ ]; nativeBuildInputs = [ ];
buildInputs = [ ]; buildInputs = [ ];

View file

@ -15,7 +15,7 @@ build_lib() {
$lib_src \ $lib_src \
--out-dir target/lib \ --out-dir target/lib \
-L dependency=target/deps \ -L dependency=target/deps \
--cap-lints allow \ --cap-lints $CAP_LINTS \
$LINK \ $LINK \
$EXTRA_LINK_ARGS \ $EXTRA_LINK_ARGS \
$EXTRA_LINK_ARGS_LIB \ $EXTRA_LINK_ARGS_LIB \
@ -52,7 +52,7 @@ build_bin() {
$EXTRA_LINK_ARGS \ $EXTRA_LINK_ARGS \
$EXTRA_LINK_ARGS_BINS \ $EXTRA_LINK_ARGS_BINS \
$EXTRA_LIB \ $EXTRA_LIB \
--cap-lints allow \ --cap-lints $CAP_LINTS \
$BUILD_OUT_DIR \ $BUILD_OUT_DIR \
$EXTRA_BUILD \ $EXTRA_BUILD \
$EXTRA_FEATURES \ $EXTRA_FEATURES \