Skip to content

Commit e76628c

Browse files
committed
integrate new example into microvms-host.nix example
1 parent 3012167 commit e76628c

File tree

3 files changed

+43
-175
lines changed

3 files changed

+43
-175
lines changed

examples/host-static-guest.nix

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

examples/microvms-host.nix

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ nixpkgs.lib.nixosSystem {
1010
# this runs as a MicroVM that nests MicroVMs
1111
self.nixosModules.microvm
1212

13-
({ config, lib, pkgs, ... }: {
13+
({ 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+
{
1426
networking.hostName = "microvms-host";
1527
system.stateVersion = config.system.nixos.version;
1628
users.users.root.password = "";
@@ -38,38 +50,30 @@ nixpkgs.lib.nixosSystem {
3850
};
3951

4052
# Nested MicroVMs (a *host* option)
41-
microvm.vms = builtins.listToAttrs (
42-
map (hypervisor: {
43-
name = hypervisor;
44-
value = {
45-
config = {
46-
system.stateVersion = config.system.nixos.version;
47-
networking.hostName = "${hypervisor}-microvm";
53+
microvm.vms = builtins.mapAttrs (hypervisor: mac:
54+
{
55+
config = {
56+
system.stateVersion = config.system.nixos.version;
57+
networking.hostName = "${hypervisor}-microvm";
4858

49-
microvm = {
50-
inherit hypervisor;
51-
interfaces = [ {
52-
type = "tap";
53-
id = "vm-${builtins.substring 0 12 hypervisor}";
54-
mac =
55-
let
56-
hash = builtins.hashString "sha256" hypervisor;
57-
c = off: builtins.substring off 2 hash;
58-
in
59-
"${builtins.substring 0 1 hash}2:${c 2}:${c 4}:${c 6}:${c 8}:${c 10}";
60-
} ];
61-
};
62-
# Just use 99-ethernet-default-dhcp.network
63-
systemd.network.enable = true;
59+
microvm = {
60+
inherit hypervisor;
61+
interfaces = [ {
62+
type = "tap";
63+
id = "vm-${builtins.substring 0 12 hypervisor}";
64+
inherit mac;
65+
} ];
66+
};
67+
# Just use 99-ethernet-default-dhcp.network
68+
systemd.network.enable = true;
6469

65-
users.users.root.password = "";
66-
services.openssh = {
67-
enable = true;
68-
settings.PermitRootLogin = "yes";
69-
};
70+
users.users.root.password = "";
71+
services.openssh = {
72+
enable = true;
73+
settings.PermitRootLogin = "yes";
7074
};
7175
};
72-
}) self.lib.hypervisors);
76+
}) hypervisors-with-mac;
7377

7478
systemd.network = {
7579
enable = true;
@@ -85,6 +89,16 @@ nixpkgs.lib.nixosSystem {
8589
DHCPServer = true;
8690
IPv6SendRA = true;
8791
};
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+
];
88102
addresses = [ {
89103
addressConfig.Address = "10.0.0.1/24";
90104
} {

flake.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
};
4848
in {
4949
vm = nixosToApp ./examples/microvms-host.nix;
50-
host-static-guest = nixosToApp ./examples/host-static-guest.nix;
5150
qemu-vnc = nixosToApp ./examples/qemu-vnc.nix;
5251
graphics = {
5352
type = "app";

0 commit comments

Comments
 (0)