Nix (language): Difference between revisions
imported>Makefu add operators |
imported>Ixxie m Standardized section capitalization and breaklines. |
||
Line 32: | Line 32: | ||
This section describes the main language features of the nix expression language | This section describes the main language features of the nix expression language | ||
=== Expressions === | |||
When Nix tutorials talk about '''Nix Expressions''' they typically mean the definition of a function with multiple inputs which as a result in a derivation. However a Nix expression can be everything, from a simple string, to function to a set of expressions. | |||
=== Types === | |||
The nix language provides a number of basic types: | The nix language provides a number of basic types: | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 71: | Line 71: | ||
A detailed description of all types can be found in [https://nixos.org/nix/manual/#ssec-values The Nix manual] | A detailed description of all types can be found in [https://nixos.org/nix/manual/#ssec-values The Nix manual] | ||
=== | === Functions === | ||
Functions have the following form: <code>pattern: body</code> | Functions have the following form: <code>pattern: body</code> | ||
Line 81: | Line 82: | ||
=== Operators === | === Operators === | ||
Lower precedence means a stronger binding; ie. this list is sorted from strongest to weakest binding, and in the case of equal precedence between two operators, the associativity decides the binding. | Lower precedence means a stronger binding; ie. this list is sorted from strongest to weakest binding, and in the case of equal precedence between two operators, the associativity decides the binding. | ||
Line 213: | Line 215: | ||
Source: [https://gist.github.com/joepie91/c3c047f3406aea9ec65eebce2ffd449d Gist of joepie91] | Source: [https://gist.github.com/joepie91/c3c047f3406aea9ec65eebce2ffd449d Gist of joepie91] | ||
=== | === Imports === | ||
<code>import</code> loads, parses and imports the nix expression stored in path. This keyword is essentially a builtin of nix but not a part of the language itself. | <code>import</code> loads, parses and imports the nix expression stored in path. This keyword is essentially a builtin of nix but not a part of the language itself. | ||
Line 223: | Line 225: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
=== | === Notable constructs === | ||
Nix looks a lot like JSON with functions but also provides a number of very specialized constructs which can help you build clean and easy to read expressions. In this sub-chapter the most notable constructs will be shown by example: | Nix looks a lot like JSON with functions but also provides a number of very specialized constructs which can help you build clean and easy to read expressions. In this sub-chapter the most notable constructs will be shown by example: | ||
Line 315: | Line 318: | ||
} | } | ||
</syntaxHighlight> | </syntaxHighlight> | ||
==== <code>let ... in</code> statement ==== | ==== <code>let ... in</code> statement ==== | ||
With <code>let</code> you can define local variables which can also reference to self without the need of the <code>rec</code> construct. This feature is use inside expressions to prepare variables which become part of an output. | With <code>let</code> you can define local variables which can also reference to self without the need of the <code>rec</code> construct. This feature is use inside expressions to prepare variables which become part of an output. | ||
The usage of <code>let</code> is comparable to the [http://zvon.org/other/haskell/Outputsyntax/letQexpressions_reference.html Haskell let expression] | The usage of <code>let</code> is comparable to the [http://zvon.org/other/haskell/Outputsyntax/letQexpressions_reference.html Haskell let expression] | ||
Line 327: | Line 332: | ||
==== <code>inherit</code> statement ==== | ==== <code>inherit</code> statement ==== | ||
The <code>inherit</code> expression can be used to copy variables from the surrounding lexical scope. A typical use case is to declare the version or name of a derivation in the expression and reuse this parameter in the function to fetch the source. | The <code>inherit</code> expression can be used to copy variables from the surrounding lexical scope. A typical use case is to declare the version or name of a derivation in the expression and reuse this parameter in the function to fetch the source. | ||
Line 342: | Line 348: | ||
==== <code>rec</code> statement ==== | ==== <code>rec</code> statement ==== | ||
The <code>rec</code> expression turns a basic set into a set where self-referencing is possible. This can be used when the <code>let</code> expression would create too much clutter. It is often seen in package derivation descriptions. | The <code>rec</code> expression turns a basic set into a set where self-referencing is possible. This can be used when the <code>let</code> expression would create too much clutter. It is often seen in package derivation descriptions. | ||
Line 352: | Line 359: | ||
=> 23 | => 23 | ||
</syntaxHighlight> | </syntaxHighlight> | ||
== Learning | |||
== Learning resources == | |||
The [https://nixos.org/nix/manual/#ch-expression-language manual] provides a '''reference''' of the Nix language. All language constructs you may use in nix are defined here, together with code snippets. | The [https://nixos.org/nix/manual/#ch-expression-language manual] provides a '''reference''' of the Nix language. All language constructs you may use in nix are defined here, together with code snippets. | ||
Line 359: | Line 367: | ||
The [https://nixos.org/nixos/nix-pills/index.html nix pills] also provide a lot of insight into the language and functional package management in general. | The [https://nixos.org/nixos/nix-pills/index.html nix pills] also provide a lot of insight into the language and functional package management in general. | ||
== Development | == Development tools == | ||
=== Syntax | === Syntax highlighting & editor modes === | ||
Nix language has decent syntax highlighting (SH) support among popular code editors, but refactoring/autocomplete is still rare. | Nix language has decent syntax highlighting (SH) support among popular code editors, but refactoring/autocomplete is still rare. |