Skip to content

buildRustPackage: only set RUSTFLAGS if non-empty#464992

Closed
HeitorAugustoLN wants to merge 1 commit intoNixOS:stagingfrom
HeitorAugustoLN:cargo-target-fix
Closed

buildRustPackage: only set RUSTFLAGS if non-empty#464992
HeitorAugustoLN wants to merge 1 commit intoNixOS:stagingfrom
HeitorAugustoLN:cargo-target-fix

Conversation

@HeitorAugustoLN
Copy link
Member

Closes #464392

Previously, the refactor of buildRustPackage caused RUSTFLAGS to be defined in the environment unconditionally, often resulting in an empty string (RUSTFLAGS="").

When RUSTFLAGS is set (even to an empty string), Cargo prioritizes it and ignores CARGO_TARGET_<TRIPLE>_RUSTFLAGS. This behavior broke packages like cosmic-comp that rely on target-specific flags for linking libraries (e.g., libEGL).

This change calculates the flags beforehand and uses lib.optionalAttrs to ensure RUSTFLAGS is only added to the environment if it contains a value. This restores the ability for Cargo to fall back to target-specific configuration variables.

tested with:

diff --git a/pkgs/by-name/ru/rustcat/package.nix b/pkgs/by-name/ru/rustcat/package.nix
index 65a241e99bc2..285452eaf751 100644
--- a/pkgs/by-name/ru/rustcat/package.nix
+++ b/pkgs/by-name/ru/rustcat/package.nix
@@ -3,6 +3,7 @@
   fetchFromGitHub,
   rustPlatform,
   versionCheckHook,
+  libglvnd,
 }:

 rustPlatform.buildRustPackage rec {
@@ -24,6 +25,18 @@ rustPlatform.buildRustPackage rec {

   versionCheckProgram = [ "${placeholder "out"}/bin/rcat" ];

+  buildInputs = [ libglvnd ];
+
+  env = {
+    CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS = toString (
+      map (arg: "-C link-arg=" + arg) [
+        "-Wl,--push-state,--no-as-needed"
+        "-lEGL"
+        "-Wl,--pop-state"
+      ]
+    );
+  };
+

And its getting linked:

❯ , readelf -d ./result/bin/rcat
Dynamic section at offset 0x1735d0 contains 32 entries:
  Tag                Type           Name/Value
  0x0000000000000001 (NEEDED)       Shared library: [libc.so.6]
  0x0000000000000001 (NEEDED)       Shared library: [ld-linux-x86-64.so.2]
  0x0000000000000001 (NEEDED)       Shared library: [libgcc_s.so.1]
  0x0000000000000001 (NEEDED)       Shared library: [libEGL.so.1]
  0x000000000000001d (RUNPATH)      Library runpath: [/nix/store/mm4v6w3wj8wi5qc6rwdrb2rkqs08whp9-libglvnd-1.7.0/lib:/nix/store/xx7cm72qy2c0643cm1ipngd87aqwkcdp-glibc-2.40-66/lib:/nix/store/xm08aqdd7pxcdhm0ak6aqb1v7hw5q6ri-gcc-14.3.0-lib/lib]

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

Add a 👍 reaction to pull requests you find important.

@HeitorAugustoLN HeitorAugustoLN changed the base branch from master to staging November 25, 2025 17:22
@nixpkgs-ci nixpkgs-ci bot closed this Nov 25, 2025
@nixpkgs-ci nixpkgs-ci bot reopened this Nov 25, 2025
github-actions[bot]

This comment was marked as resolved.

Previously, the refactor of `buildRustPackage` caused `RUSTFLAGS` to be defined in the environment unconditionally, often resulting in an empty string (`RUSTFLAGS=""`).

When `RUSTFLAGS` is set (even to an empty string), Cargo prioritizes it and ignores `CARGO_TARGET_<TRIPLE>_RUSTFLAGS`. This behavior broke packages like `cosmic-comp` that rely on target-specific flags for linking libraries (e.g., `libEGL`).

This change calculates the flags beforehand and uses `lib.optionalAttrs` to ensure `RUSTFLAGS` is only added to the environment if it contains a value. This restores the ability for Cargo to fall back to target-specific configuration variables.
@github-actions github-actions bot dismissed their stale review November 25, 2025 17:24

All good now, thank you!

@Aleksanaa Aleksanaa added the backport staging-25.11 Backport PR automatically label Nov 25, 2025
@Aleksanaa
Copy link
Member

Good, but this doesn't change the fact that platform-specific RUSTFLAGS will lose effect once RUSTFLAGS is set 🫠 Only bringing us back to previous status

@nixpkgs-ci nixpkgs-ci bot requested review from winterqt and zowoq November 25, 2025 17:32
@HeitorAugustoLN
Copy link
Member Author

Good, but this doesn't change the fact that platform-specific RUSTFLAGS will lose effect once RUSTFLAGS is set 🫠 Only bringing us back to previous status

Right, but I am pretty sure that this is standard cargo behavior, so there's not much we can do about it

@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. labels Nov 25, 2025
@thefossguy
Copy link
Member

I think the relevant issue is upstream: rust-lang/cargo#13981
TL;DR: We specify --target (I think always) and that causes CARGO_TARGET_${TRIPLET}_RUSTFLAGS to not be respected.

@thefossguy
Copy link
Member

Should we start using our own flags RUSTFLAGS_TARGET and RUSTFLAGS_HOST which then are merged? That would help mitigate overwriting RUSTFLAGS already defined for cross compilation.

@Aleksanaa
Copy link
Member

Should we start using our own flags RUSTFLAGS_TARGET and RUSTFLAGS_HOST which then are merged? That would help mitigate overwriting RUSTFLAGS already defined for cross compilation.

Good idea, try if it works for cosmic?

SFrijters added a commit to SFrijters/nix-qemu-esp32c3-rust-example that referenced this pull request Nov 25, 2025
Caused by NixOS/nixpkgs#435278

RUSTFLAGS is now always set and overrides whatever we specify in .cargo/config.toml

TODO: Keep an eye on

NixOS/nixpkgs#464707
NixOS/nixpkgs#464992

and possibly revert or adjust to the new status quo in nixpkgs.
@Aleksanaa
Copy link
Member

This is the same as #464707

@Aleksanaa
Copy link
Member

Should we start using our own flags RUSTFLAGS_TARGET and RUSTFLAGS_HOST which then are merged? That would help mitigate overwriting RUSTFLAGS already defined for cross compilation.

I don't see these variables exist?

@HeitorAugustoLN
Copy link
Member Author

Closing in favor of #464707

@HeitorAugustoLN HeitorAugustoLN deleted the cargo-target-fix branch November 26, 2025 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. backport staging-25.11 Backport PR automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cosmic-comp: crash durring startup

3 participants