$ nix repl
+$ nix repl
Welcome to Nix 2.18.1. Type :? for help.
nix-repl> :?
@@ -736,7 +645,7 @@ The following commands are available:
diff --git a/index.html b/index.html index 8428df4..6f60e25 100644 --- a/index.html +++ b/index.html @@ -18,78 +18,6 @@ -
.cabal file).cabal file)
.cabal file)
+:? to see a list of available commands.
$ nix repl
+$ nix repl
Welcome to Nix 2.18.1. Type :? for help.
nix-repl> :?
@@ -736,7 +645,7 @@ The following commands are available:
nix-repl> a = 7
+nix-repl> a = 7
nix-repl> b = 3
@@ -759,7 +668,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:
@@ -798,7 +707,7 @@ This makes it much easier to experiment in the REPL.
nix-repl> x = 1
+nix-repl> x = 1
nix-repl> x
1
@@ -824,7 +733,7 @@ nix-repl> x
nix-repl> 1 + 2 # addition
+nix-repl> 1 + 2 # addition
3
nix-repl> 5 - 3 # subtraction
@@ -854,7 +763,7 @@ Consider the following example.
nix-repl> 6/2
+nix-repl> 6/2
/home/amy/codeberg/nix-book/6/2
:t command to find out the type of the expressi
nix-repl> :t 6/2
+nix-repl> :t 6/2
a path
/home/amy/codeberg/nix-book.
nix-repl> 6/ 2
+nix-repl> 6/ 2
3
nix-repl> :t 5
+nix-repl> :t 5
an integer
nix-repl> :t 5.0
@@ -915,7 +824,7 @@ while the second produces a floating-point result.
nix-repl> 5 / 3
+nix-repl> 5 / 3
1
nix-repl> 5.0 / 3
@@ -933,7 +842,7 @@ nix-repl> 5.0 / 3
nix-repl> "Hello, " + "world!"
+nix-repl> "Hello, " + "world!"
"Hello, world!"
nix-repl> name = "Wombat"
+nix-repl> name = "Wombat"
nix-repl> "Hi, I'm ${name}."
"Hi, I'm Wombat."
@@ -964,7 +873,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
@@ -997,7 +906,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
0.
nix-repl> builtins.substring 3 6 "hayneedlestack"
+nix-repl> builtins.substring 3 6 "hayneedlestack"
"needle"
0.
nix-repl> builtins.toString 7
+nix-repl> builtins.toString 7
"7"
nix-repl> builtins.toString (3*4 + 1)
@@ -1033,7 +942,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
@@ -1067,7 +976,7 @@ The expression u → v is equivalent to !u || v.
nix-repl> u = false
+nix-repl> u = false
nix-repl> v = true
@@ -1089,7 +998,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> ./.
@@ -1103,7 +1012,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
@@ -1123,7 +1032,7 @@ This is why the result in the example below is not /home/wombat/file.txt
nix-repl> /home/wombat + ./file.txt
+nix-repl> /home/wombat + ./file.txt
/home/wombat/home/amy/codeberg/nix-book/file.txt
/home/wombat/file.txt
nix-repl> /home/wombat + "/file.txt"
+nix-repl> /home/wombat + "/file.txt"
/home/wombat/file.txt
nix-repl> :t /home/wombat + "/file.txt"
@@ -1176,7 +1085,7 @@ The result is a string, not a path.
nix-repl> "/home/wombat" + ./file.txt
+nix-repl> "/home/wombat" + ./file.txt
"/home/wombat/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt"
nix-repl> "/home/wombat" + ./no-such-file.txt
+nix-repl> "/home/wombat" + ./no-such-file.txt
error (ignored): error: end of string reached
error: getting status of '/home/amy/codeberg/nix-book/no-such-file.txt': No such file or directory
nix-repl> builtins.pathExists ./index.html
+nix-repl> builtins.pathExists ./index.html
true
nix-repl> builtins.pathExists /no/such/path
@@ -1216,7 +1125,7 @@ false
nix-repl> builtins.readDir ./.
+nix-repl> builtins.readDir ./.
{ ".envrc" = "regular"; ".git" = "directory"; ".gitignore" = "regular"; Makefile = "regular"; images = "directory"; "index.html" = "regular"; "shell.nix" = "regular"; source = "directory"; themes = "directory"; "wombats-book-of-nix.pdf" = "regular"; }
nix-repl> builtins.readFile ./.envrc
+nix-repl> builtins.readFile ./.envrc
"use nix\n"
nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
+nix-repl> [ 1 2 3 ] ++ [ "apple" "banana" ]
[ 1 2 3 "apple" "banana" ]
nix-repl> fruit = [ "apple" "banana" "cantaloupe" ]
+nix-repl> fruit = [ "apple" "banana" "cantaloupe" ]
nix-repl> builtins.elem "apple" fruit
true
@@ -1273,7 +1182,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
@@ -1285,7 +1194,7 @@ nix-repl> builtins.elemAt fruit 2
nix-repl> builtins.length fruit
+nix-repl> builtins.length fruit
3
nix-repl> builtins.head fruit
+nix-repl> builtins.head fruit
"apple"
nix-repl> builtins.tail fruit
+nix-repl> builtins.tail fruit
[ "banana" "cantaloupe" ]
nix-repl> numbers = [ 1 3 6 8 9 15 25 ]
+nix-repl> numbers = [ 1 3 6 8 9 15 25 ]
nix-repl> isBig = n: n > 10 # is the number "big" (greater than 10)?
@@ -1330,7 +1239,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 ]
@@ -1347,7 +1256,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
@@ -1364,7 +1273,7 @@ nix-repl> animal . name . first
nix-repl> animal ? species
+nix-repl> animal ? species
true
nix-repl> animal ? bicycle
@@ -1381,7 +1290,7 @@ Attributes in the right-hand set take preference.
nix-repl> animal // { species = "tiger"; }
+nix-repl> animal // { species = "tiger"; }
{ age = 10; name = { ... }; species = "tiger"; }
nix-repl> { x = 3; y = 4*x; }
+nix-repl> { x = 3; y = 4*x; }
error: undefined variable 'x'
at «string»:1:16:
@@ -1420,7 +1329,7 @@ For more information on these and other built-in functions, see the Nix Manual
nix-repl> builtins.attrNames animal
+nix-repl> builtins.attrNames animal
[ "age" "name" "species" ]
nix-repl> builtins.attrValues animal
+nix-repl> builtins.attrValues animal
[ 10 "Professor Paws" "cat" ]
nix-repl> builtins.getAttr "age" animal
+nix-repl> builtins.getAttr "age" animal
10
nix-repl> builtins.hasAttr "name" animal
+nix-repl> builtins.hasAttr "name" animal
true
nix-repl> builtins.hasAttr "car" animal
@@ -1459,7 +1368,7 @@ false
nix-repl> builtins.removeAttrs animal [ "age" "species" ]
+nix-repl> builtins.removeAttrs animal [ "age" "species" ]
{ name = "Professor Paws"; }
nix-repl> x: x + 1
+nix-repl> x: x + 1
«lambda @ «string»:1:1»
nix-repl> :t x: x + 1
+nix-repl> :t x: x + 1
a function
nix-repl> f = x: x + 1
+nix-repl> f = x: x + 1
nix-repl> f
«lambda @ «string»:1:2»
@@ -1526,7 +1435,7 @@ This reduces visual clutter when chaining a series of functions.
nix-repl> f 5
+nix-repl> f 5
6
nix-repl> add = a: (b: a+b)
+nix-repl> add = a: (b: a+b)
nix-repl> add = a: b: a+b
+nix-repl> add = a: b: a+b
nix-repl> add 3 # Returns a function that adds 3 to any input
+nix-repl> add 3 # Returns a function that adds 3 to any input
«lambda @ «string»:1:6»
nix-repl> (add 3) 5
+nix-repl> (add 3) 5
8
nix-repl> add 3 5
+nix-repl> add 3 5
8
add to the value 3, and give the res
nix-repl> g = add 3
+nix-repl> g = add 3
g is indeed a function.
nix-repl> :t g
+nix-repl> :t g
a function
nix-repl> g 5
+nix-repl> g 5
8
nix-repl> greet = { first, last }: "Hello ${first} ${last}! May I call you ${first}?"
+nix-repl> greet = { first, last }: "Hello ${first} ${last}! May I call you ${first}?"
nix-repl> greet { first="Amy"; last="de Buitléir"; }
"Hello Amy de Buitléir! May I call you Amy?"
@@ -1674,7 +1583,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?"
@@ -1692,7 +1601,7 @@ This is done with the special parameter ….
nix-repl> formatName = { first, last, ... }: "${first} ${last}"
+nix-repl> formatName = { first, last, ... }: "${first} ${last}"
nix-repl> person = { first="Joe"; last="Bloggs"; address="123 Main Street"; }
+nix-repl> person = { first="Joe"; last="Bloggs"; address="123 Main Street"; }
nix-repl> formatName person
"Joe Bloggs"
@@ -1721,7 +1630,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"
@@ -1732,7 +1641,7 @@ nix-repl> formatPoint { x=5; y=3; z=2; }
nix-repl> formatPoint = { x, y, ... } @ p: builtins.toXML p
+nix-repl> formatPoint = { x, y, ... } @ p: builtins.toXML p
confirmAddress.
nix-repl> confirmAddress = { address, ... }: "Do you still live at ${address}?"
+nix-repl> confirmAddress = { address, ... }: "Do you still live at ${address}?"
nix-repl> greet = args@{ first, last, ... }: "Hello ${first}. " + confirmAddress args
@@ -1761,7 +1670,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
@@ -1777,7 +1686,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
@@ -1790,7 +1699,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
@@ -1806,7 +1715,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
@@ -1825,7 +1734,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
@@ -1838,7 +1747,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
@@ -1857,7 +1766,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"; }
@@ -1871,7 +1780,7 @@ using the attribute selection operator (.).
-nix-repl> with animal; "Hello, " + animal.name
+nix-repl> with animal; "Hello, " + animal.name
"Hello, Professor Paws"
@@ -1987,11 +1896,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!"
@@ -2017,93 +1926,93 @@ is always called flake.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
{
- # 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;
+ };
+ }
+ );
+}
@@ -2116,10 +2025,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";
+ };
@@ -2136,7 +2045,7 @@ flakes. The important thing to note is that the hello-flake package
flake.lock
-{
+{
"nodes": {
"flake-utils": {
"inputs": {
@@ -2203,7 +2112,7 @@ benefit of using flakes.
-{
+{
description = package description
inputs = dependencies
outputs = what the flake produces
@@ -2248,7 +2157,7 @@ pre-packaged for Nix. We can write that as
-nixpkgs.url = "github:NixOS/nixpkgs/nixos-version";
+nixpkgs.url = "github:NixOS/nixpkgs/nixos-version";
@@ -2259,7 +2168,7 @@ without the version number
-nixpkgs.url = "github:NixOS/nixpkgs/nixos";
+nixpkgs.url = "github:NixOS/nixpkgs/nixos";
@@ -2267,7 +2176,7 @@ without the version number
-nixpkgs.url = "nixpkgs";
+nixpkgs.url = "nixpkgs";
@@ -2319,11 +2228,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";
+};
@@ -2331,7 +2240,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";
@@ -2357,7 +2266,7 @@ dependency defined in the previous example.
-outputs = { self, nixpkgs, import-cargo }: {
+outputs = { self, nixpkgs, import-cargo }: {
definitions for outputs
};
@@ -2388,7 +2297,7 @@ writing your own flakes. I’ll explain the example in some detail.
-{
+{
description = "brief package description";
inputs = {
@@ -2808,9 +2717,9 @@ Here’s the modified hello-flake file.
hello-flake
-#!/usr/bin/env sh
+#!/usr/bin/env sh
-cowsay "Hello from your flake!"
+cowsay "Hello from your flake!"
@@ -2838,56 +2747,56 @@ 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.
+{
+ # 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
- {
- 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
+ '';
+ };
+ 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;
+ };
+ }
+ );
+}
@@ -2964,7 +2873,7 @@ don’t need to git push the changes until we’re ready to
$ git commit hello-flake flake.nix -m 'added bovine feature'
-[main 584de79] added bovine feature
+[main c3c7006] added bovine feature
2 files changed, 7 insertions(+), 1 deletion(-)
$ nix run
________________________
@@ -3085,19 +2994,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
@@ -3269,33 +3178,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.
@@ -3418,121 +3327,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;
+ };
+ };
+}
@@ -3567,7 +3476,7 @@ warning: creating lock file '"/home/amy/codeberg/nix-book/source/new-flake/haske
• Added input 'haskell-flake':
'github:srid/haskell-flake/73e3891fb135c679a1c30fae4b101e5b41b8ca61?narHash=sha256-%2B99fEAk0HwjYgIW2tEOs7ayBDxnU9NAM5E29ZxgyX40%3D' (2025-08-31)
• Added input 'nixpkgs':
- 'github:nixos/nixpkgs/58dcbf1ec551914c3756c267b8b9c8c86baa1b2f?narHash=sha256-6FWagzm0b7I/IGigOv9pr6LL7NQ86mextfE8g8Q6HBg%3D' (2025-09-01)
+ 'github:nixos/nixpkgs/aaff8c16d7fc04991cac6245bee1baa31f72b1e1?narHash=sha256-12V64nKG/O/guxSYnr5/nq1EfqwJCdD2%2BcIGmhz3nrE%3D' (2025-09-02)
warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/haskell-flake/hello-haskell' is dirty
@@ -3604,7 +3513,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) 32ca704] initial commit
+[master (root-commit) 13bd936] initial commit
5 files changed, 170 insertions(+)
create mode 100644 LICENSE
create mode 100644 Main.hs
@@ -3662,19 +3571,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()
@@ -3749,29 +3658,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']
+ },
+)
@@ -3903,101 +3812,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;
+ };
+ }
+ );
+}
@@ -4033,7 +3942,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/a7a78cc789217374761981009642baa4add0a94e?narHash=sha256-16WA%2B5sgGI5NOLVYdb9HTX3XQ8OgXQC3gEWX%2BCdOB5A%3D' (2025-09-02)
+ 'github:NixOS/nixpkgs/a7a241d389c733eec41ce783696ced2fdf21a3cf?narHash=sha256-TfvMA0TsVj754lmK0VRj3OaoK4k5djo7UWsJDDGYvj0%3D' (2025-09-04)
warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/python-flake/hello-python' is dirty
@@ -4051,6 +3960,9 @@ The important thing for now is that the build succeeded.
$ nix run
warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/python-flake/hello-python' is dirty
+this derivation will be built:
+ /nix/store/abfhnjamdd7741mq8ighm1z552mi7adl-hello-flake-python.drv
+building '/nix/store/abfhnjamdd7741mq8ighm1z552mi7adl-hello-flake-python.drv'...
Hello from inside a Python program built with a Nix flake!
@@ -4069,7 +3981,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) d6c844d] initial commit
+[master (root-commit) 70da3d5] initial commit
4 files changed, 127 insertions(+)
create mode 100644 flake.lock
create mode 100644 flake.nix
@@ -4117,27 +4029,14 @@ comparing the different recipes will help you better understand Nix.
-
-
An "ad hoc" shell
-is useful when you want to quickly create an environment
+
"Ad hoc" shells
+are useful when you want to quickly create an environment
for a one-off task.
-
-
A traditional nix shell
-is useful when you want to define an environment that you will
-use more than once.
-
--
Nix flakes
-are the recommended approach for development projects.
-
--
-
You can use nix-shell to run scripts in arbitrary languages, providing
-the necessary dependencies. This is particularly convenient for
-standalone scripts because you don’t need to create a repo and write a
-separate flake.nix.
-The script should start with two "shebang" (#!) commands. The first
-should invoke nix-shell. The second should declares the script
-interpreter and any dependencies.
+are the recommended approach for development projects,
+including defining environments that you will use more than once.
@@ -4179,7 +4078,9 @@ Hello from your flake!
-To run a specific branch, use the command below.
+To use a package from GitHub, GitLab, or any other public platform,
+modify the URL accordingly.
+To run a specific branch, use the command below.
@@ -4242,6 +4143,10 @@ $ cowsay "moo"
10.2.2. A flake
+
+In this example, we will use a flake defined in a remote git repo.
+However, you can use any of the flake reference styles defined in Section 10.1.2, “Flakes”.
+
$ nix shell git+https://codeberg.org/mhwombat/hello-flake
@@ -4249,25 +4154,31 @@ $ hello-flake
Hello from your flake!
-
-You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.
-
10.3. Scripts with access to…
+
+You can use nix shell to run scripts in arbitrary languages, providing
+the necessary dependencies.
+This is particularly convenient for standalone scripts
+because you don’t need to create a repo and write a separate flake.nix.
+The script should start with two "shebang" (#!) commands.
+The first should invoke nix.
+The second should declares the script interpreter and any dependencies.
+
10.3.1. A top level package from the Nixpkgs/NixOS repo
Script
-1
-2
-3
-4
#! /usr/bin/env nix
-#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
-hello
-cowsay "Pretty cool, huh?"
+1
+2
+3
+4
#! /usr/bin/env nix
+#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
+hello
+cowsay "Pretty cool, huh?"
@@ -4288,14 +4199,18 @@ cowsay "Pretty cool, huh?&q
10.3.2. A flake
+
+In this example, we will use a flake defined in a remote git repo.
+However, you can use any of the flake reference styles defined in Section 10.1.2, “Flakes”.
+
Script
-1
-2
-3
#! /usr/bin/env nix
-#! nix shell git+https://codeberg.org/mhwombat/hello-flake --command bash
-hello-flake
+1
+2
+3
#! /usr/bin/env nix
+#! nix shell git+https://codeberg.org/mhwombat/hello-flake --command bash
+hello-flake
@@ -4305,9 +4220,6 @@ hello-flake
Hello from your flake!
-
-You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.
-
@@ -4317,45 +4229,45 @@ hello-flake
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 ];
+ };
+ };
+ }
+ );
+}
@@ -4381,158 +4293,251 @@ $ cowsay "Moo!"
10.4.2. A flake
+
+In this example, we will use a flake defined in a remote git repo.
+However, you can use any of the flake reference styles defined in Section 10.1.2, “Flakes”.
+
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 ];
+ };
+ };
+ }
+ );
+}
-EXPLAIN
-
-
-EXPLAIN
-
-
-EXPLAIN
-
-
-EXPLAIN
+Let’s take a closer look at the buildInputs.
- buildInputs = [ hello-flake.packages.${system}.hello ];
+buildInputs=[hello-flake.packages.${system}.hello];
+Why is the first part hello-flake and the last part hello?
+The first part refers to the name we assigned in the input section of our flake,
+and the last part is the name of the package or app we want.
+
+
+You can find the outputs of a flake using the nix flake show command.
+
+
+
+$ nix flake show git+https://codeberg.org/mhwombat/hello-flake
+git+https://codeberg.org/mhwombat/hello-flake?ref=refs/heads/main&rev=099bfadc43f79a8929809bd097d12ef5de73ef62
+├───apps
+│ ├───aarch64-darwin
+│ │ ├───default: app: no description
+│ │ └───hello: app: no description
+│ ├───aarch64-linux
+│ │ ├───default: app: no description
+│ │ └───hello: app: no description
+│ ├───x86_64-darwin
+│ │ ├───default: app: no description
+│ │ └───hello: app: no description
+│ └───x86_64-linux
+│ ├───default: app: no description
+│ └───hello: app: no description
+└───packages
+ ├───aarch64-darwin
+ │ ├───default omitted (use '--all-systems' to show)
+ │ └───hello omitted (use '--all-systems' to show)
+ ├───aarch64-linux
+ │ ├───default omitted (use '--all-systems' to show)
+ │ └───hello omitted (use '--all-systems' to show)
+ ├───x86_64-darwin
+ │ ├───default omitted (use '--all-systems' to show)
+ │ └───hello omitted (use '--all-systems' to show)
+ └───x86_64-linux
+ ├───default: package 'hello-flake'
+ └───hello: package 'hello-flake'
+
+
+
+Examining the output of this command,
+we see that this flake provides both a package and an app called hello.
+
+
Here’s a demonstration using the shell.
$ hello-flake
-bash: line 32: hello-flake: command not found
+bash: line 40: hello-flake: command not found
$ nix develop
$ hello-flake
Hello from your flake!
+
+
+10.4.3. A Haskell library package in the nixpkgs repo (without a .cabal file)
-You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.
+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.
+
+
+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";
+ };
+
+ 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 ];
+ };
+ };
+ }
+ );
+}
+
+
+
+
+Here’s a short Haskell program that uses it.
+
+
+Main.hs
+
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
import Data.Map
+
+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."
+
+
+
+
+Here’s a demonstration using the program.
+
+
+
+$ nix develop
+$ runghc Main.hs
+"I have 3 cats, 2 dogs, and 0 zebras."
+
-10.5. Access to a top-level package from the Nixpkgs/NixOS repo
+10.5. An (old-style) Nix shell with access to a flake
-Ex: Access two packages from nixpkgs:
-hello and cowsay.
+If you are maintaining legacy code,
+you may need to provide access to a flake in a nix-shell.
+Here’s how.
-
-10.5.1. From the command line (flake style)
-
-
-$ nix shell nixpkgs#hello nixpkgs#cowsay --command bash
-$ hello
-Hello, world!
-$ cowsay "moo"
- _____
-< moo >
- -----
- \ ^__^
- \ (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
-
-
-
-
-10.5.2. From the command line (non-flake style)
-
-
-$ nix-shell -p "[hello cowsay]"
-$ hello
-Hello, world!
-$ cowsay "moo"
- _____
-< moo >
- -----
- \ ^__^
- \ (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
-
-
-
-
-10.5.3. In flake.nix
-
-See the example in Section 8.2, “Introducing a dependency”
-where defined a development shell in flake.nix
-with a dependency on cowsay ,
-and tested the shell.
-
-
-
-10.5.4. In shell.nix
shell.nix
-1
-2
-3
-4
-5
-6
-7
with (import <nixpkgs> {});
-mkShell {
- buildInputs = [
- hello
- cowsay
- ];
-}
+ 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
+ ];
+}
@@ -4542,78 +4547,8 @@ mkShell {
$ nix-shell
-$ hello
-Hello, world!
-$ cowsay "moo"
- _____
-< moo >
- -----
- \ ^__^
- \ (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
-
-
-
-
-10.5.5. In a Bash script (flake style)
-
-Script
-
-1
-2
-3
-4
#! /usr/bin/env nix
-#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
-hello
-cowsay "Pretty cool, huh?"
-
-
-
-
-Output
-
-Hello, world!
- ___________________
-< Pretty cool, huh? >
- -------------------
- \ ^__^
- \ (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
-
-
-
-
-10.5.6. In a Bash script (non-flake style)
-
-Script
-
-1
-2
-3
-4
#! /usr/bin/env nix-shell
-#! nix-shell -i bash -p "[hello cowsay]"
-hello
-cowsay "Pretty cool, huh?"
-
-
-
-
-Output
-
-Hello, world!
- ___________________
-< Pretty cool, huh? >
- -------------------
- \ ^__^
- \ (oo)\_______
- (__)\ )\/\
- ||----w |
- || ||
-
+$ hello-flake
+Hello from your flake!
@@ -4625,106 +4560,24 @@ which is defined in a remote git repo on codeberg.
To use a package from GitHub, GitLab, or any other public platform,
modify the URL.
-
-10.6.1. In shell.nix
-
-shell.nix
-
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
with (import <nixpkgs> {});
-let
- hello-nix = import (builtins.fetchGit {
- url = "https://codeberg.org/mhwombat/hello-nix";
- rev = "aa2c87f8b89578b069b09fdb2be30a0c9d8a77d8";
- });
-in
-mkShell {
- buildInputs = [ hello-nix ];
-}
-
-
+
+REMOVE ME
-Here’s a demonstration using the shell.
+REMOVE ME
-
-
-$ nix-shell
-$ hello-nix
-Hello from your nix package!
+
+REMOVE ME
+
+REMOVE ME
+
+Unresolved directive in recipes/remote-git/main.adoc - include::flake/main-generated.adoc[leveloffset=+1]
-10.7. Access to a flake defined in a remote git repo
-
-Ex: Access a flake called hello-flake,
-which is defined in a remote git repo on codeberg.
-To use a package from GitHub, GitLab, or any other public platform,
-modify the URL.
-
-
-10.7.1. From the command line
-
-
-$ nix shell git+https://codeberg.org/mhwombat/hello-flake
-$ hello-flake
-Hello from your flake!
-
-
-
-
-10.7.2. In shell.nix
-
-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 = [
- hello-flake
- ];
-}
-
-
-
-
-Here’s a demonstration using the shell.
-
-
-
-$ nix-shell
-$ hello-flake
-Hello from your flake!
-
-
-
-
-
-10.8. Access to a Haskell library package in the nixpkgs repo (without a .cabal file)
+10.7. Access to a Haskell library package in the nixpkgs repo (without a .cabal file)
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.
@@ -4732,106 +4585,43 @@ but you don’t want to bother writing a cabal file.
Example: Access the containers package from the haskellPackages set in the nixpkgs repo.
-
-10.8.1. In shell.nix
-
-shell.nix
-
-1
-2
-3
-4
-5
-6
-7
-8
-9
with (import <nixpkgs> {});
-let
- customGhc = haskellPackages.ghcWithPackages (pkgs: with pkgs; [ containers ]);
-in
-mkShell {
- buildInputs = [
- customGhc
- ];
-}
-
-
-
-Here’s a short Haskell program that uses it.
-
-
-Main.hs
-
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
import Data.Map
-
-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."
-
-
-
-
-Here’s a demonstration using the program.
-
-
-
-$ nix-shell
-$ runghc Main.hs
-"I have 3 cats, 2 dogs, and 0 zebras."
-
-
+Unresolved directive in recipes/haskell-nixpkg/main.adoc - include::shell/main-generated.adoc[leveloffset=+1]
-10.8.2. In a Haskell script
+10.7.1. In a Haskell script
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."
@@ -4844,51 +4634,51 @@ $ runghc Main.hs
-10.9. Access to a Haskell package on your local computer
+10.8. Access to a Haskell package on your local computer
Ex: Access three Haskell packages
(pandoc-linear-table, pandoc-logic-proof, and pandoc-columns)
that are on my hard drive.
-10.9.1. In shell.nix
+10.8.1. In shell.nix
shell.nix
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
with (import <nixpkgs> {});
-let
- pandoc-linear-table = haskellPackages.callCabal2nix "pandoc-linear-table" /home/amy/github/pandoc-linear-table {};
- pandoc-logic-proof = haskellPackages.callCabal2nix "pandoc-logic-proof" /home/amy/github/pandoc-logic-proof {};
- pandoc-columns = haskellPackages.callCabal2nix "pandoc-columns" /home/amy/github/pandoc-columns {};
-in
-mkShell {
- buildInputs = [
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
with (import <nixpkgs> {});
+let
+ pandoc-linear-table = haskellPackages.callCabal2nix "pandoc-linear-table" /home/amy/github/pandoc-linear-table {};
+ pandoc-logic-proof = haskellPackages.callCabal2nix "pandoc-logic-proof" /home/amy/github/pandoc-logic-proof {};
+ pandoc-columns = haskellPackages.callCabal2nix "pandoc-columns" /home/amy/github/pandoc-columns {};
+in
+mkShell {
+ buildInputs = [
pandoc
pandoc-linear-table
pandoc-logic-proof
pandoc-columns
- ];
-}
+ ];
+}
-10.10. Access to a Haskell package on your local computer, with inter-dependencies
+10.9. Access to a Haskell package on your local computer, with inter-dependencies
Ex: Access four Haskell packages
(pandoc-linear-table, pandoc-logic-proof, pandoc-columns and pandoc-maths-web)
@@ -4896,54 +4686,54 @@ that are on my hard drive.
The fourth package depends on the first three to build.
-10.10.1. In shell.nix
+10.9.1. In shell.nix
shell.nix
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
with (import <nixpkgs> {});
-let
- pandoc-linear-table = haskellPackages.callCabal2nix "pandoc-linear-table" /home/amy/github/pandoc-linear-table {};
- pandoc-logic-proof = haskellPackages.callCabal2nix "pandoc-logic-proof" /home/amy/github/pandoc-logic-proof {};
- pandoc-columns = haskellPackages.callCabal2nix "pandoc-columns" /home/amy/github/pandoc-columns {};
- pandoc-maths-web = haskellPackages.callCabal2nix "pandoc-maths-web" /home/amy/github/pandoc-maths-web
- {
- inherit pandoc-linear-table pandoc-logic-proof pandoc-columns;
- };
-in
-mkShell {
- buildInputs = [
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
with (import <nixpkgs> {});
+let
+ pandoc-linear-table = haskellPackages.callCabal2nix "pandoc-linear-table" /home/amy/github/pandoc-linear-table {};
+ pandoc-logic-proof = haskellPackages.callCabal2nix "pandoc-logic-proof" /home/amy/github/pandoc-logic-proof {};
+ pandoc-columns = haskellPackages.callCabal2nix "pandoc-columns" /home/amy/github/pandoc-columns {};
+ pandoc-maths-web = haskellPackages.callCabal2nix "pandoc-maths-web" /home/amy/github/pandoc-maths-web
+ {
+ inherit pandoc-linear-table pandoc-logic-proof pandoc-columns;
+ };
+in
+mkShell {
+ buildInputs = [
pandoc
pandoc-linear-table
pandoc-logic-proof
pandoc-columns
pandoc-maths-web
- ];
-}
+ ];
+}
-10.11. Access to a Python library package in the nixpkgs repo (without using a Python builder)
+10.10. Access to a Python library package in the nixpkgs repo (without using a Python builder)
Occasionally you might want to run a short Python program that depends on a Python library,
but you don’t want to bother configuring a builder.
@@ -4952,31 +4742,31 @@ but you don’t want to bother configuring a builder.
Example: Access the html_sanitizer package from the python3nnPackages set in the nixpkgs repo.
-10.11.1. In a Python script
+10.10.1. In a Python script
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)
@@ -4990,26 +4780,26 @@ sanitized: <strong>some text</strong>
-10.12. Set an environment variable
+10.11. Set an environment variable
Ex: Set the value of the environment variable FOO to “bar”
-10.12.1. In shell.nix
+10.11.1. In shell.nix
shell.nix
-1
-2
-3
-4
-5
-6
with (import <nixpkgs> {});
-mkShell {
- shellHook = ''
- export FOO="bar"
- '';
-}
+1
+2
+3
+4
+5
+6
with (import <nixpkgs> {});
+mkShell {
+ shellHook = ''
+ export FOO="bar"
+ '';
+}
@@ -5029,7 +4819,7 @@ mkShell {
diff --git a/source/recipes/devshell/main.adoc b/source/recipes/devshell/main.adoc
index 236a888..8494074 100644
--- a/source/recipes/devshell/main.adoc
+++ b/source/recipes/devshell/main.adoc
@@ -5,3 +5,6 @@ include::nixpkg/main-generated.adoc[leveloffset=+1]
include::flake/main-generated.adoc[leveloffset=+1]
include::haskell-pkg/main-generated.adoc[leveloffset=+1]
+
+include::non-flake/main-generated.adoc[leveloffset=+1]
+
diff --git a/source/recipes/devshell/haskell-pkg/tempwork/flake.lock b/source/recipes/devshell/non-flake/tempwork/flake.lock
similarity index 65%
rename from source/recipes/devshell/haskell-pkg/tempwork/flake.lock
rename to source/recipes/devshell/non-flake/tempwork/flake.lock
index 2bcb884..25e25ef 100644
--- a/source/recipes/devshell/haskell-pkg/tempwork/flake.lock
+++ b/source/recipes/devshell/non-flake/tempwork/flake.lock
@@ -18,13 +18,29 @@
"type": "github"
}
},
+ "hello-nix": {
+ "flake": false,
+ "locked": {
+ "lastModified": 1676213208,
+ "narHash": "sha256-Zds5C+f5sA3w+j85EsAQjte0zeHqubMVPGYn5lGwATo=",
+ "ref": "refs/heads/main",
+ "rev": "aa2c87f8b89578b069b09fdb2be30a0c9d8a77d8",
+ "revCount": 4,
+ "type": "git",
+ "url": "https://codeberg.org/mhwombat/hello-nix"
+ },
+ "original": {
+ "type": "git",
+ "url": "https://codeberg.org/mhwombat/hello-nix"
+ }
+ },
"nixpkgs": {
"locked": {
- "lastModified": 1757012778,
- "narHash": "sha256-TfvMA0TsVj754lmK0VRj3OaoK4k5djo7UWsJDDGYvj0=",
+ "lastModified": 1757096966,
+ "narHash": "sha256-nJpPWCFbc5/u0VIoSHT350FEwPS5pwiRfyZBbZA3Kgg=",
"owner": "NixOS",
"repo": "nixpkgs",
- "rev": "a7a241d389c733eec41ce783696ced2fdf21a3cf",
+ "rev": "6eb784fb955ab56952e6eeb01f3e64a79933e2b5",
"type": "github"
},
"original": {
@@ -36,6 +52,7 @@
"root": {
"inputs": {
"flake-utils": "flake-utils",
+ "hello-nix": "hello-nix",
"nixpkgs": "nixpkgs"
}
},
diff --git a/source/recipes/devshell/haskell-pkg/tempwork/flake.nix b/source/recipes/devshell/non-flake/tempwork/flake.nix
similarity index 56%
rename from source/recipes/devshell/haskell-pkg/tempwork/flake.nix
rename to source/recipes/devshell/non-flake/tempwork/flake.nix
index da098df..560bc6e 100644
--- a/source/recipes/devshell/haskell-pkg/tempwork/flake.nix
+++ b/source/recipes/devshell/non-flake/tempwork/flake.nix
@@ -2,18 +2,24 @@
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 }:
+ outputs = { self, nixpkgs, flake-utils, hello-nix }:
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 ];
+ shellHook =
+ ''
+ PATH=${hello-nix}:$PATH
+ '';
};
};
}
diff --git a/source/recipes/haskell-nixpkg/shell/shell.nix b/source/recipes/haskell-nixpkg/shell/shell.nix
deleted file mode 100644
index 2fe5f1f..0000000
--- a/source/recipes/haskell-nixpkg/shell/shell.nix
+++ /dev/null
@@ -1,9 +0,0 @@
-with (import {});
-let
- customGhc = haskellPackages.ghcWithPackages (pkgs: with pkgs; [ containers ]);
-in
-mkShell {
- buildInputs = [
- customGhc
- ];
-}
diff --git a/source/recipes/remote-git/flake/flake.nix b/source/recipes/remote-git/flake/flake.nix
deleted file mode 100644
index f142706..0000000
--- a/source/recipes/remote-git/flake/flake.nix
+++ /dev/null
@@ -1,54 +0,0 @@
-{
- # 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 sample flake";
-
- 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; };
- in
- {
- devShells = rec {
- default = pkgs.mkShell {
- packages = [ hello-nix ];
- };
- };
-
- packages = rec {
- hello = pkgs.stdenv.mkDerivation rec {
- name = "hello-again";
-
- src = ./.;
-
- unpackPhase = "true";
-
- buildPhase = ":";
-
- installPhase =
- ''
- mkdir -p $out/bin
- cp $src/hello-again $out/bin/hello-again
- chmod +x $out/bin/hello-again
- '';
- };
- default = hello;
- };
-
- apps = rec {
- hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
- default = hello;
- };
- }
- );
-}
diff --git a/source/recipes/remote-git/flake/hello-again b/source/recipes/remote-git/flake/hello-again
deleted file mode 100755
index 5315da4..0000000
--- a/source/recipes/remote-git/flake/hello-again
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env sh
-
-echo "I'm a flake, but I'm running a command defined in a non-flake package."
-hello-nix
diff --git a/source/recipes/remote-git/main.adoc b/source/recipes/remote-git/main.adoc
index e3d61c1..58efbd6 100644
--- a/source/recipes/remote-git/main.adoc
+++ b/source/recipes/remote-git/main.adoc
@@ -5,4 +5,12 @@ which is defined in a remote git repo on codeberg.
To use a package from GitHub, GitLab, or any other public platform,
modify the URL.
-include::shell/main-generated.adoc[leveloffset=+1]
+REMOVE ME
+
+REMOVE ME
+
+REMOVE ME
+
+REMOVE ME
+
+include::flake/main-generated.adoc[leveloffset=+1]
diff --git a/source/recipes/run/main.adoc0 b/source/recipes/run/main.adoc0
index 7ee8048..679fe85 100644
--- a/source/recipes/run/main.adoc0
+++ b/source/recipes/run/main.adoc0
@@ -21,6 +21,8 @@ $ nix run ~/codeberg/hello-flake
$ nix run git+https://codeberg.org/mhwombat/hello-flake
....
+To use a package from GitHub, GitLab, or any other public platform,
+modify the URL accordingly.
To run a specific branch, use the command below.
....