|
108 | 108 | }; |
109 | 109 | } // |
110 | 110 | # wrap self.nixosConfigurations in executable packages |
111 | | - builtins.foldl' (result: systemName: |
112 | | - let |
113 | | - nixos = self.nixosConfigurations.${systemName}; |
114 | | - name = builtins.replaceStrings [ "${system}-" ] [ "" ] systemName; |
115 | | - inherit (nixos.config.microvm) hypervisor; |
116 | | - in |
117 | | - if nixos.pkgs.stdenv.hostPlatform.system == lib.replaceString "-darwin" "-linux" system |
118 | | - then result // { |
119 | | - "${name}" = nixos.config.microvm.runner.${hypervisor}; |
| 111 | + lib.listToAttrs ( |
| 112 | + lib.concatMap (configName: |
| 113 | + let |
| 114 | + config = self.nixosConfigurations.${configName}; |
| 115 | + packageName = lib.replaceString "${system}-" "" configName; |
| 116 | + # Check if this config's guest system matches our current build system |
| 117 | + # (accounting for darwin hosts building linux guests) |
| 118 | + guestSystem = config.pkgs.stdenv.hostPlatform.system; |
| 119 | + buildSystem = lib.replaceString "-darwin" "-linux" system; |
| 120 | + in |
| 121 | + lib.optional (guestSystem == buildSystem) |
| 122 | + { |
| 123 | + name = packageName; |
| 124 | + value = config.config.microvm.runner.${config.config.microvm.hypervisor}; |
120 | 125 | } |
121 | | - else result |
122 | | - ) {} (builtins.attrNames self.nixosConfigurations); |
| 126 | + ) (builtins.attrNames self.nixosConfigurations) |
| 127 | + ); |
123 | 128 |
|
124 | 129 | # Takes too much memory in `nix flake show` |
125 | 130 | # checks = import ./checks { inherit self nixpkgs system; }; |
|
162 | 167 | ]; |
163 | 168 | hypervisorsWithUserNet = [ "qemu" "kvmtool" "vfkit" ]; |
164 | 169 | hypervisorsDarwinOnly = [ "vfkit" ]; |
| 170 | + hypervisorsWithTap = builtins.filter |
| 171 | + # vfkit supports networking, but does not support tap |
| 172 | + (hv: hv != "vfkit") |
| 173 | + self.lib.hypervisorsWithNetwork; |
| 174 | + |
| 175 | + isDarwinOnly = hypervisor: builtins.elem hypervisor hypervisorsDarwinOnly; |
| 176 | + isDarwinSystem = system: lib.hasSuffix "-darwin" system; |
| 177 | + hypervisorSupportsSystem = hypervisor: system: |
| 178 | + # Darwin-only hypervisors only work on darwin, others work everywhere |
| 179 | + !(isDarwinOnly hypervisor && !(isDarwinSystem system)); |
| 180 | + |
165 | 181 | makeExample = { system, hypervisor, config ? {} }: |
166 | 182 | lib.nixosSystem { |
167 | 183 | system = lib.replaceString "-darwin" "-linux" system; |
|
220 | 236 | config |
221 | 237 | ]; |
222 | 238 | }; |
223 | | - in |
224 | | - (builtins.foldl' (results: system: |
225 | | - builtins.foldl' ({ result, n }: hypervisor: |
226 | | - let |
227 | | - # Skip darwin-only hypervisors on Linux systems |
228 | | - isDarwinOnly = builtins.elem hypervisor hypervisorsDarwinOnly; |
229 | | - isDarwinSystem = lib.hasSuffix "-darwin" system; |
230 | | - shouldSkip = isDarwinOnly && !isDarwinSystem; |
231 | | - in |
232 | | - if shouldSkip then { inherit result n; } |
233 | | - else { |
234 | | - result = result // { |
235 | | - "${system}-${hypervisor}-example" = makeExample { |
236 | | - inherit system hypervisor; |
237 | | - }; |
238 | | - } // |
239 | | - # Skip tap example for darwin-only hypervisors (vfkit doesn't support tap) |
240 | | - lib.optionalAttrs (builtins.elem hypervisor self.lib.hypervisorsWithNetwork && !isDarwinOnly) { |
241 | | - "${system}-${hypervisor}-example-with-tap" = makeExample { |
242 | | - inherit system hypervisor; |
243 | | - config = _: { |
244 | | - microvm.interfaces = [ { |
245 | | - type = "tap"; |
246 | | - id = "vm-${builtins.substring 0 4 hypervisor}"; |
247 | | - mac = "02:00:00:01:01:0${toString n}"; |
248 | | - } ]; |
249 | | - networking = { |
250 | | - interfaces.eth0.useDHCP = true; |
251 | | - firewall.allowedTCPPorts = [ 22 ]; |
252 | | - }; |
253 | | - services.openssh = { |
254 | | - enable = true; |
255 | | - settings.PermitRootLogin = "yes"; |
256 | | - }; |
| 239 | + |
| 240 | + basicExamples = lib.flatten ( |
| 241 | + lib.map (system: |
| 242 | + lib.map (hypervisor: { |
| 243 | + name = "${system}-${hypervisor}-example"; |
| 244 | + value = makeExample { inherit system hypervisor; }; |
| 245 | + shouldInclude = hypervisorSupportsSystem hypervisor system; |
| 246 | + }) self.lib.hypervisors |
| 247 | + ) systems |
| 248 | + ); |
| 249 | + |
| 250 | + tapExamples = lib.flatten ( |
| 251 | + lib.map (system: |
| 252 | + lib.imap1 (idx: hypervisor: { |
| 253 | + name = "${system}-${hypervisor}-example-with-tap"; |
| 254 | + value = makeExample { |
| 255 | + inherit system hypervisor; |
| 256 | + config = _: { |
| 257 | + microvm.interfaces = [ { |
| 258 | + type = "tap"; |
| 259 | + id = "vm-${builtins.substring 0 4 hypervisor}"; |
| 260 | + mac = "02:00:00:01:01:0${toString idx}"; |
| 261 | + } ]; |
| 262 | + networking = { |
| 263 | + interfaces.eth0.useDHCP = true; |
| 264 | + firewall.allowedTCPPorts = [ 22 ]; |
| 265 | + }; |
| 266 | + services.openssh = { |
| 267 | + enable = true; |
| 268 | + settings.PermitRootLogin = "yes"; |
257 | 269 | }; |
258 | 270 | }; |
259 | 271 | }; |
260 | | - n = n + 1; |
261 | | - } |
262 | | - ) results self.lib.hypervisors |
263 | | - ) { result = {}; n = 1; } systems).result; |
| 272 | + shouldInclude = builtins.elem hypervisor hypervisorsWithTap |
| 273 | + && hypervisorSupportsSystem hypervisor system; |
| 274 | + }) self.lib.hypervisors |
| 275 | + ) systems |
| 276 | + ); |
| 277 | + |
| 278 | + included = builtins.filter (ex: ex.shouldInclude) (basicExamples ++ tapExamples); |
| 279 | + in |
| 280 | + builtins.listToAttrs (builtins.map ({ name, value, ... }: { inherit name value; }) included); |
264 | 281 | }; |
265 | 282 | } |
0 commit comments