Nix (language): Difference between revisions
imported>Makefu m fix syntax highlight |
imported>Makefu add sets, functions, import |
||
Line 52: | Line 52: | ||
| Integers || There is no float in the nix language|| <code>5</code> | | Integers || There is no float in the nix language|| <code>5</code> | ||
|- | |- | ||
| Path || relative paths will become to absolute when evaluated,paths must contain a slash || < | | Path || relative paths will become to absolute when evaluated,paths must contain a slash. <code><nixpkgs/pkgs></code> is also possible and will resolve to the folder incl. subfolders in your NIX_PATH || <syntaxHighlight>./hello/world | ||
> /abs/path/to/hello/world | |||
</syntaxHighlight> | |||
<syntaxHighlight><nixpkgs/lib> | |||
> /path/to/your/nixpkgs/lib</syntaxHighlight> | |||
|- | |- | ||
| URI || || <code>http://example.org/foo.tar.bz2</code> | | URI || || <code>http://example.org/foo.tar.bz2</code> | ||
Line 60: | Line 64: | ||
| Null || || <code>null</code> | | Null || || <code>null</code> | ||
|- | |- | ||
| Lists || items are not separated by comma, can contain any other type || <syntaxHighlight lang=nix>[ 1 ./example.bin { hello="world"; }] </syntaxHighlight> | |||
|- | |||
| Sets || In other languages called <code>dicts</code>(py),<code>objects</code>(js) <code>hashes</code>(ruby), essentially a list of key-value pairs || <syntaxHighlight lang=nix>{ hello="world"; }.hello | |||
> "world"</syntaxHighlight> | |||
|- | |||
| Functions || see below || <code>pattern: body</code> | |||
|} | |} | ||
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 === | ||
Functions have the following form: <code>pattern: body</code> | |||
There are 3 patterns in how functions can be written: | |||
# <code>f = a: a*a;</code> a single identifier, can be extended with multiple identifiers as <code>f = a: b: a*b;</code> as a partial function would be returned. | |||
# <code>{ x, y ? "foo", z ? "bar", ... }: z + y + x</code> a set pattern, which can also set defaults. ellipse means that the function may receive extra arguments. | |||
# <code>args@{ x, y, z, ... }: z + y + x + args.a</code> an <code>@</code> pattern which can be used to match extra arguments and store them in the <code>args</code> set. | |||
=== imports === | === 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. | |||
Usage: | |||
<syntaxHighlight lang=nix> | |||
x = import <nixpkgs> {}; | |||
y = trace x.pkgs.hello.name x; | |||
</syntaxHighlight> | |||
=== notable constructs === | === notable constructs === |