Build flags

From NixOS Wiki
Revision as of 20:22, 23 May 2021 by imported>Symphorien (Created page with "=== 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 trested 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

Building the whole system on NixOS

In /etc/nixos/configuration/nix:

{ config, pkgs, lib, ... }:
{
    nixpkgs.localSystem = {
      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