aboutsummaryrefslogtreecommitdiff
path: root/doc/manual/nix-lang-ref.xml
blob: bda0ff5432db4e1c31cf1d05df56db502ad1cd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<appendix>
  <title>Nix Language Reference</title>

  <sect1>
    <title>Grammar</title>

    <productionset>
      <title>Expressions</title>
      
      <production id="nix.expr">
        <lhs>Expr</lhs>
        <rhs>
          <nonterminal def="#nix.expr_function" />
        </rhs>
      </production>
      
      <production id="nix.expr_function">
        <lhs>ExprFunction</lhs>
        <rhs>
          '{' <nonterminal def="#nix.formals" /> '}' ':' <nonterminal def="#nix.expr_function" />
          <sbr />|
          <nonterminal def="#nix.expr_assert" />
        </rhs>
      </production>
      
      <production id="nix.expr_assert">
        <lhs>ExprAssert</lhs>
        <rhs>
          'assert' <nonterminal def="#nix.expr" /> ';' <nonterminal def="#nix.expr_assert" />
          <sbr />|
          <nonterminal def="#nix.expr_if" />
        </rhs>
      </production>
      
      <production id="nix.expr_if">
        <lhs>ExprIf</lhs>
        <rhs>
          'if' <nonterminal def="#nix.expr" /> 'then' <nonterminal def="#nix.expr" />
          'else' <nonterminal def="#nix.expr" />
          <sbr />|
          <nonterminal def="#nix.expr_op" />
        </rhs>
      </production>
      
      <production id="nix.expr_op">
        <lhs>ExprOp</lhs>
        <rhs>
          '!' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '==' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '!=' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '&amp;&amp;' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '||' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '->' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '//' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '~' <nonterminal def="#nix.expr_op" />
          <sbr />|
          <nonterminal def="#nix.expr_op" /> '?' <nonterminal def="#nix.id" />
          <sbr />|
          <nonterminal def="#nix.expr_app" />
        </rhs>
      </production>
      
      <production id="nix.expr_app">
        <lhs>ExprApp</lhs>
        <rhs>
          <nonterminal def="#nix.expr_app" /> '.' <nonterminal def="#nix.expr_select" />
          <sbr />|
          <nonterminal def="#nix.expr_select" />
        </rhs>
      </production>
      
      <production id="nix.expr_select">
        <lhs>ExprSelect</lhs>
        <rhs>
          <nonterminal def="#nix.expr_select" /> <nonterminal def="#nix.id" />
          <sbr />|
          <nonterminal def="#nix.expr_simple" />
        </rhs>
      </production>
      
      <production id="nix.expr_simple">
        <lhs>ExprSimple</lhs>
        <rhs>
          <nonterminal def="#nix.id" /> |
          <nonterminal def="#nix.int" /> |
          <nonterminal def="#nix.str" /> |
          <nonterminal def="#nix.path" /> |
          <nonterminal def="#nix.uri" />
          <sbr />|
          'true' | 'false' | 'null'
          <sbr />|
          '(' <nonterminal def="#nix.expr" /> ')'
          <sbr />|
          '{' <nonterminal def="#nix.bind" />* '}'
          <sbr />|
          'let' '{' <nonterminal def="#nix.bind" />* '}'
          <sbr />|
          'rec' '{' <nonterminal def="#nix.bind" />* '}'
          <sbr />|
          '[' <nonterminal def="#nix.expr_select" />* ']'
        </rhs>
      </production>

      <production id="nix.bind">
        <lhs>Bind</lhs>
        <rhs>
          <nonterminal def="#nix.id" /> '=' <nonterminal def="#nix.expr" /> ';'
          <sbr />|
          'inherit' ('(' <nonterminal def="#nix.expr" /> ')')? <nonterminal def="#nix.id" />* ';'
        </rhs>
      </production>

      <production id="nix.formals">
        <lhs>Formals</lhs>
        <rhs>
          <nonterminal def="#nix.formal" /> ',' <nonterminal def="#nix.formals" />
          | <nonterminal def="#nix.formal" />
        </rhs>
      </production>
          
      <production id="nix.formal">
        <lhs>Formal</lhs>
        <rhs>
          <nonterminal def="#nix.id" />
          <sbr />|
          <nonterminal def="#nix.id" /> '?' <nonterminal def="#nix.expr" />
        </rhs>
      </production>
          
    </productionset>

    <productionset>
      <title>Terminals</title>

      <production id="nix.id">
        <lhs>Id</lhs>
        <rhs>[a-zA-Z\_][a-zA-Z0-9\_\']*</rhs>
      </production>
    
      <production id="nix.int">
        <lhs>Int</lhs>
        <rhs>[0-9]+</rhs>
      </production>
    
      <production id="nix.str">
        <lhs>Str</lhs>
        <rhs>\"[^\n\"]*\"</rhs>
      </production>

      <production id="nix.path">
        <lhs>Path</lhs>
        <rhs>[a-zA-Z0-9\.\_\-\+]*(\/[a-zA-Z0-9\.\_\-\+]+)+</rhs>
      </production>
    
      <production id="nix.uri">
        <lhs>Uri</lhs>
        <rhs>[a-zA-Z][a-zA-Z0-9\+\-\.]*\:[a-zA-Z0-9\%\/\?\:\@\&amp;\=\+\$\,\-\_\.\!\~\*\']+</rhs>
      </production>

      <production id="nix.ws">
        <lhs>Whitespace</lhs>
        <rhs>
          [ \t\n]+
          <sbr />|
          \#[^\n]*
          <sbr />|
          \/\*(.|\n)*\*\/
        </rhs>
      </production>

    </productionset>
    
  </sect1>
  


  <sect1>
    <title>Semantics</title>


    
    <sect2>
      <title>Built-in functions</title>

      <para>
        The Nix language provides the following built-in function
        (<quote>primops</quote>):
      </para>

      <variablelist>

        <varlistentry>
          <term><function>import</function>
          <replaceable>e</replaceable></term>
          <listitem>
            <para>
              Evaluates the expression <replaceable>e</replaceable>,
              which must yield a path value.  The Nix expression
              stored at this path in the file system is then read,
              parsed, and evaluated.  Returns the result of the
              evaluation of the Nix expression just read.
            </para>

            <para>
              Example: <literal>import ./foo.nix</literal> evaluates
              the expression stored in <filename>foo.nix</filename>
              (in the directory containing the expression in which the
              <function>import</function> occurs).
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><function>derivation</function>
          <replaceable>e</replaceable></term>
          <listitem>
            <para>
              Evaluates the expression <replaceable>e</replaceable>,
              which must yield an attribute set.  [...]
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><function>baseNameOf</function>
          <replaceable>e</replaceable></term>
          <listitem>
            <para>
              Evaluates the expression <replaceable>e</replaceable>,
              which must yield a string value, and returns a string
              representing its <emphasis>base name</emphasis>.  This
              is the substring following the last path separator
              (<literal>/</literal>).
            </para>

            <para>
              Example: <literal>baseNameOf "/foo/bar"</literal>
              returns <literal>"bar"</literal>, and
              <literal>baseNameOf "/foo/bar/"</literal> returns
              <literal>""</literal>.
            </para>
          </listitem>
        </varlistentry>

        <varlistentry>
          <term><function>toString</function>
          <replaceable>e</replaceable></term>
          <listitem>
            <para>
              Evaluates the expression <replaceable>e</replaceable>
              and coerces it into a string, if possible.  Only
              strings, paths, and URIs can be so coerced.
            </para>

            <para>
              Example: <literal>toString
              http://www.cs.uu.nl/</literal> returns
              <literal>"http://www.cs.uu.nl/"</literal>.
            </para>
          </listitem>
        </varlistentry>
            
      </variablelist>

    </sect2>

  </sect1>
  

</appendix>