⟰ Some NixOS Notes
⟸ Some NixOS Notes
⟹ Modules

The Nix Expression Language

Nix Manual: Part IV. Writing Nix Expressions
NixOS Wiki: Nix Expression Language

Nix is a lazy, purely functional language which you’ll be using for packages and configuration in NixOS. It has:

A fine selection of types,

A lively cast of operators,

set.x        # attribute access
f x          # function application

-x           # numerical negation

#! attribute existence testing (true if `set` contains an attribute `x`)
set ? x

list ++ list # list concatenation

x * y / z    # multiplication and division
x + y - z    # addition and subtraction

!a           # logical negation

#! set merging (a set with both sets' attributes, if both sets have
#! an attribute with the same key then `set2` takes precedence)
set1 // set2

a<b<=c>=d>e  # comparison
a == b != c  # equality testing
a && b       # logical conjunction
a || b       # logical disjunction

#! logical implication (if `a` is true, `b` needs
#! to be true or else the result is false)
a -> b

A fair few builtins,

abort s # no return value. stop evaluation and print error string

baseNameOf s # part of `s` after its last '/' character

derivation inputs # a derivation. will be discussed later

dirOf s # part of `s` preceding `baseNameOf s`

fetchTarball url # path of unpacked .tar.(gz|xz|bz2) downloaded from `url`

import path # the expression at `path`

# whether `x` is null. this is deprecated because you can just write `x == null`
isNull x

map f list # list of each `f x` for each `x` in `list`

removeAttrs set list # `set` with each attribute in `list` removed

throw s # no return value. `abort s` but softer

toString x # `x` converted to a string

builtins # a set containing more builtins

The remaining built-in functions are kept in builtins to prevent clutter. The contents of that set are described in Nix Manual: 15.5. Built-in Functions.

As well as a few extra tricks.

# comments, as you've probably noticed, begin like this
/* multi-line comments, however,
   use these */

# bring a set's attributes into scope
s = { a = 1; };
x = with s; [ a ]; #: [ 1 ]

# define some temporary local variables to use in an expression
x = let
  a = 1;
  b = 2;
in a + b; #: 3

# allow a set to reference its own attributes
x = rec {
  a = 1;
  b = a + 1;
}; #: { a = 1; b = 2; }

# inherit some variables from the surrounding scope
x = rec {
  a = 1;
  b = {
    inherit a;
  }
}; #: { a = 1; b = { a = 1; }; }

# or from another set
x = rec {
  a = { b = 1; };
  inherit (a) b;
}; #: { a = { b = 1; }; b = 1; }

⟰ Some NixOS Notes
⟸ Some NixOS Notes
⟹ Modules