Build flags: Difference between revisions

From NixOS Wiki
imported>Appetrosyan
Rewrote to use the file template
imported>Appetrosyan
Moved to templates and fixed rendering.
Line 9: Line 9:
{{file|optimised_openssl.nix|nix|<nowiki>
{{file|optimised_openssl.nix|nix|<nowiki>
let
let
   pkgs = import <nixpkgs> {
   pkgs = </nowiki>import <nixpkgs> {<nowiki>
     localSystem = {
     localSystem = {
       gcc.arch = "skylake";
       gcc.arch = "skylake";
       gcc.tune = "skylake";
       gcc.tune = "skylake";
       system = "x86_64-linux";
       system = "x86_64-linux";
     };
     </nowiki>};
   };
   };
in
in
pkgs.openssl
  pkgs.openssl
</nowiki>}}
}}


Then build the file:
Then build the file:

Revision as of 08:23, 3 June 2021

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

Building the whole system on NixOS

/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