1# Syntax Summary {#sec-nix-syntax-summary}
2
3Below is a summary of the most important syntactic constructs in the Nix
4expression language. It's not complete. In particular, there are many
5other built-in functions. See the [Nix
6manual](https://nixos.org/nix/manual/#chap-writing-nix-expressions) for
7the rest.
8
9| Example | Description |
10|-----------------------------------------------|--------------------------------------------------------------------------------------------------------------------|
11| *Basic values* | |
12| `"Hello world"` | A string |
13| `"${pkgs.bash}/bin/sh"` | A string containing an expression (expands to `"/nix/store/hash-bash-version/bin/sh"`) |
14| `true`, `false` | Booleans |
15| `123` | An integer |
16| `./foo.png` | A path (relative to the containing Nix expression) |
17| *Compound values* | |
18| `{ x = 1; y = 2; }` | A set with attributes named `x` and `y` |
19| `{ foo.bar = 1; }` | A nested set, equivalent to `{ foo = { bar = 1; }; }` |
20| `rec { x = "foo"; y = x + "bar"; }` | A recursive set, equivalent to `{ x = "foo"; y = "foobar"; }` |
21| `[ "foo" "bar" ]` | A list with two elements |
22| *Operators* | |
23| `"foo" + "bar"` | String concatenation |
24| `1 + 2` | Integer addition |
25| `"foo" == "f" + "oo"` | Equality test (evaluates to `true`) |
26| `"foo" != "bar"` | Inequality test (evaluates to `true`) |
27| `!true` | Boolean negation |
28| `{ x = 1; y = 2; }.x` | Attribute selection (evaluates to `1`) |
29| `{ x = 1; y = 2; }.z or 3` | Attribute selection with default (evaluates to `3`) |
30| `{ x = 1; y = 2; } // { z = 3; }` | Merge two sets (attributes in the right-hand set taking precedence) |
31| *Control structures* | |
32| `if 1 + 1 == 2 then "yes!" else "no!"` | Conditional expression |
33| `assert 1 + 1 == 2; "yes!"` | Assertion check (evaluates to `"yes!"`). See [](#sec-assertions) for using assertions in modules |
34| `let x = "foo"; y = "bar"; in x + y` | Variable definition |
35| `with pkgs.lib; head [ 1 2 3 ]` | Add all attributes from the given set to the scope (evaluates to `1`) |
36| *Functions (lambdas)* | |
37| `x: x + 1` | A function that expects an integer and returns it increased by 1 |
38| `(x: x + 1) 100` | A function call (evaluates to 101) |
39| `let inc = x: x + 1; in inc (inc (inc 100))` | A function bound to a variable and subsequently called by name (evaluates to 103) |
40| `{ x, y }: x + y` | A function that expects a set with required attributes `x` and `y` and concatenates them |
41| `{ x, y ? "bar" }: x + y` | A function that expects a set with required attribute `x` and optional `y`, using `"bar"` as default value for `y` |
42| `{ x, y, ... }: x + y` | A function that expects a set with required attributes `x` and `y` and ignores any other attributes |
43| `{ x, y } @ args: x + y` | A function that expects a set with required attributes `x` and `y`, and binds the whole set to `args` |
44| *Built-in functions* | |
45| `import ./foo.nix` | Load and return Nix expression in given file |
46| `map (x: x + x) [ 1 2 3 ]` | Apply a function to every element of a list (evaluates to `[ 2 4 6 ]`) |