nix-language.md

Nix language basics

Before this, you'll want

  1. What Nix is
The Nix language10 min

You have almost certainly seen Nix code before you decided to learn any of it. Somebody’s configuration on GitHub, a snippet in a forum answer that happened to fix your problem, or a wall of curly braces and semicolons that clearly meant something to somebody.

So here is the one idea that makes all of it readable, and it is far smaller than that wall of punctuation seems to suggest: all Nix code you will ever work with describes one single value.

That value might be a number, a string or even an entire operating system. Either way, you write an expression and Nix works out what it comes to.

Everything below runs as you type. Change the code, then watch the value appear.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

The basic values

Nix gives you five basic types of value that stand on their own. Lists, attribute sets and functions all arrive later, and every one of them is just a way of holding these or putting them together.

number423.14-7Int, or float with a point.
string"nix""hello world""26.05"In quotes. Joined with +.
booleantruefalseYes or no. Nothing else.
nullnullDeliberately nothing.
path./flake.nix/etc/nixosNo quotes, and a slash in it.

Paths on this website belong to the course’s small virtual project. This page never reads files from your computer.

Numbers

Arithmetic is arithmetic. * and / go before + and -, just like in school.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Parentheses can be used to change the order.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Division is where int and float part ways. Two whole numbers divide into a whole number and the remainder is thrown away, so this is 3 rather than 3.5.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Put a point on either side, write 7.0 / 2, and the fraction survives.

Try it

Make this come out as 100.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Strings

Surprisingly, numbers are the part of Nix you will likely use the least. Strings on the other hand you will be knee-deep in forever, so let’s get comfortable with them.

They join together with +, exactly like you would hope.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.
Try it

Make this come out as "hello world".

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Lists

Single values are cool, but lists are where it’s at.

A list holds a bunch of values in square brackets. No commas. This trips up everybody coming from another language.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

++ is just like +, but for lists. It joins two of them into one.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.
Try it

Join a second list on, so you get [ 1 2 3 4 ].

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

What to keep

  • Nix describes one value, and everything is an expression.
  • Numbers and strings use familiar operators, and brackets change precedence.
  • Lists use spaces, not commas.
  • ++ joins two lists together into a new one.
  • The live result is your quickest feedback loop: change an expression and watch.

Next you will give values names and start building the shape used by every Nix configuration.

Useful links

NORMALCOURSE IN BETA