at 18.03-beta 7.1 kB view raw
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>). See <xref 117 linkend="sec-assertions"/> for using assertions in modules</entry> 118 </row> 119 <row> 120 <entry><literal>let x = "foo"; y = "bar"; in x + y</literal></entry> 121 <entry>Variable definition</entry> 122 </row> 123 <row> 124 <entry><literal>with pkgs.lib; head [ 1 2 3 ]</literal></entry> 125 <entry>Add all attributes from the given set to the scope 126 (evaluates to <literal>1</literal>)</entry> 127 </row> 128 129 <row> 130 <entry namest="c1" nameend="c2"><emphasis>Functions (lambdas)</emphasis></entry> 131 </row> 132 <row> 133 <entry><literal>x: x + 1</literal></entry> 134 <entry>A function that expects an integer and returns it increased by 1</entry> 135 </row> 136 <row> 137 <entry><literal>(x: x + 1) 100</literal></entry> 138 <entry>A function call (evaluates to 101)</entry> 139 </row> 140 <row> 141 <entry><literal>let inc = x: x + 1; in inc (inc (inc 100))</literal></entry> 142 <entry>A function bound to a variable and subsequently called by name (evaluates to 103)</entry> 143 </row> 144 <row> 145 <entry><literal>{ x, y }: x + y</literal></entry> 146 <entry>A function that expects a set with required attributes 147 <literal>x</literal> and <literal>y</literal> and concatenates 148 them</entry> 149 </row> 150 <row> 151 <entry><literal>{ x, y ? "bar" }: x + y</literal></entry> 152 <entry>A function that expects a set with required attribute 153 <literal>x</literal> and optional <literal>y</literal>, using 154 <literal>"bar"</literal> as default value for 155 <literal>y</literal></entry> 156 </row> 157 <row> 158 <entry><literal>{ x, y, ... }: x + y</literal></entry> 159 <entry>A function that expects a set with required attributes 160 <literal>x</literal> and <literal>y</literal> and ignores any 161 other attributes</entry> 162 </row> 163 <row> 164 <entry><literal>{ x, y } @ args: x + y</literal></entry> 165 <entry>A function that expects a set with required attributes 166 <literal>x</literal> and <literal>y</literal>, and binds the 167 whole set to <literal>args</literal></entry> 168 </row> 169 170 <row> 171 <entry namest="c1" nameend="c2"><emphasis>Built-in functions</emphasis></entry> 172 </row> 173 <row> 174 <entry><literal>import ./foo.nix</literal></entry> 175 <entry>Load and return Nix expression in given file</entry> 176 </row> 177 <row> 178 <entry><literal>map (x: x + x) [ 1 2 3 ]</literal></entry> 179 <entry>Apply a function to every element of a list (evaluates to <literal>[ 2 4 6 ]</literal>)</entry> 180 </row> 181 <!-- 182 <row> 183 <entry><literal>throw "Urgh"</literal></entry> 184 <entry>Raise an error condition</entry> 185 </row> 186 --> 187 188 </tbody> 189 </tgroup> 190</informaltable> 191 192</section>