Build flags
Building a package for a specific CPU
By default, packages built for, say, x86_64 do not take advantage of the feature of more recent cpus so that the executables you compile also work on older cpus of the same architecture. This is essential because of the binary cache feature of nix: if a package is compiled on hydra by a recent CPU, older systems using hydra may download software that they can't run.
However, you can build some package or even all your system to take advantage of the specific model of your cpu. Note that you will not be able to take advantage of the binary cache and thus build everything locally from scratch. The first step is to determine the -march
and -mtune
arguments that you want to pass to gcc. In the following we want to target a skylake cpu so -march=skylake -mtune=skylake
.
Building a single package
You need to be a trusted user to override the local system feature.
optimised_openssl.nix
let
pkgs = import <nixpkgs> {
localSystem = {
gcc.arch = "skylake";
gcc.tune = "skylake";
system = "x86_64-linux";
};
};
in
pkgs.openssl
Then build the file:
nix-build optimised_openssl.nix --option system-features gccarch-skylake
Adapting a derivation to specific plaforms
The technique above should pass the correct flags to gcc so that it uses the processor to its fullest. However, some build systems or configure scripts want to know whether to enable some processor-specific instructions, for example sse. One way to do so is to inspect the stdenv.hostPlatform.*Support
predicates: here is an example from g2o:
cmakeFlags = [ "-DDISABLE_SSE3=${ if stdenv.hostPlatform.sse3Support then "OFF" else "ON"}" ]
Available flags are defined in lib/systems/architectures.nix.
Building the whole system on NixOS
/etc/nixos/configuration.nix
{ config, pkgs, lib, ... }:
{
nixpkgs.hostPlatform = {
gcc.arch = "skylake";
gcc.tune = "skylake";
system = "x86_64-linux";
};
}
Building an impure package with -march=native
To build an openssl specially tailored to the local CPU, build
let
pkgs = import <nixpkgs> {
overlays = [
(self: super: {
stdenv = super.impureUseNativeOptimizations super.stdenv;
})
];
};
in
pkgs.openssl