|
1 | | -{ lib, ... }: |
2 | 1 | { |
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 | +in |
| 51 | +{ |
| 52 | + options = { |
| 53 | + flake.diskoConfigurations = mkOption { |
| 54 | + type = types.lazyAttrsOf types.raw; |
| 55 | + default = { }; |
| 56 | + description = "Instantiated Disko configurations. Used by `disko` and `disko-install`."; |
| 57 | + example = lib.literalExpression '' |
| 58 | + { |
| 59 | + my-pc = { |
| 60 | + disko.devices = { |
| 61 | + disk = { |
| 62 | + my-disk = { |
| 63 | + device = "/dev/sda"; |
| 64 | + type = "disk"; |
| 65 | + content = { |
| 66 | + type = "gpt"; |
| 67 | + partitions = { |
| 68 | + ESP = { |
| 69 | + type = "EF00"; |
| 70 | + size = "500M"; |
| 71 | + content = { |
| 72 | + type = "filesystem"; |
| 73 | + format = "vfat"; |
| 74 | + mountpoint = "/boot"; |
| 75 | + mountOptions = [ "umask=0077" ]; |
| 76 | + }; |
| 77 | + }; |
| 78 | + root = { |
| 79 | + size = "100%"; |
| 80 | + content = { |
| 81 | + type = "filesystem"; |
| 82 | + format = "ext4"; |
| 83 | + mountpoint = "/"; |
| 84 | + }; |
| 85 | + }; |
33 | 86 | }; |
34 | 87 | }; |
35 | 88 | }; |
36 | 89 | }; |
37 | 90 | }; |
38 | 91 | }; |
39 | | - }; |
| 92 | + }''; |
| 93 | + }; |
| 94 | + |
| 95 | + disko.nixosImages = { |
| 96 | + hosts = mkOption { |
| 97 | + type = types.attrsOf ( |
| 98 | + types.submodule ( |
| 99 | + { name, ... }: |
| 100 | + { |
| 101 | + options = { |
| 102 | + nixosConfiguration = mkOption { |
| 103 | + type = types.str; |
| 104 | + default = name; |
| 105 | + defaultText = lib.literalExpression "<name>"; |
| 106 | + description = "Flake attribute for NixOS Configuration to install."; |
| 107 | + example = "my-host"; |
| 108 | + }; |
| 109 | + |
| 110 | + formats = mkOption { |
| 111 | + type = types.listOf types.str; # TODO stricter typing |
| 112 | + apply = lib.unique; |
| 113 | + default = [ "iso-installer" ]; |
| 114 | + description = "Image formats to generate installer images for."; |
| 115 | + example = "hyperv"; |
| 116 | + }; |
| 117 | + |
| 118 | + modules = mkOption { |
| 119 | + type = types.listOf types.deferredModule; |
| 120 | + default = [ ]; |
| 121 | + description = "NixOS modules to include in the installer configuration."; |
| 122 | + example = lib.literalExpression '' |
| 123 | + { pkgs, ... }: |
| 124 | + { |
| 125 | + environment.systemPackages = [ |
| 126 | + (pkgs.writeShellScriptBin "install-nixos-unattended" '''' |
| 127 | + set -eux |
| 128 | + # Replace "/dev/disk/by-id/some-disk-id" with your actual disk ID |
| 129 | + exec ''${pkgs.disko}/bin/disko-install --flake "''${self}#your-machine" --disk vdb "/dev/disk/by-id/some-disk-id" |
| 130 | + '''') |
| 131 | + ]; |
| 132 | + }''; |
| 133 | + }; |
| 134 | + }; |
| 135 | + } |
| 136 | + ) |
| 137 | + ); |
40 | 138 | }; |
41 | 139 | }; |
42 | 140 | }; |
| 141 | + |
| 142 | + config.flake.packages = lib.mkMerge ( |
| 143 | + lib.concatLists ( |
| 144 | + lib.mapAttrsToList ( |
| 145 | + name: host: |
| 146 | + let |
| 147 | + nixosConfiguration = self.nixosConfigurations.${host.nixosConfiguration}; |
| 148 | + inherit (nixosConfiguration.pkgs.stdenv.hostPlatform) system; |
| 149 | + |
| 150 | + installerConfiguration = nixpkgs.lib.nixosSystem { |
| 151 | + inherit (nixosConfiguration.pkgs.stdenv.hostPlatform) system; |
| 152 | + modules = [ (mkInstallClosureModule nixosConfiguration) ] ++ host.modules; |
| 153 | + }; |
| 154 | + in |
| 155 | + map (format: { |
| 156 | + ${system}."disko-image-${name}-${format}" = |
| 157 | + installerConfiguration.config.system.build.images.${format}; |
| 158 | + }) host.formats |
| 159 | + ) config.disko.nixosImages.hosts |
| 160 | + ) |
| 161 | + ); |
43 | 162 | } |
0 commit comments