Skip to content

Commit be79162

Browse files
feat: add flake-parts installer image options
1 parent 5af7af1 commit be79162

File tree

1 file changed

+201
-33
lines changed

1 file changed

+201
-33
lines changed

flake-module.nix

Lines changed: 201 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,211 @@
1-
{ lib, ... }:
21
{
3-
options.flake.diskoConfigurations = lib.mkOption {
4-
type = lib.types.lazyAttrsOf lib.types.raw;
5-
default = { };
6-
description = "Instantiated Disko configurations. Used by `disko` and `disko-install`.";
7-
example = {
8-
my-pc = {
9-
disko.devices = {
10-
disk = {
11-
my-disk = {
12-
device = "/dev/sda";
13-
type = "disk";
14-
content = {
15-
type = "gpt";
16-
partitions = {
17-
ESP = {
18-
type = "EF00";
19-
size = "500M";
20-
content = {
21-
type = "filesystem";
22-
format = "vfat";
23-
mountpoint = "/boot";
24-
mountOptions = [ "umask=0077" ];
25-
};
26-
};
27-
root = {
28-
size = "100%";
29-
content = {
30-
type = "filesystem";
31-
format = "ext4";
32-
mountpoint = "/";
2+
lib,
3+
config,
4+
inputs,
5+
self,
6+
...
7+
}:
8+
let
9+
inherit (lib)
10+
mkOption
11+
types
12+
;
13+
14+
nixpkgs = inputs.nixpkgs or (throw "No nixpkgs input found");
15+
16+
flakeOutPaths =
17+
let
18+
collector =
19+
parent:
20+
map (
21+
child:
22+
[ child.outPath ] ++ (if child ? inputs && child.inputs != { } then (collector child) else [ ])
23+
) (lib.attrValues parent.inputs);
24+
in
25+
lib.unique (lib.flatten (collector self));
26+
27+
mkInstallClosureModule =
28+
nixosConfiguration:
29+
{ pkgs, ... }:
30+
let
31+
dependencies = [
32+
nixosConfiguration.config.system.build.toplevel
33+
nixosConfiguration.config.system.build.diskoScript
34+
nixosConfiguration.config.system.build.diskoScript.drvPath
35+
nixosConfiguration.pkgs.stdenv.drvPath
36+
37+
# https://github.com/NixOS/nixpkgs/blob/f2fd33a198a58c4f3d53213f01432e4d88474956/nixos/modules/system/activation/top-level.nix#L342
38+
nixosConfiguration.pkgs.perlPackages.ConfigIniFiles
39+
nixosConfiguration.pkgs.perlPackages.FileSlurp
40+
41+
(nixosConfiguration.pkgs.closureInfo { rootPaths = [ ]; }).drvPath
42+
]
43+
++ flakeOutPaths;
44+
45+
closureInfo = pkgs.closureInfo { rootPaths = dependencies; };
46+
in
47+
{
48+
environment.etc."install-closure".source = "${closureInfo}/store-paths";
49+
};
50+
51+
mkInstallScriptModule =
52+
host:
53+
{ pkgs, ... }:
54+
let
55+
diskMapping = lib.concatLists (
56+
lib.mapAttrsToList (name: path: [
57+
"--disk"
58+
name
59+
path
60+
]) host.disks
61+
);
62+
63+
args = [
64+
"--flake"
65+
"${self}#${host.nixosConfiguration}"
66+
]
67+
++ diskMapping
68+
++ host.script.extraArgs;
69+
in
70+
{
71+
environment.systemPackages = lib.singleton (
72+
pkgs.writeShellScriptBin host.script.name ''
73+
set -eux
74+
exec ${lib.getExe' pkgs.disko "disko-install"} ${lib.escapeShellArgs args}
75+
''
76+
);
77+
};
78+
in
79+
{
80+
options = {
81+
flake.diskoConfigurations = mkOption {
82+
type = types.lazyAttrsOf types.raw;
83+
default = { };
84+
description = "Instantiated Disko configurations. Used by `disko` and `disko-install`.";
85+
example = lib.literalExpression ''
86+
{
87+
my-pc = {
88+
disko.devices = {
89+
disk = {
90+
my-disk = {
91+
device = "/dev/sda";
92+
type = "disk";
93+
content = {
94+
type = "gpt";
95+
partitions = {
96+
ESP = {
97+
type = "EF00";
98+
size = "500M";
99+
content = {
100+
type = "filesystem";
101+
format = "vfat";
102+
mountpoint = "/boot";
103+
mountOptions = [ "umask=0077" ];
104+
};
105+
};
106+
root = {
107+
size = "100%";
108+
content = {
109+
type = "filesystem";
110+
format = "ext4";
111+
mountpoint = "/";
112+
};
113+
};
33114
};
34115
};
35116
};
36117
};
37118
};
38119
};
39-
};
40-
};
120+
}'';
121+
};
122+
123+
disko-install.hosts = mkOption {
124+
type = types.attrsOf (
125+
types.submodule (
126+
{ name, ... }:
127+
{
128+
options = {
129+
nixosConfiguration = mkOption {
130+
type = types.str;
131+
default = name;
132+
defaultText = lib.literalExpression "<name>";
133+
description = "Flake attribute for NixOS Configuration to install.";
134+
example = "my-host";
135+
};
136+
137+
disks = mkOption {
138+
type = types.attrsOf types.path;
139+
# no default as this should always be set
140+
description = "Disk names mapped to device paths for disko-install.";
141+
example = lib.literalExpression "{ vdb = /dev/disk/by-id/some-disk-id; }";
142+
};
143+
144+
image = {
145+
formats = mkOption {
146+
type = types.listOf types.str; # TODO stricter typing
147+
apply = lib.unique;
148+
default = [ "iso-installer" ];
149+
description = "Image formats to generate installer images for.";
150+
example = "hyperv";
151+
};
152+
153+
modules = mkOption {
154+
type = types.listOf types.deferredModule;
155+
default = [ ];
156+
description = "NixOS modules to include in the installer configuration.";
157+
example = lib.literalExpression ''
158+
{ pkgs, ... }:
159+
{
160+
environment.systemPackages = [ pkgs.git ];
161+
}'';
162+
};
163+
};
164+
165+
script = {
166+
name = mkOption {
167+
type = types.str;
168+
default = "install-nixos-unattended";
169+
description = "Name to use for the unattended installer executable.";
170+
example = "my-install-script";
171+
};
172+
173+
extraArgs = mkOption {
174+
type = types.listOf types.str;
175+
default = [ ];
176+
description = "Extra args to pass to disko-install.";
177+
example = lib.literalExpression ''[ "--mode" "mount" ]'';
178+
};
179+
};
180+
};
181+
}
182+
)
183+
);
41184
};
42185
};
186+
187+
config.flake.packages = lib.mkMerge (
188+
lib.concatLists (
189+
lib.mapAttrsToList (
190+
name: host:
191+
let
192+
nixosConfiguration = self.nixosConfigurations.${host.nixosConfiguration};
193+
inherit (nixosConfiguration.pkgs.stdenv.hostPlatform) system;
194+
195+
installerConfiguration = nixpkgs.lib.nixosSystem {
196+
inherit (nixosConfiguration.pkgs.stdenv.hostPlatform) system;
197+
modules = [
198+
(mkInstallClosureModule nixosConfiguration)
199+
(mkInstallScriptModule host)
200+
]
201+
++ host.image.modules;
202+
};
203+
in
204+
map (format: {
205+
${system}."disko-image-${name}-${format}" =
206+
installerConfiguration.config.system.build.images.${format};
207+
}) host.image.formats
208+
) config.disko-install.hosts
209+
)
210+
);
43211
}

0 commit comments

Comments
 (0)