From 00bfdf2c0f20465dc8ec6a915dcdc8ea2df2427e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 14 Sep 2025 20:49:57 +0100 Subject: [PATCH] temp --- index.html | 161 ++++++------------ source/hello-flake-repo/main.adoc0 | 2 +- source/hello-flake-return/main.adoc | 8 +- source/hello-flake/main.adoc0 | 9 +- source/modify-hello-flake/main.adoc0 | 6 +- source/new-flake/haskell-flake/main.adoc0 | 10 +- source/new-flake/python-flake/main.adoc0 | 8 +- source/recipes/devshell/nixpkg/main.adoc0 | 2 +- .../devshell/nixpkg/tempwork/flake.lock | 60 +++++++ .../devshell/nixpkg/tempwork/flake.nix | 20 +++ .../recipes/shebang/haskell-nixpkg/main.adoc0 | 2 +- .../shebang/haskell-nixpkg/my-script.sh | 13 +- 12 files changed, 161 insertions(+), 140 deletions(-) create mode 100644 source/recipes/devshell/nixpkg/tempwork/flake.lock create mode 100644 source/recipes/devshell/nixpkg/tempwork/flake.nix diff --git a/index.html b/index.html index 050597c..3b49df7 100644 --- a/index.html +++ b/index.html @@ -840,7 +840,8 @@ a path

If an expression can be interpreted as a path, Nix will do so. -This is useful, because paths are far more commonly used in Nix expressions that arithmetic operators. +This is useful, because paths are far more commonly used in Nix expressions +than arithmetic operators. In this case, Nix interpreted 6/2 as a relative path from the current directory, which in the above example was /home/amy/codeberg/nix-book.

@@ -1617,9 +1618,9 @@ This is an important topic, so we will cover it separately in +
-
{ _name1_, _name2_, ... }
+
{ name1, name2, ... }
@@ -1858,9 +1859,10 @@ using the attribute selection operator (.).

Before learning to write Nix flakes, let’s learn how to use them. I’ve -created a simple example of a flake in this git -repository. To run this -flake, you don’t need to install anything; simply run the command below. +created a simple example of a flake in this git repository: +https://codeberg.org/mhwombat/hello-flake. +To run this flake, you don’t need to install anything; +simply run the command below. The first time you use a flake, Nix has to fetch and build it, which may take time. Subsequent invocations should be instantaneous.

@@ -1909,8 +1911,8 @@ directly available.

$ exit
-$ hello-flake
-sh: line 3: hello-flake: command not found
+$ hello-flake # Won't work outside development shell +bash: line 24: hello-flake: command not found
@@ -2028,13 +2030,7 @@ is always called flake.nix.

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.
-
+41
{
   description = "a very simple and friendly flake";
 
   inputs = {
@@ -2081,7 +2077,7 @@ is always called flake.nix.

If this is your first time seeing a flake definition, it probably looks intimidating. -Flakes are written in the Nix language, introduced in an earlier chapter. +Flakes are written in the Nix language, introduced in [_the_nix_language_]. However, you don’t really need to know Nix to follow this example. For now, I’d like to focus on the inputs section.

@@ -2154,11 +2150,11 @@ flakes. The important thing to note is that the hello-flake package "systems": "systems" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -2169,11 +2165,11 @@ flakes. The important thing to note is that the hello-flake package }, "nixpkgs": { "locked": { - "lastModified": 1681665000, - "narHash": "sha256-hDGTR59wC3qrQZFxVi2U3vTY+r02+Okbq080hO1C4Nk=", + "lastModified": 1757873102, + "narHash": "sha256-kYhNxLlYyJcUouNRazBufVfBInMWMyF+44xG/xar2yE=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "3a6205d9f79fe526be03d8c465403b118ca4cf37", + "rev": "88cef159e47c0dc56f151593e044453a39a6e547", "type": "github" }, "original": { @@ -2509,41 +2505,12 @@ standard environment version), developPackage, callCabal2Nix<

Now that we have a better understanding of the structure of flake.nix, 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.

+presented in Chapter 6, A generic flake, most of it should look familiar.

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
{
+
{
   description = "a very simple and friendly flake";
 
   inputs = {
@@ -2572,8 +2539,7 @@ presented in the previous section, most of it should look familiar.

}; } ); -} -
+}
@@ -2758,7 +2724,7 @@ so can’t we just run it?

$ hello-flake
-Hello from your flake!
+bash: line 16: hello-flake: command not found
@@ -2860,7 +2826,7 @@ Here’s the modified hello-flake file.

-
$ ./hello-flake
+
$ ./hello-flake     # Won't work
 ./hello-flake: line 3: cowsay: command not found
@@ -2991,7 +2957,7 @@ It’s just a warning, though; the script runs correctly.

$ nix develop
 warning: Git tree '/home/amy/codeberg/nix-book/source/modify-hello-flake/hello-flake' is dirty
 $ which cowsay # is it available now?
-/nix/store/gfi27h4y5n4aralcxrc0377p8mjb1cvb-cowsay-3.7.0/bin/cowsay
+/nix/store/y5kz17amqc5gn1yvga9wj3vsjz8s1f0c-cowsay-3.8.4/bin/cowsay
 $ ./hello-flake
  ________________________
 < Hello from your flake! >
@@ -3009,9 +2975,9 @@ $ exit
-
$ nix run
+
$ nix run     # Won't work
 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
+/nix/store/rnjqkk7a8sz6wrm1qb1hxssy7qlgaa85-hello-flake/bin/hello-flake: line 3: cowsay: command not found
@@ -3151,7 +3117,7 @@ specifying the full path to cowsay.

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.

+In that case, adding the dependency to buildInputs is sufficient.

@@ -3182,7 +3148,7 @@ we’ll see next.

$ cat result/bin/hello-flake
-#!/nix/store/zlf0f88vj30sc7567b80l52d19pbdmy2-bash-5.2-p15/bin/sh
+#!/nix/store/q7sqwn7i6w2b67adw0bmix29pxg85x3w-bash-5.3p3/bin/sh
 
 echo "Hello from your flake!"
@@ -3195,9 +3161,9 @@ echo "Hello from your flake!"
$ nix build
 warning: Git tree '/home/amy/codeberg/nix-book/source/modify-hello-flake/hello-flake' is dirty
 $ cat result/bin/hello-flake
-#!/nix/store/zlf0f88vj30sc7567b80l52d19pbdmy2-bash-5.2-p15/bin/sh
+#!/nix/store/q7sqwn7i6w2b67adw0bmix29pxg85x3w-bash-5.3p3/bin/sh
 
-/nix/store/gfi27h4y5n4aralcxrc0377p8mjb1cvb-cowsay-3.7.0/bin/cowsay "Hello from your flake!"
+/nix/store/y5kz17amqc5gn1yvga9wj3vsjz8s1f0c-cowsay-3.8.4/bin/cowsay "Hello from your flake!"
@@ -3207,8 +3173,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 6523897] added bovine feature
- 2 files changed, 13 insertions(+), 4 deletions(-)
+[main a10700e] added bovine feature
+ 2 files changed, 13 insertions(+), 1 deletion(-)
 $ nix run
  ________________________
 < Hello from your flake! >
@@ -3416,7 +3382,7 @@ You can come back to this section later to learn more.

$ nix shell nixpkgs#ghc
-$ runghc Main.hs
+$ runghc Main.hs     # Won't work
 
 Main.hs:1:1: error: [GHC-87110]
     Could not find module ‘Network.HostName’.
@@ -3435,7 +3401,7 @@ Let’s exit that shell and try again, this time adding the hostname
 
$ exit
 $ nix shell nixpkgs#ghc nixpkgs#hostname
-$ runghc Main.hs
+$ runghc Main.hs     # Won't work
 
 Main.hs:1:1: error: [GHC-87110]
     Could not find module ‘Network.HostName’.
@@ -3458,7 +3424,7 @@ The package we want is in the package set called haskellPackages<
 
$ exit
 $ nix shell nixpkgs#ghc nixpkgs#haskellPackages.hostname
-$ runghc Main.hs
+$ runghc Main.hs     # Won't work
 
 Main.hs:1:1: error: [GHC-87110]
     Could not find module ‘Network.HostName’.
@@ -3496,11 +3462,6 @@ Your hostname is: wombat11k

Success! Now we know the program works.

-
-
-
$ exit
-
-
@@ -3600,7 +3561,7 @@ First we’ll try building the package manually.
$ cabal build
-sh: line 46: cabal: command not found
+sh: line 42: cabal: command not found
@@ -3808,9 +3769,9 @@ warning: creating lock file '"/home/amy/codeberg/nix-book/source/new-flake/haske • Added input 'flake-parts/nixpkgs-lib': 'github:nix-community/nixpkgs.lib/a73b9c743612e4244d865a2fdee11865283c04e6?narHash=sha256-x2rJ%2BOvzq0sCMpgfgGaaqgBSwY%2BLST%2BWbZ6TytnT9Rk%3D' (2025-08-10) • Added input 'haskell-flake': - 'github:srid/haskell-flake/73e3891fb135c679a1c30fae4b101e5b41b8ca61?narHash=sha256-%2B99fEAk0HwjYgIW2tEOs7ayBDxnU9NAM5E29ZxgyX40%3D' (2025-08-31) + 'github:srid/haskell-flake/2acbf1c9bb7aac38b59dcc3c1bd78911cf37e42c?narHash=sha256-jwZT6Ns4BXlrFHaXjZgZwfhSaw8nTCTKZ8fCDylrONM%3D' (2025-09-14) • Added input 'nixpkgs': - 'github:nixos/nixpkgs/c6a788f552b7b7af703b1a29802a7233c0067908?narHash=sha256-6n/n1GZQ/vi%2BLhFXMSyoseKdNfc2QQaSBXJdgamrbkE%3D' (2025-09-03) + 'github:nixos/nixpkgs/6d7ec06d6868ac6d94c371458fc2391ded9ff13d?narHash=sha256-fEvTiU4s9lWgW7mYEU/1QUPirgkn%2BodUBTaindgiziY%3D' (2025-09-13) warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/haskell-flake/hello-haskell' is dirty
@@ -3847,7 +3808,7 @@ We need to add this to the repo, and commit all important files.

$ git add flake.lock
 $ git commit -a -m 'initial commit'
-[master (root-commit) d1f6c9b] initial commit
+[master (root-commit) d35f8fd] initial commit
  5 files changed, 170 insertions(+)
  create mode 100644 LICENSE
  create mode 100644 Main.hs
@@ -4049,7 +4010,7 @@ First we’ll try building the package manually.
 
-
$ python -m build
+
$ python -m build     # Won't work
 /nix/store/iyff8129iampdw13nlfqalzhxy8y1hi9-python3-3.13.6/bin/python: No module named build
@@ -4252,7 +4213,7 @@ something like this.

-
$ nix build
+
$ nix build     # Won't work
 error: Path 'flake.nix' in the repository "/home/amy/codeberg/nix-book/source/new-flake/python-flake/hello-python" is not tracked by Git.
 
        To make it visible to Nix, run:
@@ -4261,8 +4222,8 @@ error: Path 'flake.nix' in the repository "/home/amy/codeberg/nix-book/source/ne
 
-

Why can’t it find flake.nix? Nix flakes only “see” files that are -part of the repository. We need to add all of the important files to the +

Nix flakes only “see” files that are part of the repository. +We need to add all of the important files to the repo before building or running the flake.

@@ -4276,7 +4237,7 @@ warning: creating lock file '"/home/amy/codeberg/nix-book/source/new-flake/pytho • Added input 'flake-utils/systems': 'github:nix-systems/default/da67096a3b9bf56a91d16901293e51ba5b49a27e?narHash=sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768%3D' (2023-04-09) • Added input 'nixpkgs': - 'github:NixOS/nixpkgs/9aaf71a4a74d835ec05ac3ddf5fc1b859e005c41?narHash=sha256-%2B0iaqsEjfaIZMuRNGO%2BwC0uVazT5b%2BW38wB7Uv0kA5Y%3D' (2025-09-06) + 'github:NixOS/nixpkgs/5ad881b15fb2b63c2f127533ca59f840fcc24c3f?narHash=sha256-P8XdKg87fR5PpCjIgMH%2B05PDwhdmlNrigsvChdHfyaw%3D' (2025-09-14) warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/python-flake/hello-python' is dirty
@@ -4312,7 +4273,7 @@ We need to add this to the repo, and commit all important files.

$ git add flake.lock
 $ git commit -a -m 'initial commit'
-[master (root-commit) 887d465] initial commit
+[master (root-commit) 2af9bd5] initial commit
  4 files changed, 127 insertions(+)
  create mode 100644 flake.lock
  create mode 100644 flake.nix
@@ -4559,7 +4520,7 @@ However, you can use any of the flake reference styles defined in 
-

Example: Access the containers package from the haskellPackages set in the nixpkgs repo.

+

Example: Access the extra package from the haskellPackages set in the nixpkgs repo.

Script
@@ -4573,36 +4534,24 @@ but you don’t want to bother writing a cabal file.

7 8 9 -10 -11 -12 -13 -14 -15
#! /usr/bin/env nix-shell
-#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.containers])"
+10
#! /usr/bin/env nix-shell
+#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.extra])"
 #! nix-shell -i runghc
 
-import Data.Map
-
-m :: Map String Int
-m = fromList [("cats", 3), ("dogs", 2)]
+import Data.List.Extra
 
 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."
+  print $ lower "ABCDE"
+  print $ upper "XYZ"
 
Output
-
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."
+
"abcde"
+"XYZ"
@@ -4855,7 +4804,7 @@ In this example, we will access the extra package from the ha
@@ -5649,7 +5598,7 @@ Hello from your flake! diff --git a/source/hello-flake-repo/main.adoc0 b/source/hello-flake-repo/main.adoc0 index 59959d1..684c189 100644 --- a/source/hello-flake-repo/main.adoc0 +++ b/source/hello-flake-repo/main.adoc0 @@ -44,7 +44,7 @@ $# cat flake.nix If this is your first time seeing a flake definition, it probably looks intimidating. -Flakes are written in the Nix language, introduced in an earlier chapter. +Flakes are written in the Nix language, introduced in <<_the_nix_language_>>. However, you don't really need to know Nix to follow this example. For now, I'd like to focus on the inputs section. diff --git a/source/hello-flake-return/main.adoc b/source/hello-flake-return/main.adoc index a18de96..362d43d 100644 --- a/source/hello-flake-return/main.adoc +++ b/source/hello-flake-return/main.adoc @@ -3,9 +3,10 @@ Now that we have a better understanding of the structure of `flake.nix`, 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. +presented in <<_a_generic_flake>>, most of it should look familiar. -[source,nix,linenums] +// don't use linenums because part of the file is omitted +[source,nix] .flake.nix .... { @@ -48,7 +49,8 @@ additional development tools. Now let's look at the section I labeled `SOME UNFAMILIAR STUFF` and see what it does. -[source,nix,linenums,subs=quotes] +// don't use linenums because part of the file is omitted +[source,nix,subs=quotes] .... packages = rec { hello = pkgs.stdenv.mkDerivation rec { ❶ diff --git a/source/hello-flake/main.adoc0 b/source/hello-flake/main.adoc0 index 718bcb4..df77b50 100644 --- a/source/hello-flake/main.adoc0 +++ b/source/hello-flake/main.adoc0 @@ -1,11 +1,10 @@ = Hello, flake! -// TODO Need an intro explaining what flakes are. Move the "why flakes" section here. - Before learning to write Nix flakes, let's learn how to use them. I've -created a simple example of a flake in this git -https://codeberg.org/mhwombat/hello-flake[repository]. To run this -flake, you don't need to install anything; simply run the command below. +created a simple example of a flake in this git repository: +https://codeberg.org/mhwombat/hello-flake. +To run this flake, you don't need to install anything; +simply run the command below. The first time you use a flake, Nix has to fetch and build it, which may take time. Subsequent invocations should be instantaneous. diff --git a/source/modify-hello-flake/main.adoc0 b/source/modify-hello-flake/main.adoc0 index 776ff7e..a2c7477 100644 --- a/source/modify-hello-flake/main.adoc0 +++ b/source/modify-hello-flake/main.adoc0 @@ -99,7 +99,7 @@ $# cat hello-flake Let's test the modified script. .... -$ ./hello-flake +$ ./hello-flake # Won't work .... What went wrong? Remember that we are in a _development_ shell. Since @@ -140,7 +140,7 @@ $ exit Let's try `nix run`. .... -$ nix run +$ nix run # Won't work .... What went wrong? @@ -171,7 +171,7 @@ 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. +In that case, adding the dependency to `buildInputs` is sufficient. ==== This time `nix run` works. diff --git a/source/new-flake/haskell-flake/main.adoc0 b/source/new-flake/haskell-flake/main.adoc0 index 14f8829..fc69bfe 100644 --- a/source/new-flake/haskell-flake/main.adoc0 +++ b/source/new-flake/haskell-flake/main.adoc0 @@ -70,7 +70,7 @@ Let's enter a shell with the Glasgow Haskell Compiler ("ghc") and try to run the .... $# echo '$ nix shell nixpkgs#ghc' $# nix shell nixpkgs#ghc --command sh -$ runghc Main.hs +$ runghc Main.hs # Won't work .... The error message tells us that we need the module `Network.HostName`. @@ -81,7 +81,7 @@ Let's exit that shell and try again, this time adding the `hostname` package. $# echo '$ exit' $# echo '$ nix shell nixpkgs#ghc nixpkgs#hostname' $# nix shell nixpkgs#ghc nixpkgs#hostname --command sh -$ runghc Main.hs +$ runghc Main.hs # Won't work .... That reason that failed is that we asked for the wrong package. @@ -95,7 +95,7 @@ Let's try that again, with the correct package. $# echo '$ exit' $# echo '$ nix shell nixpkgs#ghc nixpkgs#haskellPackages.hostname' $# nix shell nixpkgs#ghc nixpkgs#haskellPackages.hostname --command sh -$ runghc Main.hs +$ runghc Main.hs # Won't work .... Now what's wrong? @@ -121,10 +121,6 @@ $ runghc Main.hs Success! Now we know the program works. -.... -$# echo '$ exit' -.... - == The cabal file It's time to write a Cabal file for this program. diff --git a/source/new-flake/python-flake/main.adoc0 b/source/new-flake/python-flake/main.adoc0 index cdd3023..29b51b2 100644 --- a/source/new-flake/python-flake/main.adoc0 +++ b/source/new-flake/python-flake/main.adoc0 @@ -101,7 +101,7 @@ We won't write `flake.nix` just yet. First we'll try building the package manually. (If you didn't run the `nix shell` command from <>, do so now.) .... -$ python -m build +$ python -m build # Won't work .... The missing module error happens because we don't have `build` available @@ -198,11 +198,11 @@ $# cat flake.nix Let's try out the new flake. .... -$ nix build +$ nix build # Won't work .... -Why can't it find `flake.nix`? Nix flakes only "`see`" files that are -part of the repository. We need to add all of the important files to the +Nix flakes only "`see`" files that are part of the repository. +We need to add all of the important files to the repo before building or running the flake. .... diff --git a/source/recipes/devshell/nixpkg/main.adoc0 b/source/recipes/devshell/nixpkg/main.adoc0 index 6c7bc6d..c9affb0 100644 --- a/source/recipes/devshell/nixpkg/main.adoc0 +++ b/source/recipes/devshell/nixpkg/main.adoc0 @@ -19,7 +19,7 @@ $# cat flake.nix Here's a demonstration using the shell. .... -$ cowsay "Moo!" +$ cowsay "Moo!" # Won't work; dependency not available $# ../../../../../start-shell nix develop <