1 # inspired from https://nixos.wiki/wiki/Virtualization_in_NixOS
2 { config, pkgs, lib, ... }@args:
7 target = "/etc/libvirtd/base-images";
14 target = "/var/lib/libvirt/images/buildbot-disks";
17 # pool-define-as --name zfspool --source-name zpool/libvirt --type zfs
21 <name>zpool/libvirt</name>
29 ipRange = "192.168.100";
40 <filesystem type="mount">
41 <source dir="/var/lib/caldance"/>
52 destroyVolumeOnExit = true;
55 toImage = f: "${import ./vms/base_image.nix f (args // { myEnv = config.myEnv; })}/nixos.qcow2";
58 environment.etc."libvirtd/base-images/nixos.qcow2".source = toImage ./vms/base_configuration.nix;
59 environment.etc."libvirtd/base-images/buildbot.qcow2".source = toImage ./vms/buildbot_configuration.nix;
60 systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" {
61 after = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ];
62 requires = [ "libvirtd.service" "libvirtd-pool-${guest.pool}.service" "libvirtd-network-${guest.network}.service" ];
63 wantedBy = [ "multi-user.target" ];
66 RemainAfterExit = "yes";
68 path = [ config.boot.zfs.package ];
71 xml = pkgs.writeText "libvirt-guest-${name}.xml"
76 <memory unit="GiB">${guest.memory}</memory>
77 <vcpu>${guest.cpus}</vcpu>
79 <type arch="x86_64">hvm</type>
82 <emulator>/run/current-system/sw/bin/qemu-system-x86_64</emulator>
84 <source pool="${guest.pool}" volume="guest-${name}" />
85 <target dev="vda" bus="virtio"/>
87 ${guest.extraDevicesXML or ""}
88 <input type="keyboard" bus="usb"/>
89 <graphics type="vnc" port="-1" autoport="yes"/>
90 <interface type="network">
91 <source network="${guest.network}" />
100 guest.preStart or "" + ''
101 if ! ${pkgs.libvirt}/bin/virsh vol-key 'guest-${name}' --pool ${guest.pool} &> /dev/null; then
102 ${pkgs.libvirt}/bin/virsh vol-create-as --pool ${guest.pool} --name 'guest-${name}' --capacity '${guest.diskSize}'
103 volume_path=$(${pkgs.libvirt}/bin/virsh vol-path --pool ${guest.pool} --vol 'guest-${name}')
104 ${pkgs.qemu}/bin/qemu-img convert /etc/libvirtd/base-images/nixos.qcow2 $volume_path
106 uuid="$(${pkgs.libvirt}/bin/virsh domuuid '${name}' || true)"
107 ${pkgs.libvirt}/bin/virsh define <(sed "s/UUID/$uuid/" '${xml}')
108 ${pkgs.libvirt}/bin/virsh start '${name}'
111 ${pkgs.libvirt}/bin/virsh shutdown '${name}'
112 let "timeout = $(date +%s) + 10"
113 while [ "$(${pkgs.libvirt}/bin/virsh list --name | grep --count '^${name}$')" -gt 0 ]; do
114 if [ "$(date +%s)" -ge "$timeout" ]; then
115 # Meh, we warned it...
116 ${pkgs.libvirt}/bin/virsh destroy '${name}'
118 # The machine is still running, let's give it some time to shut down
122 '' + lib.optionalString (guest.destroyVolumeOnExit or false) ''
123 if ${pkgs.libvirt}/bin/virsh vol-key 'guest-${name}' --pool ${guest.pool} &> /dev/null; then
124 ${pkgs.libvirt}/bin/virsh vol-wipe --pool ${guest.pool} --vol 'guest-${name}' || true
125 ${pkgs.libvirt}/bin/virsh vol-delete --pool ${guest.pool} --vol 'guest-${name}'
128 }) guests // (lib.mapAttrs' (name: network: lib.nameValuePair "libvirtd-network-${name}" {
129 after = [ "libvirtd.service" ];
130 requires = [ "libvirtd.service" ];
131 wantedBy = [ "multi-user.target" ];
134 RemainAfterExit = "yes";
136 path = [ config.boot.zfs.package ];
138 xml = pkgs.writeText "libvirt-network-${name}.xml" ''
142 <forward mode='nat' />
143 <bridge name='virbr${network.bridgeNumber}' />
144 <domain name='${name}' localOnly='yes'/>
145 <ip address='${network.ipRange}.1' netmask='255.255.255.0'>
147 <range start='${network.ipRange}.2' end='${network.ipRange}.254'/>
153 uuid="$(${pkgs.libvirt}/bin/virsh net-uuid '${name}' || true)"
154 ${pkgs.libvirt}/bin/virsh net-define <(sed "s/UUID/$uuid/" '${xml}')
155 ${pkgs.libvirt}/bin/virsh net-start '${name}'
158 ${pkgs.libvirt}/bin/virsh net-destroy '${name}'
160 }) networks) // (lib.mapAttrs' (name: pool: lib.nameValuePair "libvirtd-pool-${name}" {
161 after = [ "libvirtd.service" ];
162 requires = [ "libvirtd.service" ];
163 wantedBy = [ "multi-user.target" ];
166 RemainAfterExit = "yes";
168 path = [ config.boot.zfs.package ];
170 xml = pkgs.writeText "libvirt-pool-${name}.xml" ''
171 <pool type="${pool.type}">
175 ${if pool ? target then ''
177 <path>${pool.target}</path>
182 in pool.preStart or "" + ''
183 uuid="$(${pkgs.libvirt}/bin/virsh pool-uuid '${name}' || true)"
184 ${pkgs.libvirt}/bin/virsh pool-define <(sed "s/UUID/$uuid/" '${xml}')
185 ${pkgs.libvirt}/bin/virsh pool-start '${name}' || true