Skip to content

rustPlatform.buildRustPackage: miscelaneous modernization to unify unpackPhase and cargoDeps.unpackPhase#435278

Merged
ShamrockLee merged 2 commits intoNixOS:stagingfrom
ShamrockLee:build-rust-package-overriding-pre-staging
Nov 4, 2025
Merged

rustPlatform.buildRustPackage: miscelaneous modernization to unify unpackPhase and cargoDeps.unpackPhase#435278
ShamrockLee merged 2 commits intoNixOS:stagingfrom
ShamrockLee:build-rust-package-overriding-pre-staging

Conversation

@ShamrockLee
Copy link
Contributor

@ShamrockLee ShamrockLee commented Aug 20, 2025

  1. Pass all environment variables (including RUST_LOG) via env
  2. Run cargoDepsHook (a hook for tweaks before Rust vendor from cargoDeps or cargoVendorDir gets unpacked) inside cargoSetupPostUnpackHook instead of prepending to postUnpack.
  3. Make it feasible to unify the postUnpack of the main derivation and cargoDeps in PR rustPlatform.buildRustPackage: fix cargoDeps inherited attribute overriding #435239

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.

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch 2 times, most recently from 458a7d5 to 9e47b10 Compare August 20, 2025 14:39
@ShamrockLee ShamrockLee requested a review from aristidb August 20, 2025 14:42
@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from 9e47b10 to 7be5f9c Compare August 20, 2025 14:45
@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 Aug 20, 2025
@ShamrockLee ShamrockLee marked this pull request as ready for review August 20, 2025 15:05
@nix-owners nix-owners bot requested review from figsoda, winterqt and zowoq August 20, 2025 15:07
@ShamrockLee
Copy link
Contributor Author

@ofborg build nnd
@ofborg build scuba
@ofborg build tests.fetchtorrent.http-link-rqbit tests.fetchtorrent.http-link-rqbit-flattened
tests.fetchtorrent.http-link-rqbit-unflattened tests.fetchtorrent.magnet-link-rqbit tests.fetchtorrent.magnet-link-rqbit-flattened tests.fetchtorrent.magnet-link-rqbit-unflattened

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from 7be5f9c to 8613150 Compare August 20, 2025 16:50
@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Aug 20, 2025

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review --package nnd --package scuba --package libchewing --package uwc
Commit: 8613150cc4e1987e70fae2c04efa922edb1df2c8


x86_64-linux

✅ 5 packages built:
  • libchewing
  • nnd
  • scuba
  • scuba.dist (scuba.dist.dist)
  • uwc

Note

The above nixpkgs-review --branch is set to nixos-unstable (which is the before the merge base of master and staging) to reduce rebuilds.

@ShamrockLee
Copy link
Contributor Author

@ofborg build cargo

rustPlatform.buildRustPackage + cargoHash
@ofborg build uwc

rustPlatform.buildRustPackage + cargoHash + postPatch
@ofborg build rqbit

stdenv.mkDerivation + rustPlatform.cargoSetupHook
@ofborg build libchewing

pkgsCross.musl64.rustPlatform.buildRustPackage + cargoHash
@ofborg build nnd

pkgsStatic.rustPlatform.buildRustPackage + cargoHash + sourceRoot used inside a Python application
@ofborg build scuba

RUSTFLAGS specified as a list; additional env specified
@ofborg build cosmic-edit

@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Aug 21, 2025

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review --package cosmic-edit --package rqbit --package cargo
Commit: 8613150cc4e1987e70fae2c04efa922edb1df2c8


x86_64-linux

✅ 3 packages built:
  • cargo
  • cosmic-edit
  • rqbit

Note

The above nixpkgs-review --branch is set to nixos-unstable (which is the before the merge base of master and staging) to reduce rebuilds.

Comment on lines +95 to +96
# Workaround the existing RUSTFLAGS specified as a list.
+ interpolateString (args.RUSTFLAGS or "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this specification as a list already happening or an extension? The previous code didn't do that, so I suspect that this would be a bug fix, if there were already cases for it?

With this upper-case spelling, I'd argue that this should be treated as an environment variable and thus lists should not be supported anywhere.

If we want a structured way to specify them, this should be come rustFlags?

Copy link
Contributor Author

@ShamrockLee ShamrockLee Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this specification as a list already happening or an extension?

It is already happening.
Violated packages (via tree-wide evaluation):

  • alvr
  • asusctl
  • cosmic-edit
  • mitra
  • proxmox-backup-client

The previous code didn't do that

The previous code expect it to be string-concatable via the + operator, which indicates that it should be a string. However, it is only string-concatenated in the lib.optionalAttrs block requiring buildType == debug, which is seldom triggered.

If we want a structured way to specify them, this should be come rustFlags?

I think so. While implementing #359744, I gradually think that we should prepend goFlags/rustFlags explicitly to the compiler flags instead of relying on the GOFLAGS/RUSTFLAGS environment variables. Let's implement such interface change in a separate PR.

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from 8613150 to 34c2358 Compare August 23, 2025 02:31
Copy link
Contributor

@wolfgangwalther wolfgangwalther left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diff LGTM, didn't test. I don't know enough about the Rust / Cargo ecosystem in Nixpkgs to say too much beyond simply reading the code.

Copy link
Contributor Author

@ShamrockLee ShamrockLee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer have to consider backporting to Nixpkgs 25.05.

I'll drop some overly conservative backport compatibility code.

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from 34c2358 to de2a3da Compare October 7, 2025 00:06
@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Oct 27, 2025
@ShamrockLee ShamrockLee added this pull request to the merge queue Nov 3, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 3, 2025
@wolfgangwalther
Copy link
Contributor

Fails eval in the queue!

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from de2a3da to a6e0d52 Compare November 3, 2025 21:14
@ShamrockLee
Copy link
Contributor Author

Let's rebase and try again.

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from a6e0d52 to 7f229fb Compare November 3, 2025 21:19
@ShamrockLee
Copy link
Contributor Author

I see. I forgot to handle the null values.

@ShamrockLee ShamrockLee force-pushed the build-rust-package-overriding-pre-staging branch from 7f229fb to cdb722f Compare November 3, 2025 21:25
@ShamrockLee ShamrockLee added this pull request to the merge queue Nov 4, 2025
Merged via the queue into NixOS:staging with commit e43e53f Nov 4, 2025
28 of 30 checks passed
@ShamrockLee ShamrockLee deleted the build-rust-package-overriding-pre-staging branch November 4, 2025 15:50
@gepbird
Copy link
Contributor

gepbird commented Nov 24, 2025

I got a new failing build and bisected to this PR, more specifically to this diff:

diff --git a/pkgs/build-support/rust/build-rust-package/default.nix b/pkgs/build-support/rust/build-rust-package/default.nix
index 577463a0f689..bc2372278283 100644
--- a/pkgs/build-support/rust/build-rust-package/default.nix
+++ b/pkgs/build-support/rust/build-rust-package/default.nix
@@ -33,6 +33,7 @@ lib.extendMkDerivation {
     "cargoUpdateHook"
     "cargoLock"
     "useFetchCargoVendor"
+    "RUSTFLAGS"
   ];
 
   extendDrvArgs =
@@ -89,6 +90,10 @@ lib.extendMkDerivation {
       true;
 
     {
+      env = {
+        RUSTFLAGS = interpolateString (args.RUSTFLAGS or "");
+      };
+
       cargoDeps =
         if cargoVendorDir != null then
           null

The package sets CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS variable. Before this change env | grep RUSTFLAGS results in:

CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS=-L /nix/store/kchvqpqwlnh0442x75bj4y7hrghv9sh9-mingw_w64-pthreads-x86_64-w64-mingw32-13.0.0/lib                                                    

And after the change it results in:

CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS=-L /nix/store/kchvqpqwlnh0442x75bj4y7hrghv9sh9-mingw_w64-pthreads-x86_64-w64-mingw32-13.0.0/lib                                                    
RUSTFLAGS=                                                                                                                                                                                      

This causes the first variable to be ignored and generates this error: /nix/store/7via7kz8xlfwimvmpncf8ldda8chjpsc-x86_64-w64-mingw32-binutils-2.44/bin/x86_64-w64-mingw32-ld: cannot find -l:libpthread.a: No such file or directory

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.
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. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants