YOUR JOURNEY
05 / 18
01NIX FUNDAMENTALS 7 MIN READ
LESSON 05 / READ THE NIX LANGUAGE

Small language.
Big descriptions.

You only need a few building blocks to start reading Nix.

Nix expressions evaluate to values. Attribute sets, lists, bindings, and functions form the vocabulary you’ll see in every configuration.

Explore the lesson
~$ Build your understanding, one idea at a time.
THE NIX MODEL01 / 03
Drag to rotate

Give values local names using let … in.

Local bindings
05.1VALUES AND EXPRESSIONS

Read it as data with functions.

An attribute set uses braces and named values ending in semicolons. A list uses square brackets and spaces. Strings use double quotes. A let expression introduces local bindings; the expression after in is its result.

A function is written argument: expression. Applying it uses a space, so double 21 is a function call. Nix is lazily evaluated: values are generally computed when something needs them.

example.nix
let
  double = x: x * 2;
  tools = [ "git" "hello" ];
in {
  answer = double 21;
  packages = tools;
  enabled = true;
}
Next section: A configuration file is an expression.
05.2TRY EVALUATION

A configuration file is an expression.

Save the previous example as example.nix. The command below evaluates it strictly and prints JSON. It does not build a package.

A function such as { pkgs, ... }: { … } takes an attribute set and returns another value. The ellipsis allows additional arguments. You will see this shape in NixOS modules.

terminal
nix-instantiate --eval --strict --json example.nix
# {"answer":42,"enabled":true,"packages":["git","hello"]}
TAKE THIS WITH YOU

Nix evaluates expressions. Shell commands appear only inside strings that a builder or hook later runs.

Go a little deeperNix language basics
UP NEXT

From recipe to package

A derivation describes a build before that build happens.

Next lesson