March, mtune, target-cpu=native on 25.05

Hey guys, I wanted to try again to recompile my entire system with these optimization flags. But since last time I tried this the options have greatly changed. This is what I tried and no matter what I couldn’t solve this error.

  nix.settings.system-features = [
    "nixos-test"
    "benchmark"
    "big-parallel"
    "kvm"
    "gccarch-znver4"
  ];
  nixpkgs.hostPlatform = {
    # The system will take many hours and run out of space to rebuild with native support
    gcc.arch = "znver4";
    gcc.tune = "znver4";
    system = "x86_64-linux";
  };
error: a 'x86_64-linux' with features {gccarch-znver4} is required to build '/nix/store/nh432g6ixajfmk4czvvqc46j4a5brqja-bootstrap-stage0-glibc-bootstrapFiles.drv', but I am a 'x86_64-linux' with features {benchmark, big-parallel, kvm, nixos-test}

I’ve tried with and without system-features, older posts don’t apply since this is a pure rebuild and the options have greatly changed.

1 Like

You need to first change the system features, activate the change and then afterwards you can set the host platform.

Otherwise your nix will not know to be capable of building the target architecture.
That’s the reason i have it just set the system features all the time (as I sometimes also play around with march builds)

2 Likes

I’m not sure, should I remove declaring the features manually?

       … while evaluating the module argument `pkgs' in "/nix/store/fbnlawcsvls3033n9ddi81grgw030881-source/nixos/modules/services/hardware/bluetooth.nix":

       … while evaluating the option `nixpkgs.localSystem':

       … while evaluating the option `nixpkgs.system':

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: Neither nixpkgs.hostPlatform nor the legacy option nixpkgs.system has been set.
       You can set nixpkgs.hostPlatform in hardware-configuration.nix by re-running
       a recent version of nixos-generate-config.
       The option nixpkgs.system is still fully supported for NixOS 22.05 interoperability,
       but will be deprecated in the future, so we recommend to set nixpkgs.hostPlatform.

I just commented out the hostPlatform block

Sorry my previous wording was not very accurate and lead to that error. You always need to have nixpkgs system attribute or nixpkgs.hostplatform.system defined.
Otherwise it will not know if you build x86, arm or something else.
So just remove the gcc attributes and have nix.settings.system-features defined.

Then switch the config (so that the nix.conf change is applied), and then you should be able to set the gcc attributes again and build your system config.

Do I need the manually declared features?

Is there a way to fix this?

building the system configuration...
error: cannot read directory "/nix/store/6a4hf0bqvna6dx2ph89r1r4ncpwd2w4d-source/src/webui/api/serialize": Too many open files
error: some substitutes for the outputs of derivation '/nix/store/vg6h8hwkgq4m05490yxjy21a9s62rrzn-source.drv' failed (usually happens due to networking issues); try '--fallback' to build derivation from source
error: 1 dependencies of derivation '/nix/store/v6hy0nmc4x0xzyjhf35xy5xp527pxn0y-qbittorrent-5.1.0.drv' failed to build
error (ignored): error: cannot unlink "/tmp/nix-build-asio-1.24.0.drv-0/build": Directory not empty
error (ignored): error: cannot unlink "/tmp/nix-build-boost-1.86.0.drv-0/build/boost_1_86_0/libs": Directory not empty
error (ignored): error: cannot unlink "/tmp/nix-build-boost-1.87.0.drv-0/build/boost_1_87_0/libs": Directory not empty
error: 1 dependencies of derivation '/nix/store/wff043qjjrny8ki63k9677lf5gql96af-system-path.drv' failed to build
error (ignored): error: cannot unlink "/tmp/nix-build-boost-1.87.0.drv-1/build/boost_1_87_0/libs": Directory not empty
error: 1 dependencies of derivation '/nix/store/sk2c2hlj2m7lq044p1gmisacps9hgl0j-nixos-system-PopTart-25.05.20250815.ad7196a.drv' failed to build


After messing with ulimit not working properly on root, I got it working.

Add features FIRST THEN REBUILD WITH GCC ATTRIBUTES 

  nix.settings.system-features = [
    "nixos-test"
    "benchmark"
    "big-parallel"
    "kvm"
    "gccarch-znver4" # <--- Your arch
  ];
  nixpkgs.hostPlatform = {
    # The system will take many hours and run out of space to rebuild with native support
    gcc.arch = "znver4";
    gcc.tune = "znver4";
    system = "x86_64-linux";
  };

  # nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";

Does this apply to rustc?

I am actually not sure right now.
It’s a longer time since I played around with it, so I don’t remember.
I tried to look up the wrapper codes for rustc calls and did not find anything that passes those arguments.

You might want to check the arguments passed whilst building some rust app to be sure.

If you’re trying to add arch/tune parameters to rust programs, AFAIU those hostplatform settings do not propagate to the buildRustPackage builder. You can confirm yourself empirically by building a rust app with and without those parameters set and look at the output with elfx86exts.

I wanted to go down that route (for fun, I’m not expecting any practical advantages from doing this) and needed to add a patch to nixpkgs which exposes RUSTFLAGS as an overridable parameter for the builder. Then, it’s easy enough to set the RUSTFLAGS to instruction rustc to compile the binaries with march=native. Disclaimer: there might be an easier way to set RUSTFLAGS globally like this, but I couldn’t seem to find it from reading the code.

In terms of correctness: I’ve tested on my own config with no issues, but the nixpkgs CI fails and I wasn’t able to piece together why. If anyone has ideas or suggestions: I’d love feedback :slight_smile:

You might also want to do this for other programming languages. I’ve looked into it a bit for haskell and zig, see my config. Apologies in advance – it’s fairly messy. As a TLDR: Haskell’s builder supports overrides out of the box, but zig’s builder hardcodes the relevant flag so I added a small patch to make that overridable as well.

This is interesting, this contradicts what I read before. But a majority of software we use is c, c++ or compiled with c or c++ like python, so even though it’s probably good enough.