diff --git a/index.html b/index.html index 66d538b..e591b2b 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,78 @@ +
-
$ 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
@@ -763,7 +834,7 @@ Let’s use the :t command to find out the type of the expressi
-
nix-repl> :t 6/2
+
nix-repl> :t 6/2
 a path
@@ -778,7 +849,7 @@ which in the above example was /home/amy/codeberg/nix-book.

-
nix-repl> 6/ 2
+
nix-repl> 6/ 2
 3
@@ -798,7 +869,7 @@ To ensure that a number is interpreted as a floating-point value, add a decimal
-
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!"
@@ -844,7 +915,7 @@ nix-repl> 5.0 / 3
-
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.

@@ -896,7 +968,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
nix-repl> builtins.stringLength "supercalifragilisticexpialidocious"
+
nix-repl> builtins.stringLength "supercalifragilisticexpialidocious"
 34
@@ -906,16 +978,16 @@ The first character of a string has index 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
@@ -1038,7 +1110,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/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"
@@ -1084,7 +1156,7 @@ The result is a string, not a path.

-
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
@@ -1103,7 +1175,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
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"; }
@@ -1124,7 +1196,7 @@ false
-
nix-repl> builtins.readFile ./.envrc
+
nix-repl> builtins.readFile ./.envrc
 "use nix\n"
@@ -1139,7 +1211,7 @@ false
-
nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
+
nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
 [ 1 2 3 "apple" "banana" ]
@@ -1157,7 +1229,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
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
@@ -1193,7 +1265,7 @@ nix-repl> builtins.elemAt fruit 2
-
nix-repl> builtins.head fruit
+
nix-repl> builtins.head fruit
 "apple"
@@ -1202,7 +1274,7 @@ nix-repl> builtins.elemAt fruit 2
-
nix-repl> builtins.tail fruit
+
nix-repl> builtins.tail fruit
 [ "banana" "cantaloupe" ]
@@ -1216,7 +1288,7 @@ but the following examples should be somewhat self-explanatory.

-
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"; }
@@ -1293,7 +1365,7 @@ To do this, you need a recursive attribute set.

-
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" ]
@@ -1328,7 +1400,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
nix-repl> builtins.attrValues animal
+
nix-repl> builtins.attrValues animal
 [ 10 "Professor Paws" "cat" ]
@@ -1337,7 +1409,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
nix-repl> builtins.getAttr "age" animal
+
nix-repl> builtins.getAttr "age" animal
 10
@@ -1346,7 +1418,7 @@ For more information on these and other built-in functions, see the Nix Manual
-
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"; }
@@ -1375,7 +1447,7 @@ Consider the following example.

-
nix-repl> x: x + 1
+
nix-repl> x: x + 1
 «lambda @ «string»:1:1»
@@ -1395,7 +1467,7 @@ Functional programmers often call anonymous functions "lambdas".

-
nix-repl> :t x: x + 1
+
nix-repl> :t x: x + 1
 a function
@@ -1409,7 +1481,7 @@ In particular, we can assign it to a variable.

-
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
@@ -1439,7 +1511,7 @@ we create functions that return functions!

-
nix-repl> add = a: (b: a+b)
+
nix-repl> add = a: (b: a+b)
@@ -1455,7 +1527,7 @@ More commonly we would write the following.

-
nix-repl> add = a: b: a+b
+
nix-repl> add = a: b: a+b
@@ -1464,7 +1536,7 @@ Invoking a function without supplying all of the expected parameters is called <
-
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»
@@ -1473,7 +1545,7 @@ Invoking a function without supplying all of the expected parameters is called <
-
nix-repl> (add 3) 5
+
nix-repl> (add 3) 5
 8
@@ -1482,7 +1554,7 @@ Invoking a function without supplying all of the expected parameters is called <
-
nix-repl> add 3 5
+
nix-repl> add 3 5
 8
@@ -1501,7 +1573,7 @@ Let’s apply add to the value 3, and give the res
-
nix-repl> g = add 3
+
nix-repl> g = add 3
@@ -1510,7 +1582,7 @@ The Nix REPL confirms that g is indeed a function.

-
nix-repl> :t g
+
nix-repl> :t g
 a function
@@ -1519,7 +1591,7 @@ a function
-
nix-repl> g 5
+
nix-repl> g 5
 8
@@ -1557,7 +1629,7 @@ Here’s an example of a function that has an attribute set as an input para
-
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}"
@@ -1600,7 +1672,7 @@ even though each function may not need all of the values.

-
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
@@ -1642,7 +1714,7 @@ to the function 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
-
1
-2
-3
#!/usr/bin/env sh
+
1
+2
+3
#!/usr/bin/env sh
 
-echo "Hello from your flake!"
+echo "Hello from your flake!"
 
@@ -1916,93 +1988,93 @@ is always called flake.nix.

flake.nix
-
@@ -2244,7 +2354,7 @@ input.

-

5.3. Outputs

+

5.2. Outputs

This section is a function that essentially returns the recipe for building the flake.

@@ -2256,7 +2366,7 @@ dependency defined in the previous example.

-
outputs = { self, nixpkgs, import-cargo }: {
+
outputs = { self, nixpkgs, import-cargo }: {
   definitions for outputs
 };
@@ -2287,7 +2397,7 @@ writing your own flakes. I’ll explain the example in some detail.

-
{
+
{
   description = "brief package description";
 
   inputs = {
@@ -2401,39 +2511,69 @@ let’s have a look at the one we saw earlier, in the hello-flake
 
-
+
flake.nix
-
{
-  description = "a very simple and friendly flake";
+
 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
{
-  # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
-  # for a brief overview of what each section in a flake should or can contain.
+
 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
{
+  # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
+  # for a brief overview of what each section in a flake should or can contain.
 
-  description = "a very simple and friendly flake";
+  description = "a very simple and friendly flake";
 
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        packages = rec {
-          hello = pkgs.stdenv.mkDerivation rec {
-            name = "hello-flake";
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        packages = rec {
+          hello = pkgs.stdenv.mkDerivation rec {
+            name = "hello-flake";
 
-            src = ./.;
+            src = ./.;
 
-            unpackPhase = "true";
+            unpackPhase = "true";
 
-            buildPhase = ":";
+            buildPhase = ":";
 
-            installPhase =
-              ''
-                mkdir -p $out/bin
-                cp $src/hello-flake $out/bin/hello-flake
-                chmod +x $out/bin/hello-flake
-              '';
-          };
-          default = hello;
-        };
+            installPhase =
+              ''
+                mkdir -p $out/bin
+                cp $src/hello-flake $out/bin/hello-flake
+                chmod +x $out/bin/hello-flake
+              '';
+          };
+          default = hello;
+        };
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+        apps = rec {
+          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
+          default = hello;
+        };
+      }
+    );
+}
 
@@ -2015,10 +2087,10 @@ For now, I’d like to focus on the inputs section.

-
inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+
inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
@@ -2035,7 +2107,47 @@ flakes. The important thing to note is that the hello-flake package
flake.lock
-
{
+
 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
{
   "nodes": {
     "flake-utils": {
       "inputs": {
@@ -2075,7 +2187,8 @@ flakes. The important thing to note is that the hello-flake package
         "flake-utils": "flake-utils",
         "nixpkgs": "nixpkgs"
       }
-. . .
+. . . +
@@ -2102,7 +2215,7 @@ benefit of using flakes.

-
{
+
{
   description = package description
   inputs = dependencies
   outputs = what the flake produces
@@ -2110,17 +2223,14 @@ benefit of using flakes.

}
-
-

5.1. Description

The description part is self-explanatory; it’s just a string. You probably won’t need nixConfig unless you’re doing something fancy. I’m going to focus on what goes into the inputs and outputs sections, and highlight some of the things I found confusing when I began using flakes.

-
-

5.2. Inputs

+

5.1. Inputs

This section specifies the dependencies of a flake. It’s an attribute set; it maps keys to values.

@@ -2147,7 +2257,7 @@ pre-packaged for Nix. We can write that as

-
nixpkgs.url = "github:NixOS/nixpkgs/nixos-version";
+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-version";
@@ -2158,7 +2268,7 @@ without the version number

-
nixpkgs.url = "github:NixOS/nixpkgs/nixos";
+
nixpkgs.url = "github:NixOS/nixpkgs/nixos";
@@ -2166,7 +2276,7 @@ without the version number

-
nixpkgs.url = "nixpkgs";
+
nixpkgs.url = "nixpkgs";
@@ -2218,11 +2328,11 @@ flake references that you might encounter. This example

-
inputs.import-cargo = {
-  type = "github";
-  owner = "edolstra";
-  repo = "import-cargo";
-};
+
inputs.import-cargo = {
+  type = "github";
+  owner = "edolstra";
+  repo = "import-cargo";
+};
@@ -2230,7 +2340,7 @@ flake references that you might encounter. This example

-
inputs.import-cargo.url = "github:edolstra/import-cargo";
+
inputs.import-cargo.url = "github:edolstra/import-cargo";
 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
{
+  description = "a very simple and friendly flake";
 
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        packages = rec {
-          hello =
-            . . .
-            SOME UNFAMILIAR STUFF
-            . . .
-          };
-          default = hello;
-        };
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        packages = rec {
+          hello =
+            . . .
+            _SOME UNFAMILIAR STUFF_
+            . . .
+          };
+          default = hello;
+        };
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+ apps = rec { + hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; + default = hello; + }; + } + ); +} +
@@ -2446,9 +2586,9 @@ additional development tools.

Now let’s look at the section I labeled SOME UNFAMILIAR STUFF and see what it does.

-
+
-
        packages = rec {
+
        packages = rec {
           hello = pkgs.stdenv.mkDerivation rec { ❶
             name = "hello-flake";
 
@@ -2464,7 +2604,7 @@ see what it does.

cp $src/hello-flake $out/bin/hello-flake ❹ chmod +x $out/bin/hello-flake ❺ ''; - };
+ };
@@ -2618,7 +2758,7 @@ so can’t we just run it?

$ hello-flake
-bash: line 16: hello-flake: command not found
+Hello from your flake!
@@ -2707,9 +2847,12 @@ Here’s the modified hello-flake file.

hello-flake
-
#!/usr/bin/env sh
+
1
+2
+3
#!/usr/bin/env sh
 
-cowsay "Hello from your flake!"
+cowsay "Hello from your flake!" +
@@ -2737,64 +2880,111 @@ The highlighted modifications below will accomplish that.

flake.nix
-
{
-  # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
-  # for a brief overview of what each section in a flake should or can contain.
+
 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
{
+  description = "a very simple and friendly flake";
 
-  description = "a very simple and friendly flake";
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
-
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            packages = [ pkgs.cowsay ];
-          };
-        };
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            packages = [ pkgs.cowsay ];
+          };
+        };
 
-        packages = rec {
-          hello = pkgs.stdenv.mkDerivation rec {
-            name = "hello-flake";
+        packages = rec {
+          hello = pkgs.stdenv.mkDerivation rec {
+            name = "hello-flake";
 
-            src = ./.;
+            src = ./.;
 
-            unpackPhase = "true";
+            unpackPhase = "true";
 
-            buildPhase = ":";
+            buildPhase = ":";
 
-            installPhase =
-              ''
-                mkdir -p $out/bin
-                cp $src/hello-flake $out/bin/hello-flake
-                chmod +x $out/bin/hello-flake
-              '';
-          };
-          default = hello;
-        };
+            installPhase =
+              ''
+                mkdir -p $out/bin
+                cp $src/hello-flake $out/bin/hello-flake
+                chmod +x $out/bin/hello-flake
+              '';
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+ buildInputs = [ pkgs.cowsay ]; + }; + default = hello; + }; + + apps = rec { + hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; + default = hello; + }; + } + ); +} +
-

Now we restart the development shell and see that the cowsay command is -available and the script works. Because we’ve updated source files -but haven’t git commited the new version, we get a warning message -about it being “dirty”. It’s just a warning, though; the script runs -correctly.

+

Now we restart the development shell and see that the cowsay command is available. +Because we’ve updated source files but haven’t git commited the new version, +we get a warning message about it being “dirty”. +It’s just a warning, though; the script runs correctly.

@@ -2810,11 +3000,165 @@ $ ./hello-flake \ (oo)\_______ (__)\ )\/\ ||----w | - || || + || || +$ exit
-

Alternatively, we could use nix run.

+

Let’s try nix run.

+
+
+
+
$ nix run
+warning: Git tree '/home/amy/codeberg/nix-book/source/modify-hello-flake/hello-flake' is dirty
+/nix/store/7p4migpqjsr86xqj4dxlpvma3h3y2bzi-hello-flake/bin/hello-flake: line 3: cowsay: command not found
+
+
+
+

What went wrong? +Recall that we only added cowsay to the development environment; +we didn’t add it to the runtime environment. +The modified flake below will fix that.

+
+
+
flake.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
{
+  description = "a very simple and friendly flake";
+
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
+
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            packages = [ pkgs.cowsay ];
+          };
+        };
+
+        packages = rec {
+          hello = pkgs.stdenv.mkDerivation rec {
+            name = "hello-flake";
+
+            src = ./.;
+
+            unpackPhase = "true";
+
+            buildPhase = ":";
+
+            installPhase =
+              ''
+                mkdir -p $out/bin
+                cp $src/hello-flake $out/bin/hello-flake
+                chmod +x $out/bin/hello-flake
+
+                # modify the hello-flake script so it can find cowsay
+                COWSAY=$(type -p cowsay)
+                sed "s_cowsay_"$COWSAY"_" --in-place $out/bin/hello-flake
+              '';
+
+            buildInputs = [ pkgs.cowsay ];
+          };
+          default = hello;
+        };
+
+        apps = rec {
+          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
+          default = hello;
+        };
+      }
+    );
+}
+
+
+
+
+

Line 42 adds cowsay as a buildInput for the package. +That does make it available at build and runtime, but it doesn’t put it on the path, +so our hello-flake script wouldn’t be able to find it. +There are various ways to deal with this problem. +In this case we simply edited the script (lines 37-39) as we install it, +specifying the full path to cowsay.

+
+
+ + + + + +
+ + +
+

When you’re packaging a program written in a more powerful language such as +Haskell, Python, Java, C, C#, or Rust, +the language build system will usually do all that is required +to make the dependencies visible to the program at runtime. +In this case, adding the dependency to buildInputs is sufficient.

+
+
+
+
+

This time nix run works.

@@ -2853,7 +3197,7 @@ warning: Git tree '/home/amy/codeberg/nix-book/source/modify-hello-flake/hello-f $ cat result/bin/hello-flake #!/nix/store/zlf0f88vj30sc7567b80l52d19pbdmy2-bash-5.2-p15/bin/sh -cowsay "Hello from your flake!" +/nix/store/gfi27h4y5n4aralcxrc0377p8mjb1cvb-cowsay-3.7.0/bin/cowsay "Hello from your flake!"
@@ -2863,8 +3207,8 @@ don’t need to git push the changes until we’re ready to
$ git commit hello-flake flake.nix -m 'added bovine feature'
-[main b0d0d09] added bovine feature
- 2 files changed, 7 insertions(+), 1 deletion(-)
+[main 6523897] added bovine feature
+ 2 files changed, 13 insertions(+), 4 deletions(-)
 $ nix run
  ________________________
 < Hello from your flake! >
@@ -2984,19 +3328,19 @@ Initialized empty Git repository in /home/amy/codeberg/nix-book/source/new-flake
 
Main.hs
-
1
-2
-3
-4
-5
-6
-7
import Network.HostName
+
1
+2
+3
+4
+5
+6
+7
import Network.HostName
 
-main :: IO ()
-main = do
-  putStrLn "Hello from Haskell inside a Nix flake!"
-  h <- getHostName
-  putStrLn $ "Your hostname is: " ++ h
+main :: IO ()
+main = do
+  putStrLn "Hello from Haskell inside a Nix flake!"
+  h <- getHostName
+  putStrLn $ "Your hostname is: " ++ h
 
@@ -3168,33 +3512,33 @@ This is just an ordinary Cabal file; we don’t need to do anything special
hello-flake-haskell.cabal
-
 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
cabal-version:   3.0
+
 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
cabal-version:   3.0
 name:            hello-flake-haskell
 version:         1.0.0
 synopsis:        A simple demonstration using a Nix flake to package a Haskell program.
@@ -3317,121 +3661,121 @@ I’ve highlighted the change below.

flake.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
{
-  inputs = {
-    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
-    flake-parts.url = "github:hercules-ci/flake-parts";
-    haskell-flake.url = "github:srid/haskell-flake";
-  };
-  outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
-    flake-parts.lib.mkFlake { inherit inputs; } {
-      systems = nixpkgs.lib.systems.flakeExposed;
-      imports = [ inputs.haskell-flake.flakeModule ];
+
 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
{
+  inputs = {
+    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
+    flake-parts.url = "github:hercules-ci/flake-parts";
+    haskell-flake.url = "github:srid/haskell-flake";
+  };
+  outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
+    flake-parts.lib.mkFlake { inherit inputs; } {
+      systems = nixpkgs.lib.systems.flakeExposed;
+      imports = [ inputs.haskell-flake.flakeModule ];
 
-      perSystem = { self', pkgs, ... }: {
+      perSystem = { self', pkgs, ... }: {
 
-        # Typically, you just want a single project named "default". But
-        # multiple projects are also possible, each using different GHC version.
-        haskellProjects.default = {
-          # The base package set representing a specific GHC version.
-          # By default, this is pkgs.haskellPackages.
-          # You may also create your own. See https://community.flake.parts/haskell-flake/package-set
-          # basePackages = pkgs.haskellPackages;
+        # Typically, you just want a single project named "default". But
+        # multiple projects are also possible, each using different GHC version.
+        haskellProjects.default = {
+          # The base package set representing a specific GHC version.
+          # By default, this is pkgs.haskellPackages.
+          # You may also create your own. See https://community.flake.parts/haskell-flake/package-set
+          # basePackages = pkgs.haskellPackages;
 
-          # Extra package information. See https://community.flake.parts/haskell-flake/dependency
-          #
-          # Note that local packages are automatically included in `packages`
-          # (defined by `defaults.packages` option).
-          #
-          packages = {
-            # aeson.source = "1.5.0.0";      # Override aeson to a custom version from Hackage
-            # shower.source = inputs.shower; # Override shower to a custom source path
-          };
-          settings = {
-            #  aeson = {
-            #    check = false;
-            #  };
-            #  relude = {
-            #    haddock = false;
-            #    broken = false;
-            #  };
-          };
+          # Extra package information. See https://community.flake.parts/haskell-flake/dependency
+          #
+          # Note that local packages are automatically included in `packages`
+          # (defined by `defaults.packages` option).
+          #
+          packages = {
+            # aeson.source = "1.5.0.0";      # Override aeson to a custom version from Hackage
+            # shower.source = inputs.shower; # Override shower to a custom source path
+          };
+          settings = {
+            #  aeson = {
+            #    check = false;
+            #  };
+            #  relude = {
+            #    haddock = false;
+            #    broken = false;
+            #  };
+          };
 
-          devShell = {
-            # Enabled by default
-            # enable = true;
+          devShell = {
+            # Enabled by default
+            # enable = true;
 
-            # Programs you want to make available in the shell.
-            # Default programs can be disabled by setting to 'null'
-            # tools = hp: { fourmolu = hp.fourmolu; ghcid = null; };
+            # Programs you want to make available in the shell.
+            # Default programs can be disabled by setting to 'null'
+            # tools = hp: { fourmolu = hp.fourmolu; ghcid = null; };
 
-            # Check that haskell-language-server works
-            # hlsCheck.enable = true; # Requires sandbox to be disabled
-          };
-        };
+            # Check that haskell-language-server works
+            # hlsCheck.enable = true; # Requires sandbox to be disabled
+          };
+        };
 
-        # haskell-flake doesn't set the default package, but you can do it here.
-        packages.default = self'.packages.hello-flake-haskell;
-      };
-    };
-}
+        # haskell-flake doesn't set the default package, but you can do it here.
+        packages.default = self'.packages.hello-flake-haskell;
+      };
+    };
+}
 
@@ -3561,19 +3905,19 @@ Initialized empty Git repository in /home/amy/codeberg/nix-book/source/new-flake
hello.py
-
1
-2
-3
-4
-5
-6
-7
#!/usr/bin/env python
+
1
+2
+3
+4
+5
+6
+7
#!/usr/bin/env python
 
-def main():
-    print("Hello from inside a Python program built with a Nix flake!")
+def main():
+    print("Hello from inside a Python program built with a Nix flake!")
 
-if __name__ == "__main__":
-    main()
+if __name__ == "__main__":
+    main()
 
@@ -3648,29 +3992,29 @@ args.

setup.py
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
#!/usr/bin/env python
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
#!/usr/bin/env python
 
-from setuptools import setup
+from setuptools import setup
 
-setup(
-    name='hello-flake-python',
-    version='0.1.0',
-    py_modules=['hello'],
-    entry_points={
-        'console_scripts': ['hello-flake-python = hello:main']
-    },
-)
+setup(
+    name='hello-flake-python',
+    version='0.1.0',
+    py_modules=['hello'],
+    entry_points={
+        'console_scripts': ['hello-flake-python = hello:main']
+    },
+)
 
@@ -3802,101 +4146,101 @@ something like this.

flake.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
{
-  # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
-  # for a brief overview of what each section in a flake should or can contain.
+
 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
{
+  # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
+  # for a brief overview of what each section in a flake should or can contain.
 
-  description = "a very simple and friendly flake written in Python";
+  description = "a very simple and friendly flake written in Python";
 
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-        python = pkgs.python3;
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            packages = [
-              # Python plus helper tools
-              (python.withPackages (ps: with ps; [
-                virtualenv # Virtualenv
-                pip # The pip installer
-              ]))
-            ];
-          };
-        };
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+        python = pkgs.python3;
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            packages = [
+              # Python plus helper tools
+              (python.withPackages (ps: with ps; [
+                virtualenv # Virtualenv
+                pip # The pip installer
+              ]))
+            ];
+          };
+        };
 
-        packages = rec {
-          hello = python.pkgs.buildPythonApplication {
-            name = "hello-flake-python";
-            buildInputs = with python.pkgs; [ pip ];
-            src = ./.;
-            pyproject = true;
-            build-system = with python.pkgs; [ setuptools ];
-          };
-          default = hello;
-        };
+        packages = rec {
+          hello = python.pkgs.buildPythonApplication {
+            name = "hello-flake-python";
+            buildInputs = with python.pkgs; [ pip ];
+            src = ./.;
+            pyproject = true;
+            build-system = with python.pkgs; [ setuptools ];
+          };
+          default = hello;
+        };
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+        apps = rec {
+          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
+          default = hello;
+        };
+      }
+    );
+}
 
@@ -4159,13 +4503,13 @@ The second should declares the script interpreter and any dependencies.

Script
-
1
-2
-3
-4
#! /usr/bin/env nix
-#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
+
1
+2
+3
+4
#! /usr/bin/env nix
+#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
 hello
-cowsay "Pretty cool, huh?"
+cowsay "Pretty cool, huh?"
 
@@ -4193,10 +4537,10 @@ However, you can use any of the flake reference styles defined in
Script
-
1
-2
-3
#! /usr/bin/env nix
-#! nix shell git+https://codeberg.org/mhwombat/hello-flake --command bash
+
1
+2
+3
#! /usr/bin/env nix
+#! nix shell git+https://codeberg.org/mhwombat/hello-flake --command bash
 hello-flake
 
@@ -4220,42 +4564,45 @@ but you don’t want to bother writing a cabal file.

Script
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
#! /usr/bin/env nix-shell
-#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.containers])"
-#! nix-shell -i runghc
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
#! /usr/bin/env nix-shell
+#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.containers])"
+#! nix-shell -i runghc
 
-import Data.Map
+import Data.Map
 
-m :: Map String Int
-m = fromList [("cats", 3), ("dogs", 2)]
+m :: Map String Int
+m = fromList [("cats", 3), ("dogs", 2)]
 
-main :: IO ()
-main = do
-  let cats = findWithDefault 0 "cats" m
-  let dogs = findWithDefault 0 "dogs" m
-  let zebras = findWithDefault 0 "zebras" m
-  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
+main :: IO ()
+main = do
+  let cats = findWithDefault 0 "cats" m
+  let dogs = findWithDefault 0 "dogs" m
+  let zebras = findWithDefault 0 "zebras" m
+  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
 
Output
-
"I have 3 cats, 2 dogs, and 0 zebras."
+
this path will be fetched (141.61 MiB download, 1821.03 MiB unpacked):
+  /nix/store/w6awqpzhn1pmfzql7ba5ar8pvs0yq5s2-ghc-9.8.4
+copying path '/nix/store/w6awqpzhn1pmfzql7ba5ar8pvs0yq5s2-ghc-9.8.4' from 'https://cache.nixos.org'...
+"I have 3 cats, 2 dogs, and 0 zebras."
@@ -4271,27 +4618,27 @@ but you don’t want to bother configuring a builder.

Script
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
#! /usr/bin/env nix-shell
-#! nix-shell -i python3 -p python3Packages.html-sanitizer
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
#! /usr/bin/env nix-shell
+#! nix-shell -i python3 -p python3Packages.html-sanitizer
 
-from html_sanitizer import Sanitizer
-sanitizer = Sanitizer()  # default configuration
+from html_sanitizer import Sanitizer
+sanitizer = Sanitizer()  # default configuration
 
-original='<span style="font-weight:bold">some text</span>'
-print('original: ', original)
+original='<span style="font-weight:bold">some text</span>'
+print('original: ', original)
 
-sanitized=sanitizer.sanitize(original)
-print('sanitized: ', sanitized)
+sanitized=sanitizer.sanitize(original)
+print('sanitized: ', sanitized)
 
@@ -4311,45 +4658,45 @@ sanitized: <strong>some text</strong>
flake.nix
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            packages = [ pkgs.cowsay ];
-          };
-        };
-      }
-    );
-}
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            packages = [ pkgs.cowsay ];
+          };
+        };
+      }
+    );
+}
 
@@ -4382,47 +4729,47 @@ However, you can use any of the flake reference styles defined in
flake.nix
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-    hello-flake.url = "git+https://codeberg.org/mhwombat/hello-flake";
-  };
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+    hello-flake.url = "git+https://codeberg.org/mhwombat/hello-flake";
+  };
 
-  outputs = { self, nixpkgs, flake-utils, hello-flake }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            buildInputs = [ hello-flake.packages.${system}.hello ];
-          };
-        };
-      }
-    );
-}
+  outputs = { self, nixpkgs, flake-utils, hello-flake }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            buildInputs = [ hello-flake.packages.${system}.hello ];
+          };
+        };
+      }
+    );
+}
 
@@ -4503,47 +4850,47 @@ but you don’t want to bother writing a cabal file.

flake.nix
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-        customGhc = pkgs.haskellPackages.ghcWithPackages (p: with p; [ containers ]);
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-	          buildInputs = [ customGhc ];
-          };
-        };
-      }
-    );
-}
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+        customGhc = pkgs.haskellPackages.ghcWithPackages (p: with p; [ containers ]);
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+	          buildInputs = [ customGhc ];
+          };
+        };
+      }
+    );
+}
 
@@ -4553,27 +4900,27 @@ but you don’t want to bother writing a cabal file.

Main.hs
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
import Data.Map
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
import Data.Map
 
-m :: Map String Int
-m = fromList [("cats", 3), ("dogs", 2)]
+m :: Map String Int
+m = fromList [("cats", 3), ("dogs", 2)]
 
-main :: IO ()
-main = do
-  let cats = findWithDefault 0 "cats" m
-  let dogs = findWithDefault 0 "dogs" m
-  let zebras = findWithDefault 0 "zebras" m
-  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
+main :: IO ()
+main = do
+  let cats = findWithDefault 0 "cats" m
+  let dogs = findWithDefault 0 "dogs" m
+  let zebras = findWithDefault 0 "zebras" m
+  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
 
@@ -4598,71 +4945,71 @@ that are on my hard drive.

flake.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
{
-  description = "Pandoc build system for maths web";
+
 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
{
+  description = "Pandoc build system for maths web";
 
-  inputs = {
-    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
-    flake-parts.url = "github:hercules-ci/flake-parts";
-    haskell-flake.url = "github:srid/haskell-flake";
-    pandoc-linear-table.url = "/home/amy/github/pandoc-linear-table";
-    pandoc-logic-proof.url = "/home/amy/github/pandoc-logic-proof";
-    pandoc-columns.url = "/home/amy/github/pandoc-columns";
-    pandoc-query.url = "/home/amy/codeberg/pandoc-query";
-  };
-  outputs = inputs@{ self, nixpkgs, flake-parts, pandoc-linear-table, pandoc-logic-proof, pandoc-columns, pandoc-query, ... }:
-    flake-parts.lib.mkFlake { inherit inputs; } {
-      systems = nixpkgs.lib.systems.flakeExposed;
-      imports = [ inputs.haskell-flake.flakeModule ];
+  inputs = {
+    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
+    flake-parts.url = "github:hercules-ci/flake-parts";
+    haskell-flake.url = "github:srid/haskell-flake";
+    pandoc-linear-table.url = "/home/amy/github/pandoc-linear-table";
+    pandoc-logic-proof.url = "/home/amy/github/pandoc-logic-proof";
+    pandoc-columns.url = "/home/amy/github/pandoc-columns";
+    pandoc-query.url = "/home/amy/codeberg/pandoc-query";
+  };
+  outputs = inputs@{ self, nixpkgs, flake-parts, pandoc-linear-table, pandoc-logic-proof, pandoc-columns, pandoc-query, ... }:
+    flake-parts.lib.mkFlake { inherit inputs; } {
+      systems = nixpkgs.lib.systems.flakeExposed;
+      imports = [ inputs.haskell-flake.flakeModule ];
 
-      perSystem = { self', pkgs, ... }: {
-        haskellProjects.default = {
-	  # use my versions of some Haskell pagkages instead of the nixpkgs versions
-	  packages = {
-	    pandoc-linear-table.source = inputs.pandoc-linear-table;
-	    pandoc-logic-proof.source = inputs.pandoc-logic-proof;
-	    pandoc-columns.source = inputs.pandoc-columns;
-	    pandoc-query.source = inputs.pandoc-query;
-	  };
-	};
+      perSystem = { self', pkgs, ... }: {
+        haskellProjects.default = {
+	  # use my versions of some Haskell pagkages instead of the nixpkgs versions
+	  packages = {
+	    pandoc-linear-table.source = inputs.pandoc-linear-table;
+	    pandoc-logic-proof.source = inputs.pandoc-logic-proof;
+	    pandoc-columns.source = inputs.pandoc-columns;
+	    pandoc-query.source = inputs.pandoc-query;
+	  };
+	};
 
-        # haskell-flake doesn't set the default package, but you can do it here.
-        packages.default = self'.packages.pandoc-maths-web;
-      };
-    };
-}
+        # haskell-flake doesn't set the default package, but you can do it here.
+        packages.default = self'.packages.pandoc-maths-web;
+      };
+    };
+}
 
@@ -4675,49 +5022,49 @@ that are on my hard drive.

flake.nix
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
-22
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            shellHook = ''
-              export FOO="bar"
-            '';
-          };
-        };
-      }
-    );
-}
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            shellHook = ''
+              export FOO="bar"
+            '';
+          };
+        };
+      }
+    );
+}
 
@@ -4751,7 +5098,7 @@ If instead it requires <nixpkgs>, it is not pure and we canno
-
with (import <nixpkgs> {});
+
with (import <nixpkgs> {});
@@ -4763,7 +5110,7 @@ Instead, we have to write our own derivation (see Sec
-
{ pkgs ? import <nixpkgs> {} }:
+
{ pkgs ? import <nixpkgs> {} }:
@@ -4776,59 +5123,59 @@ We can use it directly to build hello-nix (see
flake.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
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-    hello-nix = {
-      url = "git+https://codeberg.org/mhwombat/hello-nix";
-      flake = false;
-    };
-  };
+
 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
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+    hello-nix = {
+      url = "git+https://codeberg.org/mhwombat/hello-nix";
+      flake = false;
+    };
+  };
 
-  outputs = { self, nixpkgs, flake-utils, hello-nix }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs {
-          inherit system;
-        };
-        helloNix = import hello-nix { inherit pkgs; };
-      in
-      {
-        devShells = rec {
-          default = pkgs.mkShell {
-            packages = [ helloNix ];
-          };
-        };
-      }
-    );
-}
+  outputs = { self, nixpkgs, flake-utils, hello-nix }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs {
+          inherit system;
+        };
+        helloNix = import hello-nix { inherit pkgs; };
+      in
+      {
+        devShells = rec {
+          default = pkgs.mkShell {
+            packages = [ helloNix ];
+          };
+        };
+      }
+    );
+}
 
@@ -4859,16 +5206,16 @@ Line 15 should be replaced with:

-
       helloNix = pkgs.stdenv.mkDerivation {
-         name = "hello-nix";
-         src = hello-nix;
-         installPhase =
-           ''
-             mkdir -p $out/bin
-             cp $src/hello-nix $out/bin/hello-nix
-             chmod +x $out/bin/hello-nix
-           '';
-       };
+
       helloNix = pkgs.stdenv.mkDerivation {
+         name = "hello-nix";
+         src = hello-nix;
+         installPhase =
+           ''
+             mkdir -p $out/bin
+             cp $src/hello-nix $out/bin/hello-nix
+             chmod +x $out/bin/hello-nix
+           '';
+       };
@@ -4884,94 +5231,98 @@ Line 15 should be replaced with:

hello-with-cow
-
1
-2
-3
#!/usr/bin/env sh
+
1
+2
+3
#!/usr/bin/env sh
 
-cowsay "Hello from your flake!"
+cowsay "Hello from your flake!"
 
flake.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
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-  };
+
 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
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
 
-  outputs = { self, nixpkgs, flake-utils }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs { inherit system; };
-      in
-      {
-        packages = rec {
-          hello = pkgs.stdenv.mkDerivation rec {
-            name = "hello-with-cow";
+  outputs = { self, nixpkgs, flake-utils }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+      in
+      {
+        packages = rec {
+          hello = pkgs.stdenv.mkDerivation rec {
+            name = "hello-with-cow";
 
-            src = ./.;
+            src = ./.;
 
-            unpackPhase = "true";
+            unpackPhase = "true";
 
-            buildPhase = ":";
+            buildPhase = ":";
 
-            installPhase =
-              ''
-                mkdir -p $out/bin
-                cp $src/hello-with-cow $out/bin
-                chmod +x $out/bin/hello-with-cow
-              '';
-          };
-          default = hello;
-        };
+            installPhase =
+              ''
+                mkdir -p $out/bin
+                cp $src/hello-with-cow $out/bin
+                chmod +x $out/bin/hello-with-cow
+              '';
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+            buildInputs = [ pkgs.cowsay ];
+          };
+          default = hello;
+        };
+
+        apps = rec {
+          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
+          default = hello;
+        };
+      }
+    );
+}
 
@@ -4982,9 +5333,9 @@ cowsay "He
$ nix run
 this derivation will be built:
-  /nix/store/bqk0dmm8ggj8q8d056q9bw9fdw5zw8l9-hello-with-cow.drv
-building '/nix/store/bqk0dmm8ggj8q8d056q9bw9fdw5zw8l9-hello-with-cow.drv'...
-/nix/store/xhv9yjsil2wsrnpbx07dlw3yb7hbr7fq-hello-with-cow/bin/hello-with-cow: line 3: cowsay: command not found
+ /nix/store/rlg4ym95l3jh9mv2mc1yd6qf0i2px844-hello-with-cow.drv +building '/nix/store/rlg4ym95l3jh9mv2mc1yd6qf0i2px844-hello-with-cow.drv'... +/nix/store/ps5mk6rvk7917f6ls58hz0ifs3580mq0-hello-with-cow/bin/hello-with-cow: line 3: cowsay: command not found
@@ -5015,12 +5366,12 @@ All it does is invoke hello-nix, which is the input we added
hello-again
-
1
-2
-3
-4
#!/usr/bin/env sh
+
1
+2
+3
+4
#!/usr/bin/env sh
 
-echo "I'm a flake, but I'm running a command defined in a non-flake package."
+echo "I'm a flake, but I'm running a command defined in a non-flake package."
 hello-nix
 
@@ -5028,111 +5379,111 @@ hello-nix
flake.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
{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs";
-    flake-utils.url = "github:numtide/flake-utils";
-    hello-nix = {
-      url = "git+https://codeberg.org/mhwombat/hello-nix";
-      flake = false;
-    };
-  };
+
 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
{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs";
+    flake-utils.url = "github:numtide/flake-utils";
+    hello-nix = {
+      url = "git+https://codeberg.org/mhwombat/hello-nix";
+      flake = false;
+    };
+  };
 
-  outputs = { self, nixpkgs, flake-utils, hello-nix }:
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = import nixpkgs {
-          inherit system;
-        };
-        helloNix = import hello-nix { inherit pkgs; };
-      in
-      {
-        packages = rec {
-          hello = pkgs.stdenv.mkDerivation rec {
-            name = "hello-again";
+  outputs = { self, nixpkgs, flake-utils, hello-nix }:
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs {
+          inherit system;
+        };
+        helloNix = import hello-nix { inherit pkgs; };
+      in
+      {
+        packages = rec {
+          hello = pkgs.stdenv.mkDerivation rec {
+            name = "hello-again";
 
-            src = ./.;
+            src = ./.;
 
-            unpackPhase = "true";
+            unpackPhase = "true";
 
-            buildPhase = ":";
+            buildPhase = ":";
 
-            installPhase =
-              ''
-                mkdir -p $out/bin
-                cp $src/hello-again $out/bin
-                chmod +x $out/bin/hello-again
+            installPhase =
+              ''
+                mkdir -p $out/bin
+                cp $src/hello-again $out/bin
+                chmod +x $out/bin/hello-again
 
-                # modify the hello-again script so it can find hello-nix
-                HELLO=$(type -p hello-nix)
-                sed "s_hello-nix_"$HELLO"_" --in-place $out/bin/hello-again
-              '';
+                # modify the hello-again script so it can find hello-nix
+                HELLO=$(type -p hello-nix)
+                sed "s_hello-nix_"$HELLO"_" --in-place $out/bin/hello-again
+              '';
 
 
-            buildInputs = [ helloNix ];
-          };
-          default = hello;
-        };
+            buildInputs = [ helloNix ];
+          };
+          default = hello;
+        };
 
-        apps = rec {
-          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
-          default = hello;
-        };
-      }
-    );
-}
+        apps = rec {
+          hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
+          default = hello;
+        };
+      }
+    );
+}
 
@@ -5175,27 +5526,27 @@ Here’s how.

shell.nix
-
 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
with (import <nixpkgs> {});
-let
-   hello-flake = ( builtins.getFlake
-                     git+https://codeberg.org/mhwombat/hello-flake?ref=main&rev=3aa43dbe7be878dde7b2bdcbe992fe1705da3150
-		 ).packages.${builtins.currentSystem}.default;
-in
-mkShell {
-  buildInputs = [
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
with (import <nixpkgs> {});
+let
+   hello-flake = ( builtins.getFlake
+                     git+https://codeberg.org/mhwombat/hello-flake?ref=main&rev=3aa43dbe7be878dde7b2bdcbe992fe1705da3150
+		 ).packages.${builtins.currentSystem}.default;
+in
+mkShell {
+  buildInputs = [
     hello-flake
-  ];
-}
+  ];
+}
 
@@ -5224,7 +5575,7 @@ Hello from your flake! diff --git a/shell.nix b/shell.nix index a21f67f..d61a5ae 100644 --- a/shell.nix +++ b/shell.nix @@ -10,8 +10,8 @@ mkShell { # python311Packages.pygments don't need to install, asciidoctor includes it rubyPackages.rbs rubyPackages.racc - # rubyPackages.coderay - # rubyPackages.rouge + rubyPackages.coderay + rubyPackages.rouge ]; # outputs = [ "start-shell" ]; } diff --git a/source/book.adoc b/source/book.adoc index 66be26f..81a93d8 100644 --- a/source/book.adoc +++ b/source/book.adoc @@ -4,10 +4,12 @@ :sectnums: :toc: left :toclevels: 4 +// :source-highlighter: rouge +// :rouge-style: monokai :source-highlighter: pygments :pygments-style: default :pygments-linenums-mode: table -:pygments-css: style +:pygments-css: class :!prewrap: :icons: font :title-logo-image: image::wombat.svg["wombat reclining against a lambda"] diff --git a/source/flake-structure/main.adoc b/source/flake-structure/main.adoc index 8acb1fa..80e229b 100644 --- a/source/flake-structure/main.adoc +++ b/source/flake-structure/main.adoc @@ -12,8 +12,6 @@ The basic structure of a flake is shown below. } .... -== Description - The `description` part is self-explanatory; it's just a string. You probably won't need `nixConfig` unless you're doing something fancy. I'm going to focus on what goes into the `inputs` and `outputs` sections, diff --git a/source/hello-flake-repo/main.adoc0 b/source/hello-flake-repo/main.adoc0 index 12d457c..59959d1 100644 --- a/source/hello-flake-repo/main.adoc0 +++ b/source/hello-flake-repo/main.adoc0 @@ -65,7 +65,7 @@ _depends_ on `nixpkgs` and `flake-utils`. Finally, let's look at `flake.lock`, or rather, just part of it. -[source,linenums] +[source,,linenums] .flake.lock .... $# head -n 40 flake.lock diff --git a/source/hello-flake-return/main.adoc b/source/hello-flake-return/main.adoc index 60d016b..a18de96 100644 --- a/source/hello-flake-return/main.adoc +++ b/source/hello-flake-return/main.adoc @@ -5,7 +5,7 @@ let's have a look at the one we saw earlier, in the `hello-flake` repo. If you compare this flake definition to the colour-coded template presented in the previous section, most of it should look familiar. -[subs=quotes] +[source,nix,linenums] .flake.nix .... { @@ -48,7 +48,7 @@ additional development tools. Now let's look at the section I labeled `SOME UNFAMILIAR STUFF` and see what it does. -[subs=quotes] +[source,nix,linenums,subs=quotes] .... packages = rec { hello = pkgs.stdenv.mkDerivation rec { ❶ diff --git a/source/modify-hello-flake/flake.nix.new b/source/modify-hello-flake/flake.nix.new index 59932cd..678eac5 100644 --- a/source/modify-hello-flake/flake.nix.new +++ b/source/modify-hello-flake/flake.nix.new @@ -1,7 +1,4 @@ { - # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md - # for a brief overview of what each section in a flake should or can contain. - description = "a very simple and friendly flake"; inputs = { @@ -37,6 +34,8 @@ cp $src/hello-flake $out/bin/hello-flake chmod +x $out/bin/hello-flake ''; + + buildInputs = [ pkgs.cowsay ]; }; default = hello; }; diff --git a/source/modify-hello-flake/main.adoc0 b/source/modify-hello-flake/main.adoc0 index 4d88d77..776ff7e 100644 --- a/source/modify-hello-flake/main.adoc0 +++ b/source/modify-hello-flake/main.adoc0 @@ -90,7 +90,7 @@ Here's the modified `hello-flake` file. $ sed -i 's/echo/cowsay/' hello-flake //// -[source,nix,highlight=4] +[source,nix,linenums,highlight=3] .hello-flake .... $# cat hello-flake @@ -118,26 +118,63 @@ The highlighted modifications below will accomplish that. $ cp ../flake.nix.new flake.nix //// -[source,nix,highlight=18..22] +[source,nix,linenums,highlight=15..19] .flake.nix .... $# cat flake.nix .... -Now we restart the development shell and see that the `cowsay` command is -available and the script works. Because we've updated source files -but haven't ``git commit``ed the new version, we get a warning message -about it being "`dirty`". It's just a warning, though; the script runs -correctly. +Now we restart the development shell and see that the `cowsay` command is available. +Because we've updated source files but haven't ``git commit``ed the new version, +we get a warning message about it being "`dirty`". +It's just a warning, though; the script runs correctly. .... $# echo '$ nix develop' $# nix develop --command sh $ which cowsay # is it available now? $ ./hello-flake +$ exit .... -Alternatively, we could use `nix run`. +Let's try `nix run`. + +.... +$ nix run +.... + +What went wrong? +Recall that we only added `cowsay` to the development environment; +we didn't add it to the runtime environment. +The modified flake below will fix that. + +//// +$ cp ../flake.nix.new.2 flake.nix +//// + +[source,nix,linenums,highlight='37..39,42'] +.flake.nix +.... +$# cat flake.nix +.... + +Line 42 adds `cowsay` as a `buildInput` for the package. +That does make it available at build and runtime, but it doesn’t put it on the path, +so our hello-flake script wouldn’t be able to find it. +There are various ways to deal with this problem. +In this case we simply edited the script (lines 37-39) as we install it, +specifying the full path to cowsay. + +[NOTE] +==== +When you're packaging a program written in a more powerful language such as +Haskell, Python, Java, C, C#, or Rust, +the language build system will usually do all that is required +to make the dependencies visible to the program at runtime. +In this case, adding the dependency to `buildInputs` is sufficient. +==== + +This time `nix run` works. .... $ nix run diff --git a/source/nix-language/strings.adoc b/source/nix-language/strings.adoc index 8341d50..5fa4de0 100644 --- a/source/nix-language/strings.adoc +++ b/source/nix-language/strings.adoc @@ -40,7 +40,8 @@ error: error: cannot coerce an integer to a string .... -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 +<<#convert-to-string,next section>>. ==== == Useful built-in functions for strings @@ -67,6 +68,7 @@ nix-repl> builtins.substring 3 6 "hayneedlestack" "needle" .... +[#convert-to-string] Convert an expression to a string. [source] diff --git a/source/recipes/build/nixpkg/tempwork/flake.lock b/source/recipes/build/nixpkg/tempwork/flake.lock deleted file mode 100644 index 98266aa..0000000 --- a/source/recipes/build/nixpkg/tempwork/flake.lock +++ /dev/null @@ -1,60 +0,0 @@ -{ - "nodes": { - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1731533236, - "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1757775813, - "narHash": "sha256-mHYalJeeuYTtJNPijlm4IqmxslIB+DTz/wQvz2EB/S4=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "1fb62b80407f458b29ba8168c11095858a250687", - "type": "github" - }, - "original": { - "owner": "NixOS", - "repo": "nixpkgs", - "type": "github" - } - }, - "root": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - } - }, - "root": "root", - "version": 7 -} diff --git a/source/recipes/build/nixpkg/tempwork/flake.nix b/source/recipes/build/nixpkg/tempwork/flake.nix deleted file mode 100644 index a7cdcc9..0000000 --- a/source/recipes/build/nixpkg/tempwork/flake.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs"; - flake-utils.url = "github:numtide/flake-utils"; - }; - - outputs = { self, nixpkgs, flake-utils }: - flake-utils.lib.eachDefaultSystem (system: - let - pkgs = import nixpkgs { inherit system; }; - in - { - packages = rec { - hello = pkgs.stdenv.mkDerivation rec { - name = "hello-with-cow"; - - src = ./.; - - unpackPhase = "true"; - - buildPhase = ":"; - - installPhase = - '' - mkdir -p $out/bin - cp $src/hello-with-cow $out/bin - chmod +x $out/bin/hello-with-cow - ''; - - buildInputs = [ pkgs.cowsay ]; - }; - default = hello; - }; - - apps = rec { - hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; - default = hello; - }; - } - ); -} diff --git a/source/recipes/build/nixpkg/tempwork/hello-with-cow b/source/recipes/build/nixpkg/tempwork/hello-with-cow deleted file mode 100644 index 82caecc..0000000 --- a/source/recipes/build/nixpkg/tempwork/hello-with-cow +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env sh - -cowsay "Hello from your flake!" diff --git a/source/recipes/devshell/flake/main.adoc0 b/source/recipes/devshell/flake/main.adoc0 index 671adf5..75d94ea 100644 --- a/source/recipes/devshell/flake/main.adoc0 +++ b/source/recipes/devshell/flake/main.adoc0 @@ -19,7 +19,7 @@ However, you can use any of the flake reference styles defined in <<#flakeref>>. $# cat flake.nix .... -Let's take a closer look at the `buildInputs`. +Let's take a closer look at the `buildInputs` from line 16. .... $# grep buildInputs flake.nix | sed 's/ //g' diff --git a/source/recipes/devshell/flake/tempwork/flake.lock b/source/recipes/devshell/flake/tempwork/flake.lock new file mode 100644 index 0000000..481ebd5 --- /dev/null +++ b/source/recipes/devshell/flake/tempwork/flake.lock @@ -0,0 +1,128 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "hello-flake": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1684524031, + "narHash": "sha256-0DYX7S7u5S2no2e6jiIv7cRfbpM5JBJq7+/1CUKEGxA=", + "ref": "refs/heads/main", + "rev": "099bfadc43f79a8929809bd097d12ef5de73ef62", + "revCount": 24, + "type": "git", + "url": "https://codeberg.org/mhwombat/hello-flake" + }, + "original": { + "type": "git", + "url": "https://codeberg.org/mhwombat/hello-flake" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1681665000, + "narHash": "sha256-hDGTR59wC3qrQZFxVi2U3vTY+r02+Okbq080hO1C4Nk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3a6205d9f79fe526be03d8c465403b118ca4cf37", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1757790242, + "narHash": "sha256-53ib4nvwVVAYF1ipe7BBEwjvvBo0lEbQcsbCHPSfzAg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "38cbd6b3be3ce84eb389eabb552dff8824019d2b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "hello-flake": "hello-flake", + "nixpkgs": "nixpkgs_2" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/source/recipes/devshell/flake/tempwork/flake.nix b/source/recipes/devshell/flake/tempwork/flake.nix new file mode 100644 index 0000000..0db427b --- /dev/null +++ b/source/recipes/devshell/flake/tempwork/flake.nix @@ -0,0 +1,21 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + hello-flake.url = "git+https://codeberg.org/mhwombat/hello-flake"; + }; + + outputs = { self, nixpkgs, flake-utils, hello-flake }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + devShells = rec { + default = pkgs.mkShell { + buildInputs = [ hello-flake.packages.${system}.hello ]; + }; + }; + } + ); +}