-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.nix
108 lines (77 loc) · 1.13 KB
/
basics.nix
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
## Core data types
# Booleans
true
false
true && true
true || false
# Int
1
2
3
# Strings
"a string"
''
multiline
string
''
"string" + "concat"
# Lists
[ 1 2 3 ]
[ 1 "hello" ]
[ 1 ] ++ [ 2 ]
# attribute sets
{
key1 = "value1";
"key 2" = "value2";
key3 = {
a = 5;
};
}
{ a = 2; }.a
{ a = 1; c = 3; } // { b = 2; c = 4;}
# paths
/some/path
./.
## Functions and binding
# function
x: x + 1
a: b: a + b
{a, b}: a + b
{a, b, ...}: a + b
set@{a, b}: a + b
{a ? 5, b ? 1}: a + b
## String antiquotation
"string ${toString (5 + 5)}"
# let binding
let
x = 1;
in x + x
let
f = x: y: x + y;
in f 1 2
# with
with { a = 1; b = 2; }; a + b
# C.f. equivalent let
let
a = 1;
b = 2;
in a + b
## recursion
let
x = y + 1;
y = 1;
in x
rec {
x = y + 1;
y = 1;
}
## Conditionals
if true then "it's true!" else 0
if { a = 1; } ? b then "there's a b" else "there's no b"
## Loading modules
import /path/to/file.nix
import <nixpkgs> {}
## Standard library functions
lib.map (x: x + 1) [1 2 3]
lib.mapAttrs (name: value: value + name ) { a = "b"; c = "d"; }
lib.mapAttrsToList (name: value: value + name ) { a = "b"; c = "d"; }