Skip to content

Commit e59c973

Browse files
tazjinclbot
authored andcommitted
docs(nix-1p): add section about the merge operator
Without the examples, some behaviour of the merge operator might not be clear from the previous description. Due to how pervasively the operator is used, I think it warrants a separate section. This fixes #16 Change-Id: Iecba5f1cb749bef0a4987b3fc5642832a92c18d5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9599 Autosubmit: tazjin <[email protected]> Tested-by: BuildkiteCI Reviewed-by: flokli <[email protected]>
1 parent fb25210 commit e59c973

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and entering code snippets there.
1919
- [Language constructs](#language-constructs)
2020
- [Primitives / literals](#primitives--literals)
2121
- [Operators](#operators)
22+
- [`//` (merge) operator](#-merge-operator)
2223
- [Variable bindings](#variable-bindings)
2324
- [Functions](#functions)
2425
- [Multiple arguments (currying)](#multiple-arguments-currying)
@@ -113,8 +114,6 @@ rec { a = 15; b = a * 2; }
113114

114115
Nix has several operators, most of which are unsurprising:
115116

116-
Make sure to understand the `//`-operator, as it is used quite a lot and is
117-
probably the least familiar one.
118117
| Syntax | Description |
119118
|---------------------------|-----------------------------------------------------------------------------|
120119
| `+`, `-`, `*`, `/` | Numerical operations |
@@ -131,6 +130,37 @@ probably the least familiar one.
131130
| `left // right` | Merge `left` & `right` attribute sets, with the right set taking precedence |
132131

133132

133+
### `//` (merge) operator
134+
135+
The `//`-operator is used pervasively in Nix code. You should familiarise
136+
yourself with it, as it is likely also the least familiar one.
137+
138+
It merges the left and right attribute sets given to it:
139+
140+
```nix
141+
{ a = 1; } // { b = 2; }
142+
143+
# yields { a = 1; b = 2; }
144+
```
145+
146+
Values from the right side take precedence:
147+
148+
```nix
149+
{ a = "left"; } // { a = "right"; }
150+
151+
# yields { a = "right"; }
152+
```
153+
154+
The merge operator does *not* recursively merge attribute sets;
155+
156+
```nix
157+
{ a = { b = 1; }; } // { a = { c = 2; }; }
158+
159+
# yields { a = { c = 2; }; }
160+
```
161+
162+
Helper functions for recursive merging exist in the [`lib` library](#pkgslib).
163+
134164
## Variable bindings
135165

136166
Bindings in Nix are introduced locally via `let` expressions, which make some

0 commit comments

Comments
 (0)