]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/system/dilion/vms.nix
8d5a57bcb712ca615b57b07c17c97a0993d8e4fd
[perso/Immae/Config/Nix.git] / modules / private / system / dilion / vms.nix
1 # inspired from https://nixos.wiki/wiki/Virtualization_in_NixOS
2 { config, pkgs, lib, ... }@args:
3 let
4 networks = {
5 immae = {
6 bridgeNumber = "1";
7 ipRange = "192.168.100";
8 };
9 };
10 guests = {
11 caldance = {
12 pool = "zfspool";
13 cpus = "1";
14 memory = "2";
15 network = "immae";
16 diskSize = "10GiB";
17 extraDevicesXML = ''
18 <filesystem type="mount">
19 <source dir="/var/lib/caldance"/>
20 <target dir="home"/>
21 </filesystem>
22 '';
23 };
24 buildbot = {
25 pool = "zfspool";
26 cpus = "1";
27 memory = "3";
28 network = "immae";
29 diskSize = "10GiB";
30 destroyVolumeOnExit = true;
31 preStart = ''
32 if ! ${pkgs.libvirt}/bin/virsh pool-info --pool niximages &> /dev/null; then
33 pool-create-as --name niximages --type dir --target /etc/libvirtd/base-images/
34 fi
35 if ! ${pkgs.libvirt}/bin/virsh pool-info --pool buildbot-disks &> /dev/null; then
36 mkdir -p /var/lib/libvirt/images/buildbot-disks
37 pool-create-as --name buildbot-disks --type dir --target /var/lib/libvirt/images/buildbot-disks
38 fi
39 '';
40 };
41 };
42 toImage = f: "${import ./vms/base_image.nix f (args // { myEnv = config.myEnv; })}/nixos.qcow2";
43 in
44 {
45 environment.etc."libvirtd/base-images/nixos.qcow2".source = toImage ./vms/base_configuration.nix;
46 environment.etc."libvirtd/base-images/buildbot.qcow2".source = toImage ./vms/buildbot_configuration.nix;
47 systemd.services = lib.mapAttrs' (name: guest: lib.nameValuePair "libvirtd-guest-${name}" {
48 after = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ];
49 requires = [ "libvirtd.service" "libvirtd-network-${guest.network}.service" ];
50 wantedBy = [ "multi-user.target" ];
51 serviceConfig = {
52 Type = "oneshot";
53 RemainAfterExit = "yes";
54 };
55 script =
56 let
57 xml = pkgs.writeText "libvirt-guest-${name}.xml"
58 ''
59 <domain type="kvm">
60 <name>${name}</name>
61 <uuid>UUID</uuid>
62 <memory unit="GiB">${guest.memory}</memory>
63 <vcpu>${guest.cpus}</vcpu>
64 <os>
65 <type arch="x86_64">hvm</type>
66 </os>
67 <devices>
68 <emulator>/run/current-system/sw/bin/qemu-system-x86_64</emulator>
69 <disk type="volume">
70 <source pool="${guest.pool}" volume="guest-${name}" />
71 <target dev="vda" bus="virtio"/>
72 </disk>
73 ${guest.extraDevicesXML or ""}
74 <input type="keyboard" bus="usb"/>
75 <graphics type="vnc" port="-1" autoport="yes"/>
76 <interface type="network">
77 <source network="${guest.network}" />
78 </interface>
79 </devices>
80 <features>
81 <acpi/>
82 </features>
83 </domain>
84 '';
85 in
86 guest.preStart or "" + ''
87 if ! ${pkgs.libvirt}/bin/virsh vol-key 'guest-${name}' --pool ${guest.pool} &> /dev/null; then
88 ${pkgs.libvirt}/bin/virsh vol-create-as --pool ${guest.pool} --name 'guest-${name}' --capacity '${guest.diskSize}'
89 volume_path=$(${pkgs.libvirt}/bin/virsh vol-path --pool ${guest.pool} --vol 'guest-${name}')
90 ${pkgs.qemu}/bin/qemu-img convert /etc/libvirtd/base-images/nixos.qcow2 $volume_path
91 fi
92 uuid="$(${pkgs.libvirt}/bin/virsh domuuid '${name}' || true)"
93 ${pkgs.libvirt}/bin/virsh define <(sed "s/UUID/$uuid/" '${xml}')
94 ${pkgs.libvirt}/bin/virsh start '${name}'
95 '';
96 preStop = ''
97 ${pkgs.libvirt}/bin/virsh shutdown '${name}'
98 let "timeout = $(date +%s) + 10"
99 while [ "$(${pkgs.libvirt}/bin/virsh list --name | grep --count '^${name}$')" -gt 0 ]; do
100 if [ "$(date +%s)" -ge "$timeout" ]; then
101 # Meh, we warned it...
102 ${pkgs.libvirt}/bin/virsh destroy '${name}'
103 else
104 # The machine is still running, let's give it some time to shut down
105 sleep 0.5
106 fi
107 done
108 '' + lib.optionalString (guest.destroyVolumeOnExit or false) ''
109 if ${pkgs.libvirt}/bin/virsh vol-key 'guest-${name}' --pool ${guest.pool} &> /dev/null; then
110 ${pkgs.libvirt}/bin/virsh vol-wipe --pool ${guest.pool} --vol 'guest-${name}' || true
111 ${pkgs.libvirt}/bin/virsh vol-delete --pool ${guest.pool} --vol 'guest-${name}'
112 fi
113 '';
114 }) guests // (lib.mapAttrs' (name: network: lib.nameValuePair "libvirtd-network-${name}" {
115 after = [ "libvirtd.service" ];
116 requires = [ "libvirtd.service" ];
117 wantedBy = [ "multi-user.target" ];
118 serviceConfig = {
119 Type = "oneshot";
120 RemainAfterExit = "yes";
121 };
122 script = let
123 xml = pkgs.writeText "libvirt-network-${name}.xml" ''
124 <network>
125 <name>${name}</name>
126 <uuid>UUID</uuid>
127 <forward mode='nat' />
128 <bridge name='virbr${network.bridgeNumber}' />
129 <domain name='${name}' localOnly='yes'/>
130 <ip address='${network.ipRange}.1' netmask='255.255.255.0'>
131 <dhcp>
132 <range start='${network.ipRange}.2' end='${network.ipRange}.254'/>
133 </dhcp>
134 </ip>
135 </network>
136 '';
137 in ''
138 uuid="$(${pkgs.libvirt}/bin/virsh net-uuid '${name}' || true)"
139 ${pkgs.libvirt}/bin/virsh net-define <(sed "s/UUID/$uuid/" '${xml}')
140 ${pkgs.libvirt}/bin/virsh net-start '${name}'
141 '';
142 preStop = ''
143 ${pkgs.libvirt}/bin/virsh net-destroy '${name}'
144 '';
145 }) networks);
146 }