Skip to content

Commit d350318

Browse files
committed
examples/microvms-host: rework using declarative microvms
1 parent 8daed5f commit d350318

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

examples/microvms-host.nix

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# `nix run microvm#vm`
12
{ self, nixpkgs, system }:
23

34
nixpkgs.lib.nixosSystem {
@@ -11,44 +12,64 @@ nixpkgs.lib.nixosSystem {
1112

1213
({ config, lib, pkgs, ... }: {
1314
networking.hostName = "microvms-host";
14-
users.users.root.password = "";
15-
nix = {
16-
extraOptions = "experimental-features = nix-command flakes";
17-
registry = {
18-
nixpkgs.flake = nixpkgs;
19-
microvm.flake = self;
20-
};
21-
};
22-
# won't build stratovirt else. TODO: why?
23-
environment.noXlibs = lib.mkForce false;
24-
environment.systemPackages = [
25-
pkgs.git
26-
];
2715
system.stateVersion = config.system.nixos.version;
28-
services = let
29-
service = if lib.versionAtLeast (lib.versions.majorMinor lib.version) "20.09" then "getty" else "mingetty";
30-
in {
31-
${service}.helpLine = ''
32-
Log in as "root" with an empty password.
33-
Type Ctrl-a c to switch to the qemu console
34-
and `quit` to stop the VM.
35-
'';
36-
};
37-
# Host MicroVM settings
16+
users.users.root.password = "";
17+
users.motd = ''
18+
Once nested MicroVMs have booted you can look up DHCP leases:
19+
networkctl status virbr0
20+
21+
They are configured to allow SSH login with an empty root
22+
password.
23+
'';
24+
services.getty.autologinUser = "root";
25+
26+
# MicroVM settings
3827
microvm = {
3928
mem = 8192;
4029
vcpu = 4;
30+
# Use QEMU because nested virtualization and user networking
31+
# are required.
32+
hypervisor = "qemu";
33+
interfaces = [ {
34+
type = "user";
35+
id = "qemu";
36+
mac = "02:00:00:01:01:01";
37+
} ];
4138
};
4239

43-
# Nested MicroVMs
44-
microvm.vms = builtins.foldl' (vms: hypervisor:
45-
vms // {
46-
"${system}-${hypervisor}-example-with-tap" = {
47-
flake = self;
48-
updateFlake = "microvm";
40+
# 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";
48+
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;
64+
65+
users.users.root.password = "";
66+
services.openssh = {
67+
enable = true;
68+
settings.PermitRootLogin = "yes";
69+
};
70+
};
4971
};
50-
}
51-
) {} self.lib.hypervisors;
72+
}) self.lib.hypervisors);
5273

5374
systemd.network = {
5475
enable = true;

0 commit comments

Comments
 (0)