Skip to content

Commit 5c202b4

Browse files
committed
Add nixos-generators devshell module
that injects our custom formats (at present, limited to `bootstrap-iso`) into the nixos-generators format search path. Additionally, introduce the `bootstrap-iso` command that wraps `nixos-generate --format bootstrap-iso`. fix: divnix#450
1 parent e2bb8ea commit 5c202b4

File tree

14 files changed

+223
-88
lines changed

14 files changed

+223
-88
lines changed

doc/start/iso.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# ISO
22

3-
Making and writing an installable iso for `hosts/bootstrap.nix` is as simple as:
3+
Making and writing an installable iso for a host is as simple as
4+
entering the devshell and running `boostrap-iso burn <host>`. For instance, to
5+
burn an iso for the host defined in `hosts/bootstrap.nix`, run:
6+
47
```sh
5-
bud build bootstrap bootstrapIso
6-
sudo -E $(which bud) burn
8+
nix develop
9+
bootstrap-iso burn bootstrap # note: uses sudo
710
```
811

912
This works for any host.

examples/devos/flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
nixos.url = "github:nixos/nixpkgs/nixos-21.11";
1212
latest.url = "github:nixos/nixpkgs/nixos-unstable";
1313

14-
digga.url = "github:divnix/digga";
14+
#digga.url = "github:divnix/digga";
15+
digga.url = "path:/home/matt/git/packaging/nix/digga";
1516
digga.inputs.nixpkgs.follows = "nixos";
1617
digga.inputs.nixlib.follows = "nixos";
1718
digga.inputs.home-manager.follows = "home";

examples/devos/hosts/bootstrap.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{ profiles, ... }:
22
{
3-
# build with: `bud build bootstrap bootstrapIso`
3+
# build (from within a devshell) with:
4+
#
5+
# nixos-generate --format bootstrap-iso --flake '.#bootstrap'
6+
#
7+
# or:
8+
#
9+
# bootstrap-iso build bootstrap
10+
#
11+
# which does the same thing.
12+
#
413
# reachable on the local link via ssh root@fe80::47%eno1
514
# where 'eno1' is replaced by your own machine's network
615
# interface that has the local link to the target machine
@@ -13,6 +22,7 @@
1322

1423
boot.loader.systemd-boot.enable = true;
1524

16-
# will be overridden by the bootstrapIso instrumentation
25+
# will be overridden by the bootstrap-iso `nixos-generate` format profile
26+
# defined by the digga "nixos-generators" devshell module
1727
fileSystems."/" = { device = "/dev/disk/by-label/nixos"; };
1828
}

examples/devos/shell/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
modules = with inputs; [
44
bud.devshellModules.bud
5+
digga.devshellModules.nixos-generators
56
];
67
exportedModules = [
78
./devos.nix

examples/devos/shell/devos.nix

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ in
4848
++ lib.optional
4949
(system != "i686-linux")
5050
(devos cachix)
51-
++ lib.optional
52-
(system != "aarch64-darwin")
53-
(devos inputs.nixos-generators.defaultPackage.${pkgs.system})
5451
;
52+
53+
# digga-supplied extensions to nixos-generators. Provides the
54+
# "bootstrap-iso" nixos-generators format, plus the "bootstrap-iso" wrapper
55+
# command.
56+
digga.nixos-generators = {
57+
enable = pkgs.system != "aarch64-darwin";
58+
category = "devos";
59+
package = inputs.nixos-generators.defaultPackage.${pkgs.system};
60+
};
5561
}

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116

117117
# a little extra service ...
118118
overlays = import ./overlays { inherit inputs; };
119-
nixosModules = import ./modules;
119+
nixosModules = import ./modules/nixos;
120+
devshellModules = import ./modules/devShell;
120121

121122
defaultTemplate = self.templates.devos;
122123
templates.devos.path = ./examples/devos;

modules/bootstrap-iso.nix

Lines changed: 0 additions & 74 deletions
This file was deleted.

modules/default.nix

Lines changed: 0 additions & 4 deletions
This file was deleted.

modules/devShell/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
nixos-generators = import ./nixos-generators;
3+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
if [ -z "${PRJ_ROOT:-}" ]; then
6+
if ! PRJ_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"; then
7+
# shellcheck disable=SC2016
8+
printf 1>&2 -- '%s: %s. %s. %s.\n' \
9+
"${0##*/}" 'unable to locate your project root ($PRJ_ROOT)' \
10+
'bud relies on the project root for locating the bootstrap ISO' \
11+
'Aborting'
12+
13+
exit 1
14+
fi
15+
fi
16+
17+
boostrapIsoRequireHost() {
18+
if [ "$#" -lt 1 ]; then
19+
printf 1>&2 -- 'Error: missing required <host> argument.\n' "${0##*/}"
20+
bootstrapIsoHelp 1
21+
fi
22+
}
23+
24+
bootstrapIsoBuild() {
25+
boostrapIsoRequireHost "$@" || return
26+
27+
local host="$1"
28+
shift
29+
30+
# XXX the way nixos-generate handles flake specifications requires that we do
31+
# not quote the host as we would with, e.g.:
32+
# nix build ".#nixosConfigurations.\"my.host.name\".config.system.build.toplevel"
33+
nixos-generate "$@" --format bootstrap-iso --flake "${PRJ_ROOT}#${host}"
34+
}
35+
36+
bootstrapIsoBurn() {
37+
boostrapIsoRequireHost "$@" || return
38+
bud="$(command -v bud)" || return
39+
bootstrapIsoBuild "$@" --out-link "${PRJ_ROOT?}/result" || return
40+
sudo -E "$bud" burn
41+
}
42+
43+
bootstrapIsoHelp() {
44+
printf 1>&2 -- 'Usage: %s {build,burn,help} <host> [<nixos-generate-arg> ...]\n' "${0##*/}"
45+
return "${1:-0}"
46+
}
47+
48+
cmd="${1:-}"
49+
shift 2>/dev/null || :
50+
51+
case "$cmd" in
52+
build)
53+
bootstrapIsoBuild "$@"
54+
;;
55+
burn)
56+
bootstrapIsoBurn "$@"
57+
;;
58+
help)
59+
bootstrapIsoHelp
60+
;;
61+
*)
62+
bootstrapIsoHelp 1
63+
;;
64+
esac

0 commit comments

Comments
 (0)