Skip to content

Commit 30e8266

Browse files
committed
examples/microvms-host: reformat
1 parent e76628c commit 30e8266

File tree

1 file changed

+80
-84
lines changed

1 file changed

+80
-84
lines changed

examples/microvms-host.nix

Lines changed: 80 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,46 @@ nixpkgs.lib.nixosSystem {
1111
self.nixosModules.microvm
1212

1313
({ config, lib, pkgs, ... }:
14-
let
15-
hypervisors-with-mac = builtins.listToAttrs(
16-
map (hypervisor:
17-
let
18-
hash = builtins.hashString "sha256" hypervisor;
19-
c = off: builtins.substring off 2 hash;
20-
mac = "${builtins.substring 0 1 hash}2:${c 2}:${c 4}:${c 6}:${c 8}:${c 10}";
21-
in
22-
{ name = hypervisor; value = mac; })
23-
self.lib.hypervisors);
24-
in
25-
{
26-
networking.hostName = "microvms-host";
27-
system.stateVersion = config.system.nixos.version;
28-
users.users.root.password = "";
29-
users.motd = ''
30-
Once nested MicroVMs have booted you can look up DHCP leases:
31-
networkctl status virbr0
14+
let
15+
hypervisorMacAddrs = builtins.listToAttrs (
16+
map (hypervisor:
17+
let
18+
hash = builtins.hashString "sha256" hypervisor;
19+
c = off: builtins.substring off 2 hash;
20+
mac = "${builtins.substring 0 1 hash}2:${c 2}:${c 4}:${c 6}:${c 8}:${c 10}";
21+
in {
22+
name = hypervisor;
23+
value = mac;
24+
}) self.lib.hypervisors);
25+
in {
26+
networking.hostName = "microvms-host";
27+
system.stateVersion = config.system.nixos.version;
28+
users.users.root.password = "";
29+
users.motd = ''
30+
Once nested MicroVMs have booted you can look up DHCP leases:
31+
networkctl status virbr0
3232
33-
They are configured to allow SSH login with an empty root
34-
password.
35-
'';
36-
services.getty.autologinUser = "root";
33+
They are configured to allow SSH login with an empty root
34+
password.
35+
'';
36+
services.getty.autologinUser = "root";
3737

38-
# MicroVM settings
39-
microvm = {
40-
mem = 8192;
41-
vcpu = 4;
42-
# Use QEMU because nested virtualization and user networking
43-
# are required.
44-
hypervisor = "qemu";
45-
interfaces = [ {
46-
type = "user";
47-
id = "qemu";
48-
mac = "02:00:00:01:01:01";
49-
} ];
50-
};
38+
# MicroVM settings
39+
microvm = {
40+
mem = 8192;
41+
vcpu = 4;
42+
# Use QEMU because nested virtualization and user networking
43+
# are required.
44+
hypervisor = "qemu";
45+
interfaces = [ {
46+
type = "user";
47+
id = "qemu";
48+
mac = "02:00:00:01:01:01";
49+
} ];
50+
};
5151

52-
# Nested MicroVMs (a *host* option)
53-
microvm.vms = builtins.mapAttrs (hypervisor: mac:
54-
{
52+
# Nested MicroVMs (a *host* option)
53+
microvm.vms = builtins.mapAttrs (hypervisor: mac: {
5554
config = {
5655
system.stateVersion = config.system.nixos.version;
5756
networking.hostName = "${hypervisor}-microvm";
@@ -61,7 +60,7 @@ nixpkgs.lib.nixosSystem {
6160
interfaces = [ {
6261
type = "tap";
6362
id = "vm-${builtins.substring 0 12 hypervisor}";
64-
inherit mac;
63+
inherit mac;
6564
} ];
6665
};
6766
# Just use 99-ethernet-default-dhcp.network
@@ -73,54 +72,51 @@ nixpkgs.lib.nixosSystem {
7372
settings.PermitRootLogin = "yes";
7473
};
7574
};
76-
}) hypervisors-with-mac;
75+
}) hypervisorMacAddrs;
7776

78-
systemd.network = {
79-
enable = true;
80-
netdevs.virbr0.netdevConfig = {
81-
Kind = "bridge";
82-
Name = "virbr0";
83-
};
84-
networks.virbr0 = {
85-
matchConfig.Name = "virbr0";
86-
# Hand out IP addresses to MicroVMs.
87-
# Use `networkctl status virbr0` to see leases.
88-
networkConfig = {
89-
DHCPServer = true;
90-
IPv6SendRA = true;
77+
systemd.network = {
78+
enable = true;
79+
netdevs.virbr0.netdevConfig = {
80+
Kind = "bridge";
81+
Name = "virbr0";
82+
};
83+
networks.virbr0 = {
84+
matchConfig.Name = "virbr0";
85+
# Hand out IP addresses to MicroVMs.
86+
# Use `networkctl status virbr0` to see leases.
87+
networkConfig = {
88+
DHCPServer = true;
89+
IPv6SendRA = true;
90+
};
91+
dhcpServerStaticLeases = [ {
92+
# Let DHCP assign a statically known address to the qemu vm
93+
dhcpServerStaticLeaseConfig = {
94+
MACAddress = hypervisors-with-mac.qemu;
95+
Address = "10.0.13.37";
96+
};
97+
} ];
98+
addresses = [ {
99+
addressConfig.Address = "10.0.0.1/24";
100+
} {
101+
addressConfig.Address = "fd12:3456:789a::1/64";
102+
} ];
103+
ipv6Prefixes = [ {
104+
ipv6PrefixConfig.Prefix = "fd12:3456:789a::/64";
105+
} ];
106+
};
107+
networks.microvm-eth0 = {
108+
matchConfig.Name = "vm-*";
109+
networkConfig.Bridge = "virbr0";
91110
};
92-
dhcpServerStaticLeases =
93-
[
94-
# Let DHCP assign a statically known address to the qemu vm
95-
{
96-
dhcpServerStaticLeaseConfig = {
97-
MACAddress = hypervisors-with-mac.qemu;
98-
Address = "10.0.13.37";
99-
};
100-
}
101-
];
102-
addresses = [ {
103-
addressConfig.Address = "10.0.0.1/24";
104-
} {
105-
addressConfig.Address = "fd12:3456:789a::1/64";
106-
} ];
107-
ipv6Prefixes = [ {
108-
ipv6PrefixConfig.Prefix = "fd12:3456:789a::/64";
109-
} ];
110111
};
111-
networks.microvm-eth0 = {
112-
matchConfig.Name = "vm-*";
113-
networkConfig.Bridge = "virbr0";
112+
# Allow DHCP server
113+
networking.firewall.allowedUDPPorts = [ 67 ];
114+
# Allow Internet access
115+
networking.nat = {
116+
enable = true;
117+
enableIPv6 = true;
118+
internalInterfaces = [ "virbr0" ];
114119
};
115-
};
116-
# Allow DHCP server
117-
networking.firewall.allowedUDPPorts = [ 67 ];
118-
# Allow Internet access
119-
networking.nat = {
120-
enable = true;
121-
enableIPv6 = true;
122-
internalInterfaces = [ "virbr0" ];
123-
};
124-
})
120+
})
125121
];
126122
}

0 commit comments

Comments
 (0)