From 56a5fc5c7880db42af2c497d4488c1218bc952ae Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Wed, 20 Aug 2025 21:47:31 +0800 Subject: [PATCH] rustPlatform.buildRustPackage: specify environment variables via the env attribute --- .../rust/build-rust-package/default.nix | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pkgs/build-support/rust/build-rust-package/default.nix b/pkgs/build-support/rust/build-rust-package/default.nix index 7efbc8a9ea36..1e500cd82c97 100644 --- a/pkgs/build-support/rust/build-rust-package/default.nix +++ b/pkgs/build-support/rust/build-rust-package/default.nix @@ -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 non‐optional 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 "");