Skip to content

pkgs/top-level/stage.nix: add crossStdenv#410056

Draft
RossComputerGuy wants to merge 12 commits intoNixOS:masterfrom
RossComputerGuy:feat/cross-stdenv
Draft

pkgs/top-level/stage.nix: add crossStdenv#410056
RossComputerGuy wants to merge 12 commits intoNixOS:masterfrom
RossComputerGuy:feat/cross-stdenv

Conversation

@RossComputerGuy
Copy link
Member

Things done

Adds the new crossStdenv mechanism to be used inside nixpkgs instead of variants. Tested several armTrustedFirmware derivations as they appear on search and they compile fine.

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • Nixpkgs 25.11 Release Notes (or backporting 24.11 and 25.05 Nixpkgs Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
  • NixOS 25.11 Release Notes (or backporting 24.11 and 25.05 NixOS Release notes)
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: stdenv Standard environment label May 23, 2025
@RossComputerGuy RossComputerGuy force-pushed the feat/cross-stdenv branch 3 times, most recently from 33c476e to 2039bbf Compare May 23, 2025 07:10
@github-actions github-actions bot added 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels May 23, 2025
@RossComputerGuy RossComputerGuy force-pushed the feat/cross-stdenv branch 2 times, most recently from 97a57cc to 8f22062 Compare May 24, 2025 02:28
@RossComputerGuy RossComputerGuy marked this pull request as ready for review May 24, 2025 02:29
@Aleksanaa
Copy link
Member

So

  • What's the final goal(s) of crossStdenv refactor?
  • How should we change our use cases of pkgsCross?

@RossComputerGuy
Copy link
Member Author

Final goal is to provide cross compilation stdenv's which align with the pattern as described in #400351 (comment). These are purely the stdenv so eval will be smaller than bootstrapping another nixpkgs instance. As for pkgsCross, after this PR merges we will move everything off it it and move it to variants.

@RossComputerGuy
Copy link
Member Author

Did a little benchmarking for musl on aarch64-linux.

crossStdenv.configs.aarch64-unknown-linux-musl

{
  "cpuTime": 0.43762001395225525,
  "envs": {
    "bytes": 5428488,
    "elements": 369695,
    "number": 308866
  },
  "gc": {
    "cycles": 1,
    "heapSize": 402915328,
    "totalBytes": 79396896
  },
  "list": {
    "bytes": 717400,
    "concats": 4609,
    "elements": 89675
  },
  "nrAvoided": 294217,
  "nrExprs": 135112,
  "nrFunctionCalls": 295668,
  "nrLookups": 152167,
  "nrOpUpdateValuesCopied": 2227726,
  "nrOpUpdates": 12343,
  "nrPrimOpCalls": 114176,
  "nrThunks": 263414,
  "sets": {
    "bytes": 42449136,
    "elements": 2626304,
    "number": 26767
  },
  "sizes": {
    "Attr": 16,
    "Bindings": 16,
    "Env": 8,
    "Value": 24
  },
  "symbols": {
    "bytes": 321885,
    "number": 32150
  },
  "time": {
    "cpu": 0.43762001395225525,
    "gc": 0.043000000000000003,
    "gcFraction": 0.09825876017793679
  },
  "values": {
    "bytes": 20363664,
    "number": 848486
  }
}

pkgsMusl.stdenv

{
  "cpuTime": 0.5026220083236694,
  "envs": {
    "bytes": 4656672,
    "elements": 324861,
    "number": 257223
  },
  "gc": {
    "cycles": 1,
    "heapSize": 402915328,
    "totalBytes": 125050816
  },
  "list": {
    "bytes": 601264,
    "concats": 4389,
    "elements": 75158
  },
  "nrAvoided": 262676,
  "nrExprs": 131023,
  "nrFunctionCalls": 242926,
  "nrLookups": 125858,
  "nrOpUpdateValuesCopied": 3930750,
  "nrOpUpdates": 13551,
  "nrPrimOpCalls": 104327,
  "nrThunks": 322854,
  "sets": {
    "bytes": 73163712,
    "elements": 4542912,
    "number": 29820
  },
  "sizes": {
    "Attr": 16,
    "Bindings": 16,
    "Env": 8,
    "Value": 24
  },
  "symbols": {
    "bytes": 320546,
    "number": 32025
  },
  "time": {
    "cpu": 0.5026220083236694,
    "gc": 0.07200000000000001,
    "gcFraction": 0.14324880090335151
  },
  "values": {
    "bytes": 32448624,
    "number": 1352026
  }
}

@github-actions github-actions bot added the 8.has: documentation This PR adds or changes documentation label May 25, 2025
@nix-owners nix-owners bot requested review from GetPsyched and hsjobeki May 25, 2025 03:21
@nix-owners nix-owners bot requested a review from fricklerhandwerk May 25, 2025 03:21
@github-actions github-actions bot removed the 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. label May 25, 2025
@RossComputerGuy RossComputerGuy force-pushed the feat/cross-stdenv branch 2 times, most recently from 448d600 to b9e4dea Compare May 25, 2025 04:48
@Ericson2314
Copy link
Member

I just want to reiterate that things being called "cross" today is entirely tech debt and should be gotten rid of. I would really like @NixOS/stdenv to help out @cloudripper who has taken on my GCC repackaging, so we can get rid of that entirely. I would feel more comfortable doing this sort of change after that, because we'd be working atop a better foundation.

@RossComputerGuy
Copy link
Member Author

I don't really see how nixpkgs could function without being able to perform cross. I do agree that refactoring GCC would definitely help. I also think #365057 and #409851 (& similar) would definitely help out since we could make things more a part of the CC wrapper.

@RossComputerGuy RossComputerGuy mentioned this pull request Jun 5, 2025
13 tasks
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jun 9, 2025
@wolfgangwalther
Copy link
Contributor

wolfgangwalther commented Jul 20, 2025

Did a little benchmarking for musl on aarch64-linux.
crossStdenv.configs.aarch64-unknown-linux-musl
[..]
pkgsMusl.stdenv

Note that pkgsMusl is not a cross-setup. You probably need to compare against pkgsCross.aarch64-multiplatform-musl.

@wolfgangwalther
Copy link
Contributor

Is crossStdenv also going to help to get rid of pkgsi686Linux?

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.

Why call this crossStdenv and not provide these nice attributes on stdenv directly?

stdenv.for.config.wasi32-unknown-wasi

Avoids the term "cross".

Comment on lines +12 to +13
- A new cross compilation mechanism called `crossStdenv` has been added. It is recommended to use `crossStdenv` as
variants such as `pkgsCross`, `pkgsLLVM`, and `pkgsStatic` usage will no longer be permitted to be used in nixpkgs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Although not used everywhere, yet, and unfortunately very hard to enforce via CI, we have a "one sentence per line" rule for docs:

https://github.com/NixOS/nixpkgs/blob/master/doc/README.md#one-sentence-per-line

Thus:

Suggested change
- A new cross compilation mechanism called `crossStdenv` has been added. It is recommended to use `crossStdenv` as
variants such as `pkgsCross`, `pkgsLLVM`, and `pkgsStatic` usage will no longer be permitted to be used in nixpkgs.
- A new cross compilation mechanism called `crossStdenv` has been added.
It is recommended to use `crossStdenv` as variants such as `pkgsCross`, `pkgsLLVM`, and `pkgsStatic` usage will no longer be permitted to be used in nixpkgs.

Comment on lines +134 to +135
#### How do I cross compile in nixpkgs? {#cross-qa-cross-compile-in-nixpkgs}
Since NixOS 25.05, package sets such as `pkgsLLVM`, `pkgsMusl`, and `pkgsCross` have been moved into being labeled as variants. Future NixOS releases will disable the use of variants in nixpkgs. This is due to the increased evaluation since variants create a new nixpkgs instance atop the existing one. NixOS 24.11 introduces a feature called `crossStdenv`, this is a set of stdenv's for cross compiling without instantiating a new nixpkgs instance. `crossStdenv` contains a recursive attribute set called `predicated` and provides boolean options to a platform attribute set. It also contains `libcs` which is an attribute set of libc's that can be utilized. Finally, the recursive attribute set contains `configs` and `native`. `configs` is a set of attribute sets which contains stdenv's for each platform target that is supported for cross compiling. An example of the new pattern for something like `pkgsCross.wasi32.hello` would be:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use one sentence per line here, too, otherwise it's really hard to comment nicely on each sentence.

Suggested change
#### How do I cross compile in nixpkgs? {#cross-qa-cross-compile-in-nixpkgs}
Since NixOS 25.05, package sets such as `pkgsLLVM`, `pkgsMusl`, and `pkgsCross` have been moved into being labeled as variants. Future NixOS releases will disable the use of variants in nixpkgs. This is due to the increased evaluation since variants create a new nixpkgs instance atop the existing one. NixOS 24.11 introduces a feature called `crossStdenv`, this is a set of stdenv's for cross compiling without instantiating a new nixpkgs instance. `crossStdenv` contains a recursive attribute set called `predicated` and provides boolean options to a platform attribute set. It also contains `libcs` which is an attribute set of libc's that can be utilized. Finally, the recursive attribute set contains `configs` and `native`. `configs` is a set of attribute sets which contains stdenv's for each platform target that is supported for cross compiling. An example of the new pattern for something like `pkgsCross.wasi32.hello` would be:
#### How do I cross compile in nixpkgs? {#cross-qa-cross-compile-in-nixpkgs}
Since NixOS 25.05, package sets such as `pkgsLLVM`, `pkgsMusl`, and `pkgsCross` have been labeled as variants. Future NixOS releases will disallow the use of variants in nixpkgs. This is due to the increased evaluation time since each variant creates a new nixpkgs instance. NixOS 25.11 introduces `crossStdenv`, which is a set of stdenvs for cross compiling without instantiating a new nixpkgs instance. It contains a recursive attribute set `predicated` and provides boolean options to a platform attribute set. It also contains `libc` which is an attribute set of libcs that can be utilized. Finally, it contains `configs` and `native`. `configs` is a set of attribute sets which contains stdenvs for each platform target that is supported for cross compiling. An example of the new pattern for something like `pkgsCross.wasi32.hello` would be:

(I put a few smaller changes in this, but it won't be highlighted due to everything being on one line here ... :/)

It mentions native but doesn't explain it.

}
```

Note that the `stdenv` is overridden to provide the `wasi32-unknown-wasi` stdenv with the `useLLVM` predicate enabled. It is important to override all runtime dependencies to ensure the package properly cross compiles.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Note that the `stdenv` is overridden to provide the `wasi32-unknown-wasi` stdenv with the `useLLVM` predicate enabled. It is important to override all runtime dependencies to ensure the package properly cross compiles.
Note that `stdenv` is overridden to provide the `wasi32-unknown-wasi` stdenv with the `useLLVM` predicate enabled.
It is important to override all runtime dependencies to ensure the package properly compiles.

Comment on lines +441 to +446
"musl"
"glibc"
"newlib"
"newlib-nano"
"msvcrt"
"ucrt"
Copy link
Contributor

Choose a reason for hiding this comment

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

People will add stuff over time and it always confuses me. Where do I add this?

Might as well sort alphabetically instead of randomly:

Suggested change
"musl"
"glibc"
"newlib"
"newlib-nano"
"msvcrt"
"ucrt"
"glibc"
"msvcrt"
"musl"
"newlib"
"newlib-nano"
"ucrt"

Comment on lines +435 to +438
predicates = [
"useLLVM"
"isStatic"
];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
predicates = [
"useLLVM"
"isStatic"
];
predicates = [
"isStatic"
"useLLVM"
];

Comment on lines +449 to +468
configs = [
"avr"
"aarch64-unknown-linux-gnu"
"aarch64-unknown-linux-musl"
"armv7l-unknown-linux-gnueabihf"
"arm-none-eabi"
"i686-unknown-linux-gnu"
"i686-w64-mingw32"
"s390-unknown-linux-gnu"
"s390x-unknown-linux-gnu"
"riscv32-unknown-linux-gnu"
"riscv32-unknown-linux-musl"
"riscv64-unknown-linux-gnu"
"riscv64-unknown-linux-musl"
"wasm32-unknown-none"
"wasm32-unknown-wasi"
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
"x86_64-w64-mingw32"
];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
configs = [
"avr"
"aarch64-unknown-linux-gnu"
"aarch64-unknown-linux-musl"
"armv7l-unknown-linux-gnueabihf"
"arm-none-eabi"
"i686-unknown-linux-gnu"
"i686-w64-mingw32"
"s390-unknown-linux-gnu"
"s390x-unknown-linux-gnu"
"riscv32-unknown-linux-gnu"
"riscv32-unknown-linux-musl"
"riscv64-unknown-linux-gnu"
"riscv64-unknown-linux-musl"
"wasm32-unknown-none"
"wasm32-unknown-wasi"
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
"x86_64-w64-mingw32"
];
configs = [
"aarch64-unknown-linux-gnu"
"aarch64-unknown-linux-musl"
"arm-none-eabi"
"armv7l-unknown-linux-gnueabihf"
"avr"
"i686-unknown-linux-gnu"
"i686-w64-mingw32"
"riscv32-unknown-linux-gnu"
"riscv32-unknown-linux-musl"
"riscv64-unknown-linux-gnu"
"riscv64-unknown-linux-musl"
"s390-unknown-linux-gnu"
"s390x-unknown-linux-gnu"
"wasm32-unknown-none"
"wasm32-unknown-wasi"
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
"x86_64-w64-mingw32"
];

Comment on lines +507 to +510
configs = mkConfigSet platform;
predicated = mkPredicatedSet platform;
libc = mkLibcSet platform;
native = overrideStdenv (
Copy link
Contributor

Choose a reason for hiding this comment

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

libc is singular (although the docs currently say libcs) and configs is plural.

We only select a single one at any given point, so I think both should be singular.

Suggested change
configs = mkConfigSet platform;
predicated = mkPredicatedSet platform;
libc = mkLibcSet platform;
native = overrideStdenv (
config = mkConfigSet platform;
predicated = mkPredicatedSet platform;
libc = mkLibcSet platform;
native = overrideStdenv (

@philiptaron
Copy link
Contributor

Tristan, this PR looks blocked on your response to feedback. I'm marking it as draft until you've got the cycles to unblock it.

@philiptaron philiptaron marked this pull request as draft September 12, 2025 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: stdenv Standard environment 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

6 participants