NiX Cheat Sheet

Table of Contents

1 nix-repl

1.1 Understand how a derivation will be build

Access the drvAttrs attribute, e.g.:

:l <nixpkgs>
:p python36Packages.matplotlib.drvAttrs

2 Remove a configure flag

2.1 Remove an element of list

Elements of a list can be removed with the lib.remove function, so for example to remove the link-time optimisation flag from gcc configure you can do:

:l <nixpkgs>

nix-repl> :load <nixpkgs>
Added 9387 variables.

nix-repl> :p gcc.cc.drvAttrs.configureFlags [
"--with-gmp-include=/nix/store/149za0vqf2ba3d943ysbm02ir3x272y7-gmp-6.1.2-dev/include"
"--with-gmp-lib=/nix/store/xczjkf2apaym9871hyhy46gs1gimw24d-gmp-6.1.2/lib"
"--with-mpfr-include=/nix/store/7hiiy1bxsr7q87cyhx9803wpzfdwn01v-mpfr-4.0.1-dev/include"
"--with-mpfr-lib=/nix/store/nk0abdf8a9qldxxm3vxycsz39bvbqsz3-mpfr-4.0.1/lib"
"--with-mpc=/nix/store/l1d2lykdqvvmxhnr1h292h65nmj4szsv-libmpc-1.1.0"
"--with-libelf=/nix/store/qjqh9qlmjy9bcz8562d5p9zv0m0awijh-libelf-0.8.13"
"--with-native-system-header-dir=/nix/store/akak0rxhbi4n87z3nx78ipv76frvj841-glibc-2.27-dev/include"
"--enable-lto" "--disable-libstdcxx-pch" "--without-included-gettext"
"--with-system-zlib" "--enable-static" "--enable-languages=c,c++"
"--disable-multilib" "--enable-plugin"
"--with-isl=/nix/store/h8mcynr6736anc3yq6qn1qs4mx87gclh-isl-0.17.1"
"--build=x86_64-unknown-linux-gnu" "--host=x86_64-unknown-linux-gnu" ]

nix-repl> :p lib.remove "--enable-lto" gcc.cc.drvAttrs.configureFlags
[
"--with-gmp-include=/nix/store/149za0vqf2ba3d943ysbm02ir3x272y7-gmp-6.1.2-dev/include"
"--with-gmp-lib=/nix/store/xczjkf2apaym9871hyhy46gs1gimw24d-gmp-6.1.2/lib"
"--with-mpfr-include=/nix/store/7hiiy1bxsr7q87cyhx9803wpzfdwn01v-mpfr-4.0.1-dev/include"
"--with-mpfr-lib=/nix/store/nk0abdf8a9qldxxm3vxycsz39bvbqsz3-mpfr-4.0.1/lib"
"--with-mpc=/nix/store/l1d2lykdqvvmxhnr1h292h65nmj4szsv-libmpc-1.1.0"
"--with-libelf=/nix/store/qjqh9qlmjy9bcz8562d5p9zv0m0awijh-libelf-0.8.13"
"--with-native-system-header-dir=/nix/store/akak0rxhbi4n87z3nx78ipv76frvj841-glibc-2.27-dev/include"
"--disable-libstdcxx-pch" "--without-included-gettext"
"--with-system-zlib" "--enable-static" "--enable-languages=c,c++"
"--disable-multilib" "--enable-plugin"
"--with-isl=/nix/store/h8mcynr6736anc3yq6qn1qs4mx87gclh-isl-0.17.1"
"--build=x86_64-unknown-linux-gnu" "--host=x86_64-unknown-linux-gnu" ]

2.2 Remove a configure flag by overriding

  • Use the overrideAttrs attribute of a derivation
  • Use lib.flatten to ensure flags are fully evaluated and simple list
  • Use lib.remove above to remove
nix-repl> newgcc=gcc.cc.overrideAttrs (oldAttrs : rec { 
     configureFlags =  lib.remove "--enable-lto"   (lib.flatten oldAttrs.configureFlags);
    })

nix-repl> newgcc
«derivation /nix/store/s2sarrlrgmwyvqps23zvb4l7571k9w95-gcc-7.3.0.drv»

nix-repl> newgcc.drvAttrs.configureFlags [
"--with-gmp-include=/nix/store/149za0vqf2ba3d943ysbm02ir3x272y7-gmp-6.1.2-dev/include"
"--with-gmp-lib=/nix/store/xczjkf2apaym9871hyhy46gs1gimw24d-gmp-6.1.2/lib"
"--with-mpfr-include=/nix/store/7hiiy1bxsr7q87cyhx9803wpzfdwn01v-mpfr-4.0.1-dev/include"
"--with-mpfr-lib=/nix/store/nk0abdf8a9qldxxm3vxycsz39bvbqsz3-mpfr-4.0.1/lib"
"--with-mpc=/nix/store/l1d2lykdqvvmxhnr1h292h65nmj4szsv-libmpc-1.1.0"
"--with-libelf=/nix/store/qjqh9qlmjy9bcz8562d5p9zv0m0awijh-libelf-0.8.13"
"--with-native-system-header-dir=/nix/store/akak0rxhbi4n87z3nx78ipv76frvj841-glibc-2.27-dev/include"
"--disable-libstdcxx-pch" "--without-included-gettext"
"--with-system-zlib" "--enable-static" "--enable-languages=c,c++"
"--disable-multilib" "--enable-plugin"
"--with-isl=/nix/store/h8mcynr6736anc3yq6qn1qs4mx87gclh-isl-0.17.1"
"--build=x86_64-unknown-linux-gnu" "--host=x86_64-unknown-linux-gnu" ]

So we have a new version of gcc .

3 Python packages

3.1 Override a Python package

If you need to override a Python package so that all other packages which depend on it use the updated version, add the following to nixpkgs/config.nix (this example builds with nixpkgs 17.03)

{pkgs} : {

packageOverrides = pkgs: rec {

   python36Packages = pkgs.python36Packages.override (oldAttrs:
   { overrides = self: super: {

   whoosh = super.whoosh.override {
     doCheck=false; doInstallCheck = false;
   };
   pyflakes = super.pyflakes.override {
     doCheck=false; doInstallCheck = false;
   }; } ; }  ) ; };
}

Note however this works for in-line Python derivations, not ones which are callPackage. In the case of a callPackage the following can be used to override (this example builds with nixpkgs 17.09):

{pkgs} : {

packageOverrides = pkgs : rec {
   python36Packages = pkgs.python36Packages.override (oldAttrs: { overrides = self: super: {
   whoosh = super.whoosh.overrideAttrs ( z : rec { doCheck=false; doInstallCheck = false;});
   pyflakes = super.pyflakes.overrideAttrs( z : rec{ doCheck=false; doInstallCheck = false; } );   
   } ; }  ) ; };
}

3.2 Failure tests?

If tests fail override the doInstallCheck parameter . Example:

x = lib.overrideDerivation python36Packages.whoosh  ( z : {doInstallCheck=false;}) 
:b x
  • Note that setting doInstallCheck within a buildPythonPackage parameter set will not resolve the problem as this attribute is reset by the buildPythonPackage.
  • Note that a dynamic override is needed to ensure other packages which depend on this pick up the new version

4 Optimisation

4.1 Build for native processor

Add following to nixpkgs/config.nix

stdenv.userHook = '' NIX_CFLAGS_COMPILE+=" -march=native" '';

5 Links to other cheat sheets

Author: Bojan Nikolic

Created: 2018-12-14 Fri 13:11

Validate