This commit is contained in:
Amy de Buitléir 2025-09-14 20:49:57 +01:00
parent a23029eb15
commit 00bfdf2c0f
12 changed files with 161 additions and 140 deletions

View file

@ -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.

View file

@ -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 { ❶

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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 <<python-nix-shell,earlier>>, 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.
....

View file

@ -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 <<EOL
$ cowsay "Moo!"
$# EOL

View file

@ -0,0 +1,60 @@
{
"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": 1757876592,
"narHash": "sha256-P8XdKg87fR5PpCjIgMH+05PDwhdmlNrigsvChdHfyaw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5ad881b15fb2b63c2f127533ca59f840fcc24c3f",
"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
}

View file

@ -0,0 +1,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 ];
};
};
}
);
}

View file

@ -3,7 +3,7 @@
Occasionally you might want to run a short Haskell program that depends on a Haskell library,
but you don't want to bother writing a cabal file.
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.
[source,haskell,linenums]
.Script

View file

@ -1,15 +1,10 @@
#! /usr/bin/env nix-shell
#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.containers])"
#! 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"