$ nix repl
+$ nix repl
Welcome to Nix 2.18.1. Type :? for help.
nix-repl> :?
@@ -635,7 +706,7 @@ The following commands are available:
diff --git a/index.html b/index.html index 66d538b..e591b2b 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,78 @@ +
:? to see a list of available commands.
$ nix repl
+$ nix repl
Welcome to Nix 2.18.1. Type :? for help.
nix-repl> :?
@@ -635,7 +706,7 @@ The following commands are available:
nix-repl> a = 7
+nix-repl> a = 7
nix-repl> b = 3
@@ -658,7 +729,7 @@ so a-b is interpreted as the name of a variable rather than a subtr
nix-repl> a-b
+nix-repl> a-b
error: undefined variable 'a-b'
at «string»:1:1:
@@ -697,7 +768,7 @@ This makes it much easier to experiment in the REPL.
nix-repl> x = 1
+nix-repl> x = 1
nix-repl> x
1
@@ -723,7 +794,7 @@ nix-repl> x
nix-repl> 1 + 2 # addition
+nix-repl> 1 + 2 # addition
3
nix-repl> 5 - 3 # subtraction
@@ -753,7 +824,7 @@ Consider the following example.
nix-repl> 6/2
+nix-repl> 6/2
/home/amy/codeberg/nix-book/6/2
:t command to find out the type of the expressi
nix-repl> :t 6/2
+nix-repl> :t 6/2
a path
/home/amy/codeberg/nix-book.
nix-repl> 6/ 2
+nix-repl> 6/ 2
3
nix-repl> :t 5
+nix-repl> :t 5
an integer
nix-repl> :t 5.0
@@ -814,7 +885,7 @@ while the second produces a floating-point result.
nix-repl> 5 / 3
+nix-repl> 5 / 3
1
nix-repl> 5.0 / 3
@@ -832,7 +903,7 @@ nix-repl> 5.0 / 3
nix-repl> "Hello, " + "world!"
+nix-repl> "Hello, " + "world!"
"Hello, world!"
nix-repl> name = "Wombat"
+nix-repl> name = "Wombat"
nix-repl> "Hi, I'm ${name}."
"Hi, I'm Wombat."
@@ -863,7 +934,7 @@ Earlier we set a = 7, so the following expression fails.
nix-repl> "My favourite number is ${a}."
+nix-repl> "My favourite number is ${a}."
error:
… while evaluating a path segment
@@ -876,7 +947,8 @@ error:
Nix does provide functions for converting between types; we’ll see these in [_built_in_functions].
+Nix does provide functions for converting between types; we’ll see these in the +next section.
nix-repl> builtins.stringLength "supercalifragilisticexpialidocious"
+nix-repl> builtins.stringLength "supercalifragilisticexpialidocious"
34
0.
nix-repl> builtins.substring 3 6 "hayneedlestack"
+nix-repl> builtins.substring 3 6 "hayneedlestack"
"needle"
Convert an expression to a string.
nix-repl> builtins.toString 7
+nix-repl> builtins.toString 7
"7"
nix-repl> builtins.toString (3*4 + 1)
@@ -932,7 +1004,7 @@ Recall that earlier we set a = 7 and b = 3.
nix-repl> a == 7 # equality test
+nix-repl> a == 7 # equality test
true
nix-repl> b != 3 # inequality
@@ -966,7 +1038,7 @@ The expression u → v is equivalent to !u || v.
nix-repl> u = false
+nix-repl> u = false
nix-repl> v = true
@@ -988,7 +1060,7 @@ You can specify the current directory as ./.
nix-repl> ./file.txt
+nix-repl> ./file.txt
/home/amy/codeberg/nix-book/file.txt
nix-repl> ./.
@@ -1002,7 +1074,7 @@ nix-repl> ./.
nix-repl> /home/wombat + /bin/sh
+nix-repl> /home/wombat + /bin/sh
/home/wombat/bin/sh
nix-repl> :t /home/wombat + /bin/sh
@@ -1022,7 +1094,7 @@ This is why the result in the example below is not /home/wombat/file.txt
nix-repl> /home/wombat + ./file.txt
+nix-repl> /home/wombat + ./file.txt
/home/wombat/home/amy/codeberg/nix-book/file.txt
/home/wombat/file.txt
nix-repl> /home/wombat + "/file.txt"
+nix-repl> /home/wombat + "/file.txt"
/home/wombat/file.txt
nix-repl> :t /home/wombat + "/file.txt"
@@ -1075,7 +1147,7 @@ The result is a string, not a path.
nix-repl> "/home/wombat" + ./file.txt
+nix-repl> "/home/wombat" + ./file.txt
"/home/wombat/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt"
nix-repl> "/home/wombat" + ./no-such-file.txt
+nix-repl> "/home/wombat" + ./no-such-file.txt
error (ignored): error: end of string reached
error: getting status of '/home/amy/codeberg/nix-book/no-such-file.txt': No such file or directory
nix-repl> builtins.pathExists ./index.html
+nix-repl> builtins.pathExists ./index.html
true
nix-repl> builtins.pathExists /no/such/path
@@ -1115,7 +1187,7 @@ false
nix-repl> builtins.readDir ./.
+nix-repl> builtins.readDir ./.
{ ".envrc" = "regular"; ".git" = "directory"; ".gitignore" = "regular"; Makefile = "regular"; images = "directory"; "index.html" = "regular"; "shell.nix" = "regular"; source = "directory"; themes = "directory"; "wombats-book-of-nix.pdf" = "regular"; }
nix-repl> builtins.readFile ./.envrc
+nix-repl> builtins.readFile ./.envrc
"use nix\n"
nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
+nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
[ 1 2 3 "apple" "banana" ]
nix-repl> fruit = [ "apple" "banana" "cantaloupe" ]
+nix-repl> fruit = [ "apple" "banana" "cantaloupe" ]
nix-repl> builtins.elem "apple" fruit
true
@@ -1172,7 +1244,7 @@ The first element in a list has index 0.
nix-repl> builtins.elemAt fruit 0
+nix-repl> builtins.elemAt fruit 0
"apple"
nix-repl> builtins.elemAt fruit 2
@@ -1184,7 +1256,7 @@ nix-repl> builtins.elemAt fruit 2
nix-repl> builtins.length fruit
+nix-repl> builtins.length fruit
3
nix-repl> builtins.head fruit
+nix-repl> builtins.head fruit
"apple"
nix-repl> builtins.tail fruit
+nix-repl> builtins.tail fruit
[ "banana" "cantaloupe" ]
nix-repl> numbers = [ 1 3 6 8 9 15 25 ]
+nix-repl> numbers = [ 1 3 6 8 9 15 25 ]
nix-repl> isBig = n: n > 10 # is the number "big" (greater than 10)?
@@ -1229,7 +1301,7 @@ nix-repl> builtins.filter isBig numbers # get just the "big" nu
nix-repl> double = n: 2*n # multiply by two
+nix-repl> double = n: 2*n # multiply by two
nix-repl> builtins.map double numbers # double each element in the list
[ 2 6 12 16 18 30 50 ]
@@ -1246,7 +1318,7 @@ nix-repl> builtins.map double numbers # double each element in the list
nix-repl> animal = { name = { first = "Professor"; last = "Paws"; }; age = 10; species = "cat"; }
+nix-repl> animal = { name = { first = "Professor"; last = "Paws"; }; age = 10; species = "cat"; }
nix-repl> animal . age
10
@@ -1263,7 +1335,7 @@ nix-repl> animal . name . first
nix-repl> animal ? species
+nix-repl> animal ? species
true
nix-repl> animal ? bicycle
@@ -1280,7 +1352,7 @@ Attributes in the right-hand set take preference.
nix-repl> animal // { species = "tiger"; }
+nix-repl> animal // { species = "tiger"; }
{ age = 10; name = { ... }; species = "tiger"; }
nix-repl> { x = 3; y = 4*x; }
+nix-repl> { x = 3; y = 4*x; }
error: undefined variable 'x'
at «string»:1:16:
@@ -1319,7 +1391,7 @@ For more information on these and other built-in functions, see the Nix Manual
nix-repl> builtins.attrNames animal
+nix-repl> builtins.attrNames animal
[ "age" "name" "species" ]
nix-repl> builtins.attrValues animal
+nix-repl> builtins.attrValues animal
[ 10 "Professor Paws" "cat" ]
nix-repl> builtins.getAttr "age" animal
+nix-repl> builtins.getAttr "age" animal
10
nix-repl> builtins.hasAttr "name" animal
+nix-repl> builtins.hasAttr "name" animal
true
nix-repl> builtins.hasAttr "car" animal
@@ -1358,7 +1430,7 @@ false
nix-repl> builtins.removeAttrs animal [ "age" "species" ]
+nix-repl> builtins.removeAttrs animal [ "age" "species" ]
{ name = "Professor Paws"; }
nix-repl> x: x + 1
+nix-repl> x: x + 1
«lambda @ «string»:1:1»
nix-repl> :t x: x + 1
+nix-repl> :t x: x + 1
a function
nix-repl> f = x: x + 1
+nix-repl> f = x: x + 1
nix-repl> f
«lambda @ «string»:1:2»
@@ -1425,7 +1497,7 @@ This reduces visual clutter when chaining a series of functions.
nix-repl> f 5
+nix-repl> f 5
6
nix-repl> add = a: (b: a+b)
+nix-repl> add = a: (b: a+b)
nix-repl> add = a: b: a+b
+nix-repl> add = a: b: a+b
nix-repl> add 3 # Returns a function that adds 3 to any input
+nix-repl> add 3 # Returns a function that adds 3 to any input
«lambda @ «string»:1:6»
nix-repl> (add 3) 5
+nix-repl> (add 3) 5
8
nix-repl> add 3 5
+nix-repl> add 3 5
8
add to the value 3, and give the res
nix-repl> g = add 3
+nix-repl> g = add 3
g is indeed a function.
nix-repl> :t g
+nix-repl> :t g
a function
nix-repl> g 5
+nix-repl> g 5
8
nix-repl> greet = { first, last }: "Hello ${first} ${last}! May I call you ${first}?"
+nix-repl> greet = { first, last }: "Hello ${first} ${last}! May I call you ${first}?"
nix-repl> greet { first="Amy"; last="de Buitléir"; }
"Hello Amy de Buitléir! May I call you Amy?"
@@ -1573,7 +1645,7 @@ This is illustrated below.
nix-repl> greet = { first, last ? "whatever-your-lastname-is", topic ? "Nix" }: "Hello ${first} ${last}! May I call you ${first}? Are you enjoying learning ${topic}?"
+nix-repl> greet = { first, last ? "whatever-your-lastname-is", topic ? "Nix" }: "Hello ${first} ${last}! May I call you ${first}? Are you enjoying learning ${topic}?"
nix-repl> greet { first="Amy"; }
"Hello Amy whatever-your-lastname-is! May I call you Amy? Are you enjoying learning Nix?"
@@ -1591,7 +1663,7 @@ This is done with the special parameter ….
nix-repl> formatName = { first, last, ... }: "${first} ${last}"
+nix-repl> formatName = { first, last, ... }: "${first} ${last}"
nix-repl> person = { first="Joe"; last="Bloggs"; address="123 Main Street"; }
+nix-repl> person = { first="Joe"; last="Bloggs"; address="123 Main Street"; }
nix-repl> formatName person
"Joe Bloggs"
@@ -1620,7 +1692,7 @@ This is done using an @-pattern.
nix-repl> formatPoint = p@{ x, y, ... }: builtins.toXML p
+nix-repl> formatPoint = p@{ x, y, ... }: builtins.toXML p
nix-repl> formatPoint { x=5; y=3; z=2; }
"<?xml version='1.0' encoding='utf-8'?>\n<expr>\n <attrs>\n <attr name=\"x\">\n <int value=\"5\" />\n </attr>\n <attr name=\"y\">\n <int value=\"3\" />\n </attr>\n <attr name=\"z\">\n <int value=\"2\" />\n </attr>\n </attrs>\n</expr>\n"
@@ -1631,7 +1703,7 @@ nix-repl> formatPoint { x=5; y=3; z=2; }
nix-repl> formatPoint = { x, y, ... } @ p: builtins.toXML p
+nix-repl> formatPoint = { x, y, ... } @ p: builtins.toXML p
confirmAddress.
nix-repl> confirmAddress = { address, ... }: "Do you still live at ${address}?"
+nix-repl> confirmAddress = { address, ... }: "Do you still live at ${address}?"
nix-repl> greet = args@{ first, last, ... }: "Hello ${first}. " + confirmAddress args
@@ -1660,7 +1732,7 @@ Since expressions must have values in all cases, you must specify both the
-nix-repl> a = 7
+nix-repl> a = 7
nix-repl> b = 3
@@ -1676,7 +1748,7 @@ nix-repl> if a > b then "yes" else "no"
-nix-repl> let x = 3; in x*x
+nix-repl> let x = 3; in x*x
9
nix-repl> let x = 3; y = 2; in x*x + y
@@ -1689,7 +1761,7 @@ The previous expression is equivalent to the following.
-nix-repl> let x = 3; in let y = 2; in x*x + y
+nix-repl> let x = 3; in let y = 2; in x*x + y
11
@@ -1705,7 +1777,7 @@ The previous expression is equivalent to the following.
-nix-repl> x = 100
+nix-repl> x = 100
nix-repl> let x = 3; in x*x
9
@@ -1724,7 +1796,7 @@ This is similar to how recursive attribute sets work.
-nix-repl> let x = 3; y = x + 1; in x*y
+nix-repl> let x = 3; y = x + 1; in x*y
12
@@ -1737,7 +1809,7 @@ but it brings all of the associations in an attribute set into scope.
-nix-repl> point = { x1 = 3; x2 = 2; }
+nix-repl> point = { x1 = 3; x2 = 2; }
nix-repl> with point; x1*x1 + x2
11
@@ -1756,7 +1828,7 @@ nix-repl> with point; x1*x1 + x2
-nix-repl> name = "Amy"
+nix-repl> name = "Amy"
nix-repl> animal = { name = "Professor Paws"; age = 10; species = "cat"; }
@@ -1770,7 +1842,7 @@ using the attribute selection operator (.).
-nix-repl> with animal; "Hello, " + animal.name
+nix-repl> with animal; "Hello, " + animal.name
"Hello, Professor Paws"
@@ -1886,11 +1958,11 @@ It’s an extremely simple script with just two lines.
hello-flake
-