Want to see more Nix documentation like this in the future, or have requests/suggestions? Fill out this anonymous Google form or submit an issue on GitHub.

nix-docs/mkDerivation

mkDerivation

function

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

}

Output

derivation

Reference

Inputs

Core Attributes

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";
  };
}

Building

buildInputs

list[derivation], optional

The package's build-time dependencies.

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

Example

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.

Example

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.

Example

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

path, optional

A path to a shell script that describes how to build the package.

If omitted, the build runs using stdenv's generic builder.

Example

# 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;
}

Nix shell

shellHook

string, optional

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

Example

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