1
+ # `nix run microvm#vm`
1
2
{ self , nixpkgs , system } :
2
3
3
4
nixpkgs . lib . nixosSystem {
@@ -11,44 +12,64 @@ nixpkgs.lib.nixosSystem {
11
12
12
13
( { config , lib , pkgs , ... } : {
13
14
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
- ] ;
27
15
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
38
27
microvm = {
39
28
mem = 8192 ;
40
29
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
+ } ] ;
41
38
} ;
42
39
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
+ } ;
49
71
} ;
50
- }
51
- ) { } self . lib . hypervisors ;
72
+ } ) self . lib . hypervisors ) ;
52
73
53
74
systemd . network = {
54
75
enable = true ;
0 commit comments