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