at 22.05-pre 9.2 kB view raw
1<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-nix-syntax-summary"> 2 <title>Syntax Summary</title> 3 <para> 4 Below is a summary of the most important syntactic constructs in the 5 Nix expression language. It’s not complete. In particular, there are 6 many other built-in functions. See the 7 <link xlink:href="https://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix 8 manual</link> for the rest. 9 </para> 10 <informaltable> 11 <tgroup cols="2"> 12 <colspec align="left" /> 13 <colspec align="left" /> 14 <thead> 15 <row> 16 <entry> 17 Example 18 </entry> 19 <entry> 20 Description 21 </entry> 22 </row> 23 </thead> 24 <tbody> 25 <row> 26 <entry> 27 <emphasis>Basic values</emphasis> 28 </entry> 29 <entry> 30 </entry> 31 </row> 32 <row> 33 <entry> 34 <literal>&quot;Hello world&quot;</literal> 35 </entry> 36 <entry> 37 A string 38 </entry> 39 </row> 40 <row> 41 <entry> 42 <literal>&quot;${pkgs.bash}/bin/sh&quot;</literal> 43 </entry> 44 <entry> 45 A string containing an expression (expands to 46 <literal>&quot;/nix/store/hash-bash-version/bin/sh&quot;</literal>) 47 </entry> 48 </row> 49 <row> 50 <entry> 51 <literal>true</literal>, <literal>false</literal> 52 </entry> 53 <entry> 54 Booleans 55 </entry> 56 </row> 57 <row> 58 <entry> 59 <literal>123</literal> 60 </entry> 61 <entry> 62 An integer 63 </entry> 64 </row> 65 <row> 66 <entry> 67 <literal>./foo.png</literal> 68 </entry> 69 <entry> 70 A path (relative to the containing Nix expression) 71 </entry> 72 </row> 73 <row> 74 <entry> 75 <emphasis>Compound values</emphasis> 76 </entry> 77 <entry> 78 </entry> 79 </row> 80 <row> 81 <entry> 82 <literal>{ x = 1; y = 2; }</literal> 83 </entry> 84 <entry> 85 A set with attributes named <literal>x</literal> and 86 <literal>y</literal> 87 </entry> 88 </row> 89 <row> 90 <entry> 91 <literal>{ foo.bar = 1; }</literal> 92 </entry> 93 <entry> 94 A nested set, equivalent to 95 <literal>{ foo = { bar = 1; }; }</literal> 96 </entry> 97 </row> 98 <row> 99 <entry> 100 <literal>rec { x = &quot;foo&quot;; y = x + &quot;bar&quot;; }</literal> 101 </entry> 102 <entry> 103 A recursive set, equivalent to 104 <literal>{ x = &quot;foo&quot;; y = &quot;foobar&quot;; }</literal> 105 </entry> 106 </row> 107 <row> 108 <entry> 109 <literal>[ &quot;foo&quot; &quot;bar&quot; ]</literal> 110 </entry> 111 <entry> 112 A list with two elements 113 </entry> 114 </row> 115 <row> 116 <entry> 117 <emphasis>Operators</emphasis> 118 </entry> 119 <entry> 120 </entry> 121 </row> 122 <row> 123 <entry> 124 <literal>&quot;foo&quot; + &quot;bar&quot;</literal> 125 </entry> 126 <entry> 127 String concatenation 128 </entry> 129 </row> 130 <row> 131 <entry> 132 <literal>1 + 2</literal> 133 </entry> 134 <entry> 135 Integer addition 136 </entry> 137 </row> 138 <row> 139 <entry> 140 <literal>&quot;foo&quot; == &quot;f&quot; + &quot;oo&quot;</literal> 141 </entry> 142 <entry> 143 Equality test (evaluates to <literal>true</literal>) 144 </entry> 145 </row> 146 <row> 147 <entry> 148 <literal>&quot;foo&quot; != &quot;bar&quot;</literal> 149 </entry> 150 <entry> 151 Inequality test (evaluates to <literal>true</literal>) 152 </entry> 153 </row> 154 <row> 155 <entry> 156 <literal>!true</literal> 157 </entry> 158 <entry> 159 Boolean negation 160 </entry> 161 </row> 162 <row> 163 <entry> 164 <literal>{ x = 1; y = 2; }.x</literal> 165 </entry> 166 <entry> 167 Attribute selection (evaluates to <literal>1</literal>) 168 </entry> 169 </row> 170 <row> 171 <entry> 172 <literal>{ x = 1; y = 2; }.z or 3</literal> 173 </entry> 174 <entry> 175 Attribute selection with default (evaluates to 176 <literal>3</literal>) 177 </entry> 178 </row> 179 <row> 180 <entry> 181 <literal>{ x = 1; y = 2; } // { z = 3; }</literal> 182 </entry> 183 <entry> 184 Merge two sets (attributes in the right-hand set taking 185 precedence) 186 </entry> 187 </row> 188 <row> 189 <entry> 190 <emphasis>Control structures</emphasis> 191 </entry> 192 <entry> 193 </entry> 194 </row> 195 <row> 196 <entry> 197 <literal>if 1 + 1 == 2 then &quot;yes!&quot; else &quot;no!&quot;</literal> 198 </entry> 199 <entry> 200 Conditional expression 201 </entry> 202 </row> 203 <row> 204 <entry> 205 <literal>assert 1 + 1 == 2; &quot;yes!&quot;</literal> 206 </entry> 207 <entry> 208 Assertion check (evaluates to 209 <literal>&quot;yes!&quot;</literal>). See 210 <xref linkend="sec-assertions" /> for using assertions in 211 modules 212 </entry> 213 </row> 214 <row> 215 <entry> 216 <literal>let x = &quot;foo&quot;; y = &quot;bar&quot;; in x + y</literal> 217 </entry> 218 <entry> 219 Variable definition 220 </entry> 221 </row> 222 <row> 223 <entry> 224 <literal>with pkgs.lib; head [ 1 2 3 ]</literal> 225 </entry> 226 <entry> 227 Add all attributes from the given set to the scope 228 (evaluates to <literal>1</literal>) 229 </entry> 230 </row> 231 <row> 232 <entry> 233 <emphasis>Functions (lambdas)</emphasis> 234 </entry> 235 <entry> 236 </entry> 237 </row> 238 <row> 239 <entry> 240 <literal>x: x + 1</literal> 241 </entry> 242 <entry> 243 A function that expects an integer and returns it increased 244 by 1 245 </entry> 246 </row> 247 <row> 248 <entry> 249 <literal>(x: x + 1) 100</literal> 250 </entry> 251 <entry> 252 A function call (evaluates to 101) 253 </entry> 254 </row> 255 <row> 256 <entry> 257 <literal>let inc = x: x + 1; in inc (inc (inc 100))</literal> 258 </entry> 259 <entry> 260 A function bound to a variable and subsequently called by 261 name (evaluates to 103) 262 </entry> 263 </row> 264 <row> 265 <entry> 266 <literal>{ x, y }: x + y</literal> 267 </entry> 268 <entry> 269 A function that expects a set with required attributes 270 <literal>x</literal> and <literal>y</literal> and 271 concatenates them 272 </entry> 273 </row> 274 <row> 275 <entry> 276 <literal>{ x, y ? &quot;bar&quot; }: x + y</literal> 277 </entry> 278 <entry> 279 A function that expects a set with required attribute 280 <literal>x</literal> and optional <literal>y</literal>, 281 using <literal>&quot;bar&quot;</literal> as default value 282 for <literal>y</literal> 283 </entry> 284 </row> 285 <row> 286 <entry> 287 <literal>{ x, y, ... }: x + y</literal> 288 </entry> 289 <entry> 290 A function that expects a set with required attributes 291 <literal>x</literal> and <literal>y</literal> and ignores 292 any other attributes 293 </entry> 294 </row> 295 <row> 296 <entry> 297 <literal>{ x, y } @ args: x + y</literal> 298 </entry> 299 <entry> 300 A function that expects a set with required attributes 301 <literal>x</literal> and <literal>y</literal>, and binds the 302 whole set to <literal>args</literal> 303 </entry> 304 </row> 305 <row> 306 <entry> 307 <emphasis>Built-in functions</emphasis> 308 </entry> 309 <entry> 310 </entry> 311 </row> 312 <row> 313 <entry> 314 <literal>import ./foo.nix</literal> 315 </entry> 316 <entry> 317 Load and return Nix expression in given file 318 </entry> 319 </row> 320 <row> 321 <entry> 322 <literal>map (x: x + x) [ 1 2 3 ]</literal> 323 </entry> 324 <entry> 325 Apply a function to every element of a list (evaluates to 326 <literal>[ 2 4 6 ]</literal>) 327 </entry> 328 </row> 329 </tbody> 330 </tgroup> 331 </informaltable> 332</section>