Build flags: Difference between revisions

From NixOS Wiki
imported>Srid
What are 'flags'?
imported>Eopb
m Fix path to configuration file
Line 36: Line 36:


==== Building the whole system on NixOS ====
==== Building the whole system on NixOS ====
{{file|/etc/nixos/configuration/nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{ config, pkgs, lib, ... }:
{ config, pkgs, lib, ... }:
{
{

Revision as of 19:51, 12 July 2023

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