Derivations
Build instructions for Nix packages expressed using the Nix language
We recommend starting with the Nix quick start and consulting concept docs primarily for clarification. Feel free to click x to the right to disable this notification on all concept docs.
A derivation is an instruction that Nix uses to realise a Nix package.
They’re created using a special derivation
function in the Nix language.
They can depend on any number of other derivations and produce one or more final outputs.
A derivation and all of the dependencies required to build it—direct dependencies, and all dependencies of those dependencies, etc—is called a closure.
Derivation outputs
The most common outputs:
out
, the most generic onelib
, for library outputsdev
, general development resources such as header filesman
, for manual pages
A derivation and all of the dependencies required to build it—direct dependencies, and all dependencies of those dependencies, etc—is called a package closure.
You may find it helpful to think of a derivation as the plan or blueprint for a Nix package.
Derivations in the Nix language
In the Nix language, derivations are created using the derivation
function.
Here’s the type signature for derivation
expressed as a Haskell-ish signature:1
Here’s an example derivation:
And that’s it!
The derivation
function takes only these arguments.
But derivations can be far less straightforward, because the scripting logic that you pass via args
can be arbitrarily complex.
String interpolation
If Nix sees this string…
The string output of a derivation is always a Nix store path
Derivation outputs
Realisation
Special variables
There are two special variables to be aware of when writing derivations:
$out
represents the root of the directory structure for build output.$src
represents the build sources.
The standard environment
Most derivations in the Nix ecosystem are based on the mkDerivation
function from the standard environment.
While you can create derivations using the raw derivation
function, it’s far more common to use a wrapper function around it.
Perhaps the most commonly used wrapper function is stdenv.mkDerivation
.
Arguments:
- The
name
of the package. If you specifypname
andversion
instead, thename
ends up${pname}-${version}
.
Other derivation functions
Outside of stdenv.mkDerivation
, there are many custom derivation wrappers for specific languages, frameworks, and more (and many of those actually wrap stdenv.mkDerivation
).
Some examples:
buildGoModule
for GobuildRustPackage
for RustbuildPythonApplication
for Python
You’re likely to encounter many more in the Nix ecosystem.
And you’re always free to create your own derivation functions and even wrap helper functions like buildPythonApplication
.