If you want to see more Nix documentation like this in the future, or have any requests or suggestions, then please fill out this anonymous Google form.

mkDerivation

stdenv.mkDerivation is the main function used to build packages with the standard environment.

Inputs

{
  # core attributes
  name:            string,
  pname:           string?,
  version:         string?,
  src:             path,

  # building
  buildInputs:     list[derivation]?,
  buildPhase:      string?,
  installPhase:    string?,
  builder:         path?,

  # `nix-shell`
  shellHook:       string?
}

name

string, required

The derivation name.

Can be omitted if pname and version are set, in which case it is automatically set to ${pname}-${version}.

Examples:

stdenv.mkDerivation {
  name = "libfoo-1.2.3";
  src = ./src;
}
stdenv.mkDerivation {
  pname = "libfoo";
  version = "1.2.3";
  src = ./src;
}

pname

string, optional

The package name.


version

string, optional

The package version.


src

path, required

The package source directory.

Examples:

stdenv.mkDerivation {
  name = "libfoo-1.2.3";
  src = ./src;
}
stdenv.mkDerivation {
  name = "libfoo-1.2.3";
  src = fetchurl {
    url = http://example.org/libfoo-1.2.3.tar.bz2;
    sha256 = "0x2g1jqygyr5wiwg4ma1nd7w4ydpy82z9gkcv8vh2v8dn3y58v5m";
  };
}

buildInputs

list[derivation], optional

The package's dependencies.

This attribute ensures that the outputs of the dependencies (e.g. bin, includes) are in scope during the package build.

Examples:

stdenv.mkDerivation {
  name = "libfoo";
  src = ./src;
  buildInputs = [libbar perl ncurses];
}

buildPhase

string, optional

A shell script to run during the build phase. If omitted, the build phase will run make.

Examples:

stdenv.mkDerivation {
  name = "libfoo";
  src = ./src;
  buildPhase = ''
    gcc foo.c -o foo
  '';
}

installPhase

string, optional

A shell script to run during the install phase. If omitted, the install phase will run make install.

Examples:

stdenv.mkDerivation {
  name = "libfoo";
  src = ./src;
  buildPhase = ''
    gcc foo.c -o foo
  '';
  installPhase ''
    mkdir -p $out/bin
    cp foo $out/bin
  '';
}

builder

path, optional

The path to a shell script that describes how to build the package. If omitted, the build runs using stdenv's generic builder.

Examples:

# builder.sh
source $stdenv/setup

buildPhase() {
  echo "... this is my custom build phase ..."
  gcc foo.c -o foo
}

installPhase() {
  mkdir -p $out/bin
  cp foo $out/bin
}

genericBuild
stdenv.mkDerivation {
  name = "libfoo";
  src = ./src;
  builder = ./builder.sh;
}

shellHook

string, optional

A shell script to run after entering a nix-shell.

If shellHook is defined, it will be run in the `nix-shell` after $stdenv/setup has been sourced by the package's builder.

Note: This hook is not executed by regular Nix builds.

Examples:

stdenv.mkDerivation {
  name = "my-package";
  src = ./src;
  shellHook = ''
    echo "Hello shell!"
  '';
}