rustPlatform.buildRustPackage: specify environment variables via the env attribute

This commit is contained in:
Yueh-Shun Li
2025-11-04 05:25:30 +08:00
parent 51a53f3eda
commit 56a5fc5c78
@@ -15,6 +15,16 @@
windows,
}:
let
interpolateString =
s:
if lib.isList s then
lib.concatMapStringsSep " " (s: "${s}") (lib.filter (s: s != null) s)
else if s == null then
""
else
"${s}";
in
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
@@ -23,6 +33,7 @@ lib.extendMkDerivation {
"cargoUpdateHook"
"cargoLock"
"useFetchCargoVendor"
"RUSTFLAGS"
];
extendDrvArgs =
@@ -77,11 +88,19 @@ lib.extendMkDerivation {
assert lib.warnIf (args ? useFetchCargoVendor)
"buildRustPackage: `useFetchCargoVendor` is nonoptional and enabled by default as of 25.05, remove it"
true;
{
env = {
PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
RUST_LOG = logLevel;
RUSTFLAGS =
lib.optionalString (
stdenv.hostPlatform.isDarwin && buildType == "debug"
) "-C split-debuginfo=packed "
# Workaround the existing RUSTFLAGS specified as a list.
+ interpolateString (args.RUSTFLAGS or "");
}
// args.env or { };
lib.optionalAttrs (stdenv.hostPlatform.isDarwin && buildType == "debug") {
RUSTFLAGS = "-C split-debuginfo=packed " + (args.RUSTFLAGS or "");
}
// {
cargoDeps =
if cargoVendorDir != null then
null
@@ -143,12 +162,8 @@ lib.extendMkDerivation {
patches = cargoPatches ++ patches;
PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
postUnpack = ''
eval "$cargoDepsHook"
export RUST_LOG=${logLevel}
''
+ (args.postUnpack or "");