Nix Language
The Nix language is designed for conveniently creating and composing derivations – precise descriptions of how contents of existing files are used to derive new files.
Tip
These pages are written as a reference. If you are learning Nix, nix.dev has a good introduction to the Nix language.
The language is:
-
domain-specific
It comes with built-in functions to integrate with the Nix store, which manages files and performs the derivations declared in the Nix language.
-
declarative
There is no notion of executing sequential steps. Dependencies between operations are established only through data.
-
pure
Values cannot change during computation. Functions always produce the same output if their input does not change.
-
functional
Functions are like any other value. Functions can be assigned to names, taken as arguments, or returned by functions.
-
lazy
Values are only computed when they are needed.
-
dynamically typed
Type errors are only detected when expressions are evaluated.
Overview
This is an incomplete overview of language features, by example.
Example | Description |
---|---|
Basic values |
|
|
A string |
|
A multi-line string. Strips common prefixed whitespace. Evaluates to |
|
String interpolation (expands to |
|
Booleans |
|
Null value |
|
An integer |
|
A floating point number |
|
An absolute path |
|
A path relative to the file containing this Nix expression |
|
A home path. Evaluates to the |
|
Search path for Nix files. Value determined by |
Compound values |
|
|
A set with attributes named |
|
A nested set, equivalent to |
|
A recursive set, equivalent to |
|
Lists with three elements. |
Operators |
|
|
String concatenation |
|
Integer addition |
|
Equality test (evaluates to |
|
Inequality test (evaluates to |
|
Boolean negation |
|
Attribute selection (evaluates to |
|
Attribute selection with default (evaluates to |
|
Merge two sets (attributes in the right-hand set taking precedence) |
Control structures |
|
|
Conditional expression |
|
Assertion check (evaluates to |
|
Variable definition |
|
Add all attributes from the given set to the scope (evaluates to |
|
Adds the variables to the current scope (attribute set or |
|
Adds the attributes, from the attribute set in parentheses, to the current scope (attribute set or |
Functions (lambdas) |
|
|
A function that expects an integer and returns it increased by 1 |
|
Curried function, equivalent to |
|
A function call (evaluates to 101) |
|
A function bound to a variable and subsequently called by name (evaluates to 103) |
|
A function that expects a set with required attributes |
|
A function that expects a set with required attribute |
|
A function that expects a set with required attributes |
|
A function that expects a set with required attributes |
Built-in functions |
|
|
Load and return Nix expression in given file |
|
Apply a function to every element of a list (evaluates to |