Overview of the Nix Language

From NixOS Wiki
Jump to: navigation, search

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.

Nix Reference Manual

This pedia article covers the syntax, semantics, typing, compilation, tooling and libraries of the Nix Language.

Note: Check the Nix Reference Manual on the Nix Language for up-to-date documentation and Nix language basics for a gentle introduction.

Language Paradigms


Not all expressions in nixpkgs will be evaluated and instantiated as nix performs evaluation only when needed for a finished output. In the following example abort will never be triggered as the variable it belongs to is unused:

  a = abort "will never happen";
  b = "hello";
  c = "world";
in b + c


Functional Programming is a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.

See also: [1]


A pure function is a function where the return value is only determined by its input values, without observable side effects. In Nix, all build operations try to be as pure as possible to achieve reproducible builds. This means that wherever you build the packages as few side effects as possible should have an impact onto the build.

Language Features

This section describes the main language features of the nix expression language.


When Nix tutorials talk about Nix Expressions they typically mean the definition of a function with multiple inputs which result in a derivation. However a Nix expression can be everything, from a simple string, to a function to a set of expressions.


The nix language provides a number of basic types:

Type Description Example
Strings Strings either start with double quotes or double single quotes. They also support antiquotation (templating). Leading spaces are stripped with double single quotes. "Say ${pkgs.hello.name}"

Multiline String:

  first line
  second line
Integers A whole number without fractional component. 5
Floating-point numbers Decimal numbers. Precision is limited. 1.2
Path Relative paths will become absolute when evaluated, paths must contain a slash. <nixpkgs/pkgs> is also possible and will resolve to the folder incl. subfolders in your NIX_PATH
> /abs/path/to/hello/world
> /path/to/your/nixpkgs/lib
URI Uniform Resource Identifiers http://example.org/foo.tar.bz2
Boolean true, false
Null A representation of nothing. null
Lists Items are separated by space, not comma. Each item can be a value of any other type
[ 1 ./example.bin { hello="world"; }]
Sets Associative data structures. In other languages called dicts(Python),objects(JavaScript) hashes(Ruby) or maps(Java). Essentially a list of key-value pairs
{ key1="value1";  key2="value2"; }
Access values through dot-notation:
{ hello="world"; }.hello
> "world"
Functions See below. argument: function-body

A detailed description of all types can be found in The Nix manual.


Functions are all unnamed (=lambda) functions with the following notation: argument: nixExpression, e.g. x: x*x.

If you want to give that function a name, you have to assign it to a name, e.g. square = x: x*x. So, f(x) = x*x in math is f = x: x*x in Nix.

If you want to use that function and apply it to a value like f(3), you leave out the parentheses and add a space. So, f(3) in math, is f 3 in Nix.

If you want multiple arguments, you can add arguments like this: arg1: arg2: nixExpression, e.g. f = x: y: x*y. Applying that function to multiple values is easy: f(3,4) in math, is f 3 4 in Nix. If you apply one argument f 3only, a partial function y: 3*yis returned.


In nix it happens that sets are given as arguments to functions. Say that we declare a function which returns the concatenation of attributes a and b of a set like:

concat_a_and_b =  set: set.a + set.b 
 concat_a_and_b { a="hello"; b="world"; }

It is then possible to destructure the argument set and pick out the attributes that we are interested in, in the function declaration, resulting in a tidier function:

 concat_a_and_b = {a, b}: a + b
concat_a_and_b { a="hello "; b="world"; }
"hello world"

Default argument

It is also possible to assign default values to be used in a function if the caller omits one, but only as part of a set.

add_a_b = { a ? 1, b ? 2 }: a + b
add_a_b {}
add_a_b {a=5;}

Accepting unexpected attributes in argument set

If you want your function to still run without error if the user provides a set with more attributes than you expected it to have you can use the ellipses.

add_a_b = { a, b }: a + b
add_a_b { a=5; b=2; c=10; }
error: anonymous function at (string):1:2 called with unexpected argument 'c', at (string):1:1
add_a_b = { a, b, ... }: a + b
add_a_b { a=5; b=2; c=10; }

You can also store away the arguments in a name of your chosing using the @ pattern.

add_a_b = args@{ a, b, ... }: a + b + args.c
add_a_b { a=5; b=2; c=10; }


Lower precedence means a stronger binding; i.e. this list is sorted from strongest to weakest binding, and in the case of equal precedence between two operators, the associativity decides the binding.

Precedence Abbreviation Example Assoc Description
e . attrpath
none Select attribute denoted by the attribute path attrpath from set e. (An attribute path is a dot-separated list of attribute names.) If the attribute doesn’t exist abort evaluation.
e . attrpath or defaultVal
none If the attribute attrpath doesn’t exist, return defaultVal. NOTE: This is a ternary operator, the "or" keyword is only a keyword when used as a part of this ternary operator.
e1 e2
left Call function e1 with argument e2.
none Numeric negation.
e ? attrpath
none Test whether set e contains the attribute denoted by attrpath; return true or false.
e1 ++ e2
right List concatenation.
e1 * e2
left Numeric multiplication.
e1 / e2
left Numeric division.
e1 + e2
left Numeric addition, or string concatenation.
e1 - e2
left Numeric subtraction.
left Boolean negation.
e1 // e2
right Return a set consisting of the attributes in e1 and e2 (with the latter taking precedence over the former in case of equally named attributes).
10 LT
e1 < e2
left Less than.
10 LTE
e1 <= e2
left Less than or equal.
10 GT
e1 > e2
left Greater than.
10 GTE
e1 >= e2
left Greater than or equal.
11 EQ
e1 == e2
none Equality.
11 NEQ
e1 != e2
none Inequality.
12 AND
e1 && e2
left Logical AND.
13 OR
e1 || e2
left Logical OR.
e1 -> e2
none Logical implication (equivalent to !e1 || e2).

Source: Gist of joepie91


import loads, parses and imports the nix expression stored in path. This keyword is essentially a builtin of nix but not a part of the language itself.


  x = import <nixpkgs> {};
  y = trace x.pkgs.hello.name x;

Notable constructs

Nix looks a lot like JSON with functions but also provides a number of very specialized constructs which can help you build clean and easy to read expressions. In this sub-chapter the most notable constructs will be shown by example:

with statement

The with statement introduces an attrset's value contents into the lexical scope of the expression which follows. This means that it brings all keys within that set into scope in that expression. So, you don't need to use the dot notation. If a key is already in the outer scope, it is only shadowed when it was introduced by a with statement.


  myattrset = { a = 1; b = 2; };
  with myattrset; "In this string we have access to ${toString a} and ${toString b}"


"In this string we have access to 1 and 2"

Note that (perhaps surprisingly) with does not shadow variables from outer scope which were bound in a let statement, let inherit ... statement, recursive attribute set, or a lambda abstraction. For example:

  a = 333;
  with { a = 1; b = 2; }; "In this string we have access to ${toString a} and ${toString b}"


"In this string we have access to 333 and 2"

This is because a was already defined in the scope outside the use of with, and with does not override it. The outer value takes precedence. (It is suspected by the author of this wiki section that the reason for this non-shadowing logic is lexical code stability: In the common usages of with as shown below, the contents of attrsets given to the statement are often large, community-maintained, and frequently updated. If e.g. let myValue = ...; with lib; doSomethingWith myValue shadowed the outer myValue bindings, the people maintaining `lib` could accidentally break this code by adding lib.myValue.)

Note that variables are shadowed when (and only when) they were introduced by a with statement. For example:

nix-repl> with { x = "outer"; }; with { x = "inner"; }; x

Common usages are:

On top of expressions:

Look at the following lib set:

lib = { 
    attrsOf = ...;
    listOf = ...;
    str = ...;

You will see the with statement a lot at the beginning of expression definition. Most of the time it is used to load the lib functions into the namespace for quick access.

{lib, ... }:

with lib;
  options = {
    networking.hosts = mkOption {
      type = with types; attrsOf ( listOf str);
      default = {};

instead of:

{lib, ... }:
  options = {
    networking.hosts = lib.mkOption {
      type = lib.types.attrsOf ( lib.types.listOf lib.types.str);
      default = {};

In package input definitions:

  buildInputs = with pkgs; [ curl php coreutils procps ffmpeg ];

Instead of :

  buildInputs = [ pkgs.curl pkgs.php pkgs.coreutils pkgs.procps pkgs.ffmpeg ];

In the package meta tag:

{lib, ...}:
  meta = with lib; {
    license = with licenses; [ lgp3 gpl3 ];
    maintainers = with maintainers; [ adisbladis lassulus ];

Instead of :

{lib, ...}:
  meta = {
    license = [ lib.licenses.lgp3 lib.licenses.gpl3 ];
    maintainers = [ lib.maintainers.adisbladis lib.maintainers.lassulus ];

In a default.nix of an external package:

with import <nixpkgs> {};
stdenv.mkDerivation rec {
    name = "mytool-env";
    src = ./.;
    buildInputs = with pkgs;[

    shellHook =''
      export HISTFILE=$PWD/histfile
    '' ;

let ... in statement

With let you can define local variables which can also reference to self without the need of the rec construct. This feature is used inside expressions to prepare variables which become part of an output. The usage of let is comparable to the Haskell let expression

  a = 1;
  b = 2;
in  a + b
=> 3

inherit statement

The inherit expression can be used to copy variables from the surrounding lexical scope. A typical use case is to declare the version or name of a derivation in the expression and reuse this parameter in the function to fetch the source.

This is a typical python package derivation as the fetchPypi function also requires pname and version as input:

buildPythonPackage rec {
  pname = "hello";
  version = "1.0";
  src = fetchPypi {
    inherit pname version;
   sha256 = "01ba..0";

rec statement

The rec expression turns a basic set into a set where self-referencing is possible. This can be used when the let expression would create too much clutter. It is often seen in package derivation descriptions.

Sample usage:

rec {
  x = y - 100;
  y = 123;
=> 23

See also