1<section xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version="5.0"
5 xml:id="sec-nix-syntax-summary">
6
7<title>Syntax Summary</title>
8
9<para>Below is a summary of the most important syntactic constructs in
10the Nix expression language. It’s not complete. In particular, there
11are many other built-in functions. See the <link
12xlink:href="http://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix
13manual</link> for the rest.</para>
14
15<informaltable frame='none'>
16 <tgroup cols='2'>
17 <colspec colname='c1' rowsep='1' colsep='1' />
18 <colspec colname='c2' rowsep='1' />
19 <thead>
20 <row>
21 <entry>Example</entry>
22 <entry>Description</entry>
23 </row>
24 </thead>
25 <tbody>
26
27 <row>
28 <entry namest="c1" nameend="c2"><emphasis>Basic values</emphasis></entry>
29 </row>
30 <row>
31 <entry><literal>"Hello world"</literal></entry>
32 <entry>A string</entry>
33 </row>
34 <row>
35 <entry><literal>"${pkgs.bash}/bin/sh"</literal></entry>
36 <entry>A string containing an expression (expands to <literal>"/nix/store/<replaceable>hash</replaceable>-bash-<replaceable>version</replaceable>/bin/sh"</literal>)</entry>
37 </row>
38 <row>
39 <entry><literal>true</literal>, <literal>false</literal></entry>
40 <entry>Booleans</entry>
41 </row>
42 <row>
43 <entry><literal>123</literal></entry>
44 <entry>An integer</entry>
45 </row>
46 <row>
47 <entry><literal>./foo.png</literal></entry>
48 <entry>A path (relative to the containing Nix expression)</entry>
49 </row>
50
51 <row>
52 <entry namest="c1" nameend="c2"><emphasis>Compound values</emphasis></entry>
53 </row>
54 <row>
55 <entry><literal>{ x = 1; y = 2; }</literal></entry>
56 <entry>An set with attributes names <literal>x</literal> and <literal>y</literal></entry>
57 </row>
58 <row>
59 <entry><literal>{ foo.bar = 1; }</literal></entry>
60 <entry>A nested set, equivalent to <literal>{ foo = { bar = 1; }; }</literal></entry>
61 </row>
62 <row>
63 <entry><literal>rec { x = "foo"; y = x + "bar"; }</literal></entry>
64 <entry>A recursive set, equivalent to <literal>{ x = "foo"; y = "foobar"; }</literal></entry>
65 </row>
66 <row>
67 <entry><literal>[ "foo" "bar" ]</literal></entry>
68 <entry>A list with two elements</entry>
69 </row>
70
71 <row>
72 <entry namest="c1" nameend="c2"><emphasis>Operators</emphasis></entry>
73 </row>
74 <row>
75 <entry><literal>"foo" + "bar"</literal></entry>
76 <entry>String concatenation</entry>
77 </row>
78 <row>
79 <entry><literal>1 + 2</literal></entry>
80 <entry>Integer addition</entry>
81 </row>
82 <row>
83 <entry><literal>"foo" == "f" + "oo"</literal></entry>
84 <entry>Equality test (evaluates to <literal>true</literal>)</entry>
85 </row>
86 <row>
87 <entry><literal>"foo" != "bar"</literal></entry>
88 <entry>Inequality test (evaluates to <literal>true</literal>)</entry>
89 </row>
90 <row>
91 <entry><literal>!true</literal></entry>
92 <entry>Boolean negation</entry>
93 </row>
94 <row>
95 <entry><literal>{ x = 1; y = 2; }.x</literal></entry>
96 <entry>Attribute selection (evaluates to <literal>1</literal>)</entry>
97 </row>
98 <row>
99 <entry><literal>{ x = 1; y = 2; }.z or 3</literal></entry>
100 <entry>Attribute selection with default (evaluates to <literal>3</literal>)</entry>
101 </row>
102 <row>
103 <entry><literal>{ x = 1; y = 2; } // { z = 3; }</literal></entry>
104 <entry>Merge two sets (attributes in the right-hand set taking precedence)</entry>
105 </row>
106
107 <row>
108 <entry namest="c1" nameend="c2"><emphasis>Control structures</emphasis></entry>
109 </row>
110 <row>
111 <entry><literal>if 1 + 1 == 2 then "yes!" else "no!"</literal></entry>
112 <entry>Conditional expression</entry>
113 </row>
114 <row>
115 <entry><literal>assert 1 + 1 == 2; "yes!"</literal></entry>
116 <entry>Assertion check (evaluates to <literal>"yes!"</literal>)</entry>
117 </row>
118 <row>
119 <entry><literal>let x = "foo"; y = "bar"; in x + y</literal></entry>
120 <entry>Variable definition</entry>
121 </row>
122 <row>
123 <entry><literal>with pkgs.lib; head [ 1 2 3 ]</literal></entry>
124 <entry>Add all attributes from the given set to the scope
125 (evaluates to <literal>1</literal>)</entry>
126 </row>
127
128 <row>
129 <entry namest="c1" nameend="c2"><emphasis>Functions (lambdas)</emphasis></entry>
130 </row>
131 <row>
132 <entry><literal>x: x + 1</literal></entry>
133 <entry>A function that expects an integer and returns it increased by 1</entry>
134 </row>
135 <row>
136 <entry><literal>(x: x + 1) 100</literal></entry>
137 <entry>A function call (evaluates to 101)</entry>
138 </row>
139 <row>
140 <entry><literal>let inc = x: x + 1; in inc (inc (inc 100))</literal></entry>
141 <entry>A function bound to a variable and subsequently called by name (evaluates to 103)</entry>
142 </row>
143 <row>
144 <entry><literal>{ x, y }: x + y</literal></entry>
145 <entry>A function that expects a set with required attributes
146 <literal>x</literal> and <literal>y</literal> and concatenates
147 them</entry>
148 </row>
149 <row>
150 <entry><literal>{ x, y ? "bar" }: x + y</literal></entry>
151 <entry>A function that expects a set with required attribute
152 <literal>x</literal> and optional <literal>y</literal>, using
153 <literal>"bar"</literal> as default value for
154 <literal>y</literal></entry>
155 </row>
156 <row>
157 <entry><literal>{ x, y, ... }: x + y</literal></entry>
158 <entry>A function that expects a set with required attributes
159 <literal>x</literal> and <literal>y</literal> and ignores any
160 other attributes</entry>
161 </row>
162 <row>
163 <entry><literal>{ x, y } @ args: x + y</literal></entry>
164 <entry>A function that expects a set with required attributes
165 <literal>x</literal> and <literal>y</literal>, and binds the
166 whole set to <literal>args</literal></entry>
167 </row>
168
169 <row>
170 <entry namest="c1" nameend="c2"><emphasis>Built-in functions</emphasis></entry>
171 </row>
172 <row>
173 <entry><literal>import ./foo.nix</literal></entry>
174 <entry>Load and return Nix expression in given file</entry>
175 </row>
176 <row>
177 <entry><literal>map (x: x + x) [ 1 2 3 ]</literal></entry>
178 <entry>Apply a function to every element of a list (evaluates to <literal>[ 2 4 6 ]</literal>)</entry>
179 </row>
180 <!--
181 <row>
182 <entry><literal>throw "Urgh"</literal></entry>
183 <entry>Raise an error condition</entry>
184 </row>
185 -->
186
187 </tbody>
188 </tgroup>
189</informaltable>
190
191</section>