mirror of
https://codeberg.org/mhwombat/nix-book.git
synced 2026-01-07 06:17:23 +08:00
34 lines
701 B
Text
34 lines
701 B
Text
= Attribute set operations
|
|
|
|
The `.` operator selects an attribute from a set.
|
|
|
|
[source]
|
|
....
|
|
nix-repl> animal = { name = { first = "Professor"; last = "Paws"; }; age = 10; species = "cat"; }
|
|
|
|
nix-repl> animal . age
|
|
10
|
|
|
|
nix-repl> animal . name . first
|
|
"Professor"
|
|
....
|
|
|
|
We can use the `?` operator to find out if a set has a particular attribute.
|
|
|
|
[source]
|
|
....
|
|
nix-repl> animal ? species
|
|
true
|
|
|
|
nix-repl> animal ? bicycle
|
|
false
|
|
....
|
|
|
|
We can use the `//` operator to modify an attribute set.
|
|
Recall that Nix values are immutable, so the result is a new value (the original is not modified).
|
|
|
|
[source]
|
|
....
|
|
nix-repl> animal // { species = "tiger"; }
|
|
{ age = 10; name = { ... }; species = "tiger"; }
|
|
....
|