How to change `RUSTFLAGS` in `build-rust-package` -- list / string merging problem?

Probably because split-debuginfo=packed was not being included in RUSTFLAGS. In your successful attempt, we can see that it’s there at the end, thus overwriting the unpacked flag.

That said, after a few trial and errors, I was able to get it working by adding split-debuginfo=packed only when isDarwinDebug is true. This way, the default behavior for Linux is untouched and we successfully get debug symbols for both systems:

Here is the patch to get this all working:

darwin-debug-symbols.patch
---
 pkgs/build-support/rust/build-rust-package/default.nix | 4 ++++
 pkgs/build-support/rust/hooks/cargo-install-hook.sh    | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/pkgs/build-support/rust/build-rust-package/default.nix b/pkgs/build-support/rust/build-rust-package/default.nix
index a1727c91ba36..bc4ebcf589e4 100644
--- a/pkgs/build-support/rust/build-rust-package/default.nix
+++ b/pkgs/build-support/rust/build-rust-package/default.nix
@@ -82,6 +82,8 @@ let
   targetIsJSON = lib.hasSuffix ".json" target;
   useSysroot = targetIsJSON && !__internal_dontAddSysroot;
 
+  isDarwinDebug = stdenv.isDarwin && buildType == "debug";
+
   sysroot = callPackage ./sysroot { } {
     inherit target;
     shortTarget = stdenv.hostPlatform.rust.cargoShortTarget;
@@ -97,6 +99,8 @@ assert useSysroot -> !(args.doCheck or true);
 
 stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoUpdateHook" "cargoLock" ]) // lib.optionalAttrs useSysroot {
   RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
+} // lib.optionalAttrs isDarwinDebug {
+  RUSTFLAGS = "-C split-debuginfo=packed " + (args.RUSTFLAGS or "");
 } // {
   inherit buildAndTestSubdir cargoDeps;
 
diff --git a/pkgs/build-support/rust/hooks/cargo-install-hook.sh b/pkgs/build-support/rust/hooks/cargo-install-hook.sh
index 24a6e6fa9eb3..7dd2c112e3fd 100644
--- a/pkgs/build-support/rust/hooks/cargo-install-hook.sh
+++ b/pkgs/build-support/rust/hooks/cargo-install-hook.sh
@@ -39,6 +39,10 @@ cargoInstallHook() {
     rmdir --ignore-fail-on-non-empty $out/lib $out/bin
     runHook postInstall
 
+    # If present, copy any .dSYM directories for debugging on darwin
+    # https://github.com/NixOS/nixpkgs/issues/330036
+    find "${releaseDir}" -maxdepth 1 -name '*.dSYM' -exec cp -RLt $out/bin/ {} +
+
     echo "Finished cargoInstallHook"
 }
 
-- 
2.45.2

Note: Since the attrset from isDarwinDebug overwrites the previous RUSTFLAGS attrset (when both isDarwinDebug and useSysroot are true), we should probably add a condition for useSysroot:

lib.optionalAttrs isDarwinDebug {
  RUSTFLAGS = "-C split-debuginfo=packed " + lib.optionalString useSysroot "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
}
1 Like