Issue: I’ve been trying to recompile Nix packages locally from source with various CPU optimizations, but have been running into a variety of errors when doing so.
Goal: All I care about is getting packages compiled from source with my chosen compiler flags. I don’t care about how I do this (or even what compiler I use, though I understand Nix uses GCC). So if I’m going about this all wrong please let me know.
What I have tried: In short, I’ve tried (and failed) to do this via (details later):
- setting
gcc.arch,gcc.tune, andsysteminnixpkgs.hostPlatform - Using overlays for specific packages in
~/.config/nixpkgs/overlays/*.nix - Using
nixpkgs.overlaysglobally in/etc/nixos/configuration.nix
Details on these methods:
I attempted method 1 as so. I put the following code in my configuration.nix
{ config, lib, pkgs, ... }:
{
# ...
nix.settings.system-features = [
"gccarch-skylake" "benchmark" "big-parallel" "kvm" "nixos-test"
];
nixpkgs.hostPlatform = {
gcc.arch = "skylake";
gcc.tune = "skylake";
system = "x86_64-linux"; # uname --machine to get this
};
# ...
}
I then ran sudo nixos-rebuild switch. This fails with the following error:
... (there is a huge chain of these)
error: Cannot build '/nix/store/17afhqczma186iyd66543bqzvb8hma8p-nixos-rebuild-ng-26.05.drv'.
Reason: 1 dependency failed.
Output paths:
/nix/store/h713m87kribn15anyxxzkfn1zxnwahzb-nixos-rebuild-ng-26.05-dist
/nix/store/mprzr6wc0917q6q0gqw8zrh3310ll7v8-nixos-rebuild-ng-26.05
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.nixos-rebuild --no-out-link' returned non-zero exit status 100.
I attempted number 2 as so – I put the following code in ~/.config/nixpkgs/overlays/wget.nix`:
self: super: {
optimizeWithFlags = pkg: flags:
pkg.overrideAttrs (old: {
NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags;
});
optimizeForHost = pkg:
self.optimizeWithFlags pkg [ "-O3" "-march=skylake" ];
wget = self.optimizeForHost super.wget;
}
I than ran `nix-build '<nixpkgs>' -A pkgs.wget. This seems to run, and outputs /nix/store/77b7mj6y6l6616w9cfq0z43dnisvlimb-wget-1.25.0. However, I am then unable to run wgetfrom the command line (bash: wget: command not found), and I cannot serve it from a binary cache in my nix/store/. I’m unsure why.
I attempted number 3 as so: I put the following code in my configuration.nix
{ config, lib, pkgs, ... }:
{
# ...
environment.systemPackages = with pkgs; [
# ...
wget
];
# ...
nixpkgs.overlays = [
(final: prev: {
wget = prev.stdenvAdapters.addAttrsToDerivation {
NIX_CFLAGS_COMPILE = "";
NIX_LDFLAGS = "";
} prev.wget;
})
];
# ...
}
I then ran sudo nixos-rebuild switch. This results in the following error:
building the system configuration...
error:
… while calling the 'head' builtin
at /nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/lib/attrsets.nix:1717:13:
1716| if length values == 1 || pred here (elemAt values 1) (head values) then
1717| head values
| ^
1718| else
… while evaluating the attribute 'value'
at /nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/lib/modules.nix:1167:7:
1166| // {
1167| value = addErrorContext "while evaluating the option `${showOption loc}':" value;
| ^
1168| inherit (res.defsFinal') highestPrio;
… while evaluating the option `system.build.toplevel':
… while evaluating definitions from `/nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/nixos/modules/system/activation/top-level.nix':
… while evaluating the option `system.systemBuilderArgs':
… while evaluating definitions from `/nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/nixos/modules/system/activation/top-level.nix':
… while evaluating the option `environment.sessionVariables':
… while evaluating definitions from `/nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/nixos/modules/services/display-managers/default.nix':
… while evaluating the option `environment.systemPackages':
… while evaluating definitions from `/etc/nixos/configuration.nix':
(stack trace truncated; use '--show-trace' to show the full, detailed trace)
error: function 'anonymous lambda' called with unexpected argument 'mkDerivationFromStdenv'
at /nix/store/1z10c48rd5gb80yziqlva2hagvwqa6k0-nixos-26.05.12297.cd4ea8d57de1/nixos/pkgs/by-name/wg/wget/package.nix:1:1:
1| {
| ^
2| lib,
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.toplevel --no-out-link' returned non-zero exit status 1.
These the various references and documentation I’ve been through that motivated the above:
- Build flags - Official NixOS Wiki (for methods 1 and 3)
- How to recompile a package with flags? - #7 by danbst (for method 2)
- nixLTO/package-flags.nix at 51f7d86338562249a47e8baf449774aa7ceeba36 · WillPower3309/nixLTO · GitHub (for method 3)
A couple other solutions out there I think are relevant, but do not fully understand how to apply:
- Build failure: x86_64-unknown-linux-gnu-nolibc-gcc with `hostPlatform` and `buildPlatform` gcc arch set · Issue #291271 · NixOS/nixpkgs · GitHub
- Setting NIX_CFLAGS_COMPILE and NIX_CFLAGS_LINK for a subset of packages - #2 by umutzd
Has anyone got this (or something similar) to work? Any help here would be greatly appreciated!