Components of a Nix derivation
The function derivation describes Nix derivationsᛦ, taking a set whose attributes describe all the inputs of a build. mkDerivation wraps this function for convenience by setting system and using bash as the builder to which the builder attribute is passed as an argument.
A derivation comprises
- a
systemattribute whose value is a platform identifier string likex86_64-darwincorresponding to the system on which the build takes place - a
nameattribute whose value is the name of the package - a
builderattribute whose value is the program to be run to build the package (either a derivation or a path) - additional attributes for the builder, passed as environment variables:
- String and numbers are passed in unchanged
- Paths are copied to the Nix storeᛦ and their store paths are placed in the variables
- Derivations are built and their store paths are placed in the variables
- Lists of the previous types are joined with spaces
truebecomes"1", andfalseandnullbecome""
- an optional list
argsof command line arguments to be passed to the builder - an optional list
outputsspecifying the outputs of a derivation
Outputs
Specifying outputs = [ "lib" "headers" "doc" ] will produce three store paths, such that the builder can specify something like
./configure --libdir=$lib/lib --includedir=$headers/include --docdir=$doc/share/doc
Suppose we did this for Hello, Nix!ᛦ Then we could specify build inputs in another derivation like
buildInputs = [ hello.lib hello.headers ]
or the equivalent
buildInputs = [ hello hello.headers ]
since the first element of outputs determines the default output.
Separating outputs this way allows us to download and garbage collect paths independently.