# Help me understand two load-bearing \`assert\` statements

**URL:** https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420
**Category:** Nixpkgs Architecture
**Created:** [August 31, 2024, 3:27pm UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420 "2024-08-31T15:27:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![PhilipTaron](https://discourse.nixos.org/user_avatar/discourse.nixos.org/philiptaron/32/35913_2.png) [@PhilipTaron](https://discourse.nixos.org/u/PhilipTaron)
#### Post date: [August 31, 2024, 3:27pm UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/1 "2024-08-31T15:27:46Z")

</div>

In `all-packages.nix`, there are as of this morning only two two `assert` statements, and they both appear to be “load-bearing” in the sense that if the asserts are removed, packages which used to compile no longer do.

> **One is for gccWithoutTargetLibc.**
>
> ```auto
> # The GCC used to build libc for the target platform. Normal gccs will be
> # built with, and use, that cross-compiled libc.
> gccWithoutTargetLibc = assert stdenv.targetPlatform != stdenv.hostPlatform; let
> libcCross1 = binutilsNoLibc.libc;
> in wrapCCWith {
> cc = gccFun {
> # copy-pasted
> inherit noSysDirs;
> majorMinorVersion = toString default-gcc-version;
> 
> reproducibleBuild = true;
> profiledCompiler = false;
> 
> isl = if !stdenv.isDarwin then isl_0_20 else null;
> 
> withoutTargetLibc = true;
> langCC = false;
> libcCross = libcCross1;
> targetPackages.stdenv.cc.bintools = binutilsNoLibc;
> enableShared =
> stdenv.targetPlatform.hasSharedLibraries
> 
> # temporarily disabled due to breakage;
> # see https://github.com/NixOS/nixpkgs/pull/243249
> && !stdenv.targetPlatform.isWindows
> && !(stdenv.targetPlatform.useLLVM or false)
> ;
> };
> bintools = binutilsNoLibc;
> libc = libcCross1;
> extraPackages = [];
> };
> 
> ```

> **The other is for libcCross.**
>
> ```auto
> libcCross = assert stdenv.targetPlatform != stdenv.buildPlatform; libcCrossChooser stdenv.targetPlatform.libc;
> 
> ```

If either of these asserts are removed, `wine` (for instance) fails to build.

I’m trying to understand where, why, and how these asserts are “load-bearing”.

They’re pretty old. @Ericson2314 added it for gcc in [bf17d6dacf24](https://github.com/NixOS/nixpkgs/commit/bf17d6dacf2408436ffb5a2f8c87a31c88ec28e2), which has a wonderfully long commit message.

> **My best guess at why it's load-bearing is this line of code in splice.nix.**
>
> ```auto
> # Get the set of outputs of a derivation. If one derivation fails to
> # evaluate we don't want to diverge the entire splice, so we fall back
> # on {}
> tryGetOutputs = value0:
> let
> inherit (builtins.tryEval value0) success value;
> in
> getOutputs (lib.optionalAttrs success value);
> 
> ```

That would tie evaluation success or failure to a difference in which packages are chosen.

So, several questions:

1. Is this analysis above right?
2. Is there another way of doing this that doesn’t have an assert in `all-packages.nix`? What would an equivalent refactor look like?
3. Is there a direction that this code should go in, other than this structure?

Your thoughts are wanted, especially @amjoseph and @emily and @Ericson2314 but all are desired.

---

<div class="post-metadata">

### Author: ![Ericson2314](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ericson2314/32/316_2.png) [@Ericson2314](https://discourse.nixos.org/u/Ericson2314)
#### Post date: [September 1, 2024, 3:19am UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/2 "2024-09-01T03:19:29Z")

</div>

From memory: Splicing has a `tryEval`. The assets I think are caught by that, which avoids infinite recursion.

[Clean up cross bootstrapping, take 2 by Ericson2314 · Pull Request #321525 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/pull/321525) I (or you? :)) should rebase soon which is a small step in the direction of making this stuff less confusing.

---

<div class="post-metadata">

### Author: ![PhilipTaron](https://discourse.nixos.org/user_avatar/discourse.nixos.org/philiptaron/32/35913_2.png) [@PhilipTaron](https://discourse.nixos.org/u/PhilipTaron)
#### Post date: [September 2, 2024, 2:41pm UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/3 "2024-09-02T14:41:35Z")

</div>

Thanks for your insight John (@Ericson2314)!

#321525 looks great, though I haven’t put it to the machine to see what it says yet. Is there anything that makes it unmergable today, either through incompleteness or through lack of function?

And yeah, continuing to caretake and evolve the stdenv remains one of my callings where I think I can contribute in nixpkgs. So I’d love to help with this big, old, and worthwhile mission.

---

<div class="post-metadata">

### Author: ![Ericson2314](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ericson2314/32/316_2.png) [@Ericson2314](https://discourse.nixos.org/u/Ericson2314)
#### Post date: [September 2, 2024, 4:19pm UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/4 "2024-09-02T16:19:15Z")

</div>

> [@PhilipTaron](#):
>
> Is there anything that makes it unmergable today, either through incompleteness or through lack of function?

No just some very trivial conflicts, which probably were made by me too.

> [@PhilipTaron](#):
>
> And yeah, continuing to caretake and evolve the stdenv remains one of my callings where I think I can contribute in nixpkgs. So I’d love to help with this big, old, and worthwhile mission.

Glad to hear it! 🙂

---

<div class="post-metadata">

### Author: ![amjoseph](https://discourse.nixos.org/user_avatar/discourse.nixos.org/amjoseph/32/28378_2.png) [@amjoseph](https://discourse.nixos.org/u/amjoseph)
#### Post date: [September 6, 2024, 5:05am UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/5 "2024-09-06T05:05:37Z")

</div>

> [@PhilipTaron](#):
>
> If either of these asserts are removed, `wine` (for instance) fails to build.

Sorry for the slow reply.

At 0e0665d1e0a05ade073ff23e54b6e389c928af20 I was able to remove the first `assert` with no change to the instantiated derivation.

Regarding `libcCross`: the assertion here tickles (via wine, which is always the biggest headache for our cross-compilation machinery) the `tryEval` that @Ericson2314 mentioned. In this case, the `gcc.libgcc` output is `throw`ing due to checking if `libcCross==null`. The same effect is had by simply setting `libcCross` to `null` in that situation, and this way we don’t have to lean on `tryEval`.

Here’s a commit that does so:

[https://git.sr.ht/~amjoseph/nixpkgs/commit/fa8b6b8e2f4939c7c433ec3087cbc677e08fecca](https://git.sr.ht/~amjoseph/nixpkgs/commit/fa8b6b8e2f4939c7c433ec3087cbc677e08fecca)

~~I have not tested this too extensively, though – I’m running `nix build tests.cross.sanity` on it to make sure nothing breaks, so consider it a draft until that happens.~~ built

---

<div class="post-metadata">

### Author: ![amjoseph](https://discourse.nixos.org/user_avatar/discourse.nixos.org/amjoseph/32/28378_2.png) [@amjoseph](https://discourse.nixos.org/u/amjoseph)
#### Post date: [September 7, 2024, 3:01am UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/6 "2024-09-07T03:01:13Z")

</div>

> [@amjoseph](#):
>
> I’m running `nix build tests.cross.sanity` on it to make sure nothing breaks, so consider it a draft until that happens.

Built everything except `pkgs.pkgsCross.aarch64-multiplatform.qutebrowser-qt5`, which seems to have been broken by something else already _sigh_.

So, if you want the `assert` gone, you can submit that patch and blame me in the unlikely event it breaks anything.

> [@PhilipTaron](#):
>
> And yeah, continuing to caretake and evolve the stdenv remains one of my callings where I think I can contribute in nixpkgs. So I’d love to help with this big, old, and worthwhile mission.

Glad to hear this. The `libcCross` spaghetti-pile was sort of next on the list of stuff to clean up when I moved on to other things. It is sorely in need of cleaning-up.

The native bootstrap used to be a mess like this (a bunch of mutually-recursive packages shoved into the top level) until @Ericson2314 factored them out into the separate stages that we currently have in `pkgs/stdenv/linux/default.nix` (which is not really as linuxgcc-specific as its name would have you believe).

The ultimate goal was to have that nice clean multistage bootstrap handle not just the native case, but the cross case as well, as a single codepath. That’s pretty ambitious though. Just separating the cross bootstrap into isolated stages would probably be a more feasible first step. Then we could try slowly merging the staged-cross-bootstrap with the native-cross-bootstrap.

Anyways basically anything will be an improvement on the current state of `libcCross`. It’s hideously ugly. It would be hard to make it any worse 😄

---

<div class="post-metadata">

### Author: ![PhilipTaron](https://discourse.nixos.org/user_avatar/discourse.nixos.org/philiptaron/32/35913_2.png) [@PhilipTaron](https://discourse.nixos.org/u/PhilipTaron)
#### Post date: [September 7, 2024, 4:02pm UTC](https://discourse.nixos.org/t/help-me-understand-two-load-bearing-assert-statements/51420/7 "2024-09-07T16:02:23Z")

</div>

Thank you for this work and analysis! I’ve opened [libcCross: set to `null` rather than using `assert` in nonsense scenarios by philiptaron · Pull Request #340331 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/pull/340331) to integrate this commit into nixpkgs.
