initial commit

This commit is contained in:
Amy de Buitléir 2023-12-01 20:14:26 +00:00
parent 3bda18c1db
commit cb079d4d5c
7 changed files with 142 additions and 1 deletions

View file

@ -1,5 +1,24 @@
= Attribute set operations
An ordinary attribute set cannot refer to its own elements.
To do this, you need a _recursive_ attribute set.
[source]
....
nix-repl> { x = 3; y = 4*x; }
error: undefined variable 'x'
at «string»:1:16:
1| { x = 3; y = 4*x; }
| ^
nix-repl> rec { x = 3; y = 4*x; }
{ x = 3; y = 12; }
....
The `.` operator selects an attribute from a set.
[source]

View file

@ -0,0 +1,15 @@
= If expressions
The conditional construct in Nix is an _expression_, not a _statement_.
Since expressions must have values in all cases, you must specify both the `then` and the `else` branch.
[source]
....
nix-repl> a = 7
nix-repl> b = 3
nix-repl> if a > b then "yes" else "no"
"yes"
....

View file

@ -0,0 +1,46 @@
= Let expressions
A `let` expression defines a value with a local scope.
[source]
....
nix-repl> let x = 3; in x*x
9
nix-repl> let x = 3; y = 2; in x*x + y
11
....
You can also nest `let` expressions.
The previous expression is equivalent to the following.
[source]
....
nix-repl> let x = 3; in let y = 2; in x*x + y
11
....
[IMPORTANT]
====
A variable defined inside a `let` expression will "shadow" an outer variable with the same name.
[source]
....
nix-repl> x = 100
nix-repl> let x = 3; in x*x
9
nix-repl> let x = 3; in let x = 7; in x+1
8
....
====
A variable in a let expression can refer to another variable in the expression.
This is similar to how recursive attribute sets work.
[source]
....
nix-repl> let x = 3; y = x + 1; in x*y
12
....

View file

@ -25,3 +25,9 @@ include::attribute-sets.adoc[leveloffset=+1]
include::functions.adoc[leveloffset=+1]
include::builtins.adoc[leveloffset=+1]
include::if.adoc[leveloffset=+1]
include::let.adoc[leveloffset=+1]
include::with.adoc[leveloffset=+1]

View file

@ -1,4 +1,18 @@
= Path opearations
= Path operations
Any expression that contains a forward slash (`/`) _not_ followed by a space
is interpreted as a path.
To refer to a file or directory relative to the current directory, prefix it with `./`.
You can specify the current directory as `./.`
[source]
....
nix-repl> ./file.txt
/home/amy/codeberg/nix-book/file.txt
nix-repl> ./.
/home/amy/codeberg/nix-book
....
Paths can be concatenated with each other to produce a new path.

View file

@ -63,6 +63,10 @@ Note that the final semi-colon before the closing bracket is required.
{ name = "Professor Paws"; age = 10; species = "cat"; }
The keys of an attribute set must be strings.
When the key is not a valid identifier, it must be enclosed in quotation marks.
{ abc = true; "123" = false; }
Attribute sets can be empty.

View file

@ -0,0 +1,37 @@
= With expression
A `with` expression is somewhat similar to a `let` expression,
but it brings all of the associations in an attribute set into scope.
[source]
....
nix-repl> point = { x1 = 3; x2 = 2; }
nix-repl> with point; x1*x1 + x2
11
....
[IMPORTANT]
====
Unlike a `let` expression, a variable defined inside a `with` expression will _not_
"shadow" an outer variable with the same name.
[source]
....
nix-repl> name = "Amy"
nix-repl> animal = { name = "Professor Paws"; age = 10; species = "cat"; }
nix-repl> with animal; "Hello, " + name
"Hello, Amy"
....
However, you can refer to it using the attribute set selection operaator `.`.
[source]
....
nix-repl> with animal; "Hello, " + animal.name
"Hello, Professor Paws"
....
====