{ _name1_, _name2_, ... }
+{ name1, name2, ... }
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.
{ _name1_, _name2_, ... }
+{ name1, name2, ... }
.).
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.
$ 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
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 calledflake.nix.@@ -2154,11 +2150,11 @@ flakes. The important thing to note is that theIf 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.
hello-flakepackage "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 thehello-flakepackage }, "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
+presented in Chapter 6, A generic flake, most of it should look familiar.flake.nix, let’s have a look at the one we saw earlier, in thehello-flakerepo. If you compare this flake definition to the colour-coded template -presented in the previous section, most of it should look familiar.
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.
};
}
);
-}
-
+}
$ hello-flake -Hello from your flake!+bash: line 16: hello-flake: command not found
hello-flake file.
$ ./hello-flake +$ ./hello-flake # Won't work ./hello-flake: line 3: cowsay: command not found
$ 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
buildInputs is sufficient.
+In that case, adding the dependency to buildInputs is sufficient.
$ 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!"
$ 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!"
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.@@ -4312,7 +4273,7 @@ We need to add this to the repo, and commit all important files.$ 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 thehostname$ 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 calledhaskellPackages<$ 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.@@ -3847,7 +3808,7 @@ We need to add this to the repo, and commit all important files.$ 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$ 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.@@ -4252,7 +4213,7 @@ something like this.-$ python -m build +$ python -m build # Won't work /nix/store/iyff8129iampdw13nlfqalzhxy8y1hi9-python3-3.13.6/bin/python: No module named build-$ 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$ 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
+containerspackage from thehaskellPackagesset in the nixpkgs repo.Example: Access the
extrapackage from thehaskellPackagesset 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"
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"
extra package from the ha
For non-trivial Haskell development projects,
it’s usually more convenient to use haskell-flake as described in Section 9.1.5, “The Nix flake”,
-together with the high-level workflow described in Section 8.3, “Development workflows”.
+together with the high-level workflow described in Section 8.3, “Development workflows”.
@@ -5649,7 +5598,7 @@ Hello from your flake!