@@ -15,7 +15,7 @@ let
1515 '';
1616 qemuConfigFile = pkgs.writeText "qemu.conf" ''
1717 ${optionalString cfg.qemuOvmf ''
18− nvram = ["${pkgs.OVMF.fd}/FV/OVMF_CODE.fd:${pkgs.OVMF.fd}/FV/OVMF_VARS.fd"]
18+ nvram = ["/run/libvirt/nix-ovmf/OVMF_CODE.fd:/run/libvirt/nix-ovmf/OVMF_VARS.fd"]
1919 ''}
2020 ${cfg.qemuVerbatimConfig}
2121 '';
@@ -102,9 +102,7 @@ in {
102102
103103 config = mkIf cfg.enable {
104104
105− environment.systemPackages = with pkgs;
106− [ libvirt netcat-openbsd ]
107− ++ optional cfg.enableKVM qemu_kvm;
105+ environment.systemPackages = with pkgs; [ libvirt netcat-openbsd ];
108106
109107 boot.kernelModules = [ "tun" ];
110108
@@ -129,7 +127,6 @@ in {
129127 dnsmasq
130128 ebtables
131129 ]
132− ++ optional cfg.enableKVM qemu_kvm
133130 ++ optional vswitch.enable vswitch.package;
134131
135132 preStart = ''
@@ -155,34 +152,31 @@ in {
155152 # Copy generated qemu config to libvirt directory
156153 cp -f ${qemuConfigFile} /var/lib/libvirt/qemu.conf
157154
158− # libvirtd puts the full path of the emulator binary in the machine
159− # config file. But this path can unfortunately be garbage collected
160− # while still being used by the virtual machine. So update the
161− # emulator path on each startup to something valid (re-scan $PATH).
162− for file in /var/lib/libvirt/qemu/*.xml /var/lib/libvirt/lxc/*.xml; do
163− test -f "$file" || continue
164− # get (old) emulator path from config file
165− emulator=$("${pkgs.xmlstarlet}/bin/xmlstarlet" select --template --value-of "/domain/devices/emulator" "$file")
166− # get a (definitely) working emulator path by re-scanning $PATH
167− new_emulator=$(PATH=${pkgs.libvirt}/libexec:$PATH command -v $(basename "$emulator"))
168− # write back
169− "${pkgs.xmlstarlet}/bin/xmlstarlet" edit --inplace --update "/domain/devices/emulator" -v "$new_emulator" "$file"
170−
171− # Also refresh the OVMF path. Files with no matches are ignored.
172− "${pkgs.xmlstarlet}/bin/xmlstarlet" edit --inplace --update "/domain/os/loader" -v "${pkgs.OVMF.fd}/FV/OVMF_CODE.fd" "$file"
173− done
174− ''; # */
155+ # stable (not GC'able as in /nix/store) paths for using in <emulator> section of xml configs
156+ mkdir -p /run/libvirt/nix-emulators
157+ ln -s --force ${pkgs.libvirt}/libexec/libvirt_lxc /run/libvirt/nix-emulators/
158+ ${optionalString pkgs.stdenv.isAarch64 "ln -s --force ${pkgs.qemu}/bin/qemu-system-aarch64 /run/libvirt/nix-emulators/"}
159+ ${optionalString cfg.enableKVM "ln -s --force ${pkgs.qemu_kvm}/bin/qemu-kvm /run/libvirt/nix-emulators/"}
160+
161+ ${optionalString cfg.qemuOvmf ''
162+ mkdir -p /run/libvirt/nix-ovmf
163+ ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_CODE.fd /run/libvirt/nix-ovmf/
164+ ln -s --force ${pkgs.OVMF.fd}/FV/OVMF_VARS.fd /run/libvirt/nix-ovmf/
165+ ''}
166+ '';
175167
176168 serviceConfig = {
177169 Type = "notify";
178170 KillMode = "process"; # when stopping, leave the VMs alone
179− Restart = "on-failure";
171+ Restart = "no";
180172 };
173+ restartIfChanged = false;
181174 };
182175
183176 systemd.services.libvirt-guests = {
184177 wantedBy = [ "multi-user.target" ];
185178 path = with pkgs; [ coreutils libvirt gawk ];
179+ restartIfChanged = false;
186180 };
187181
188182 systemd.sockets.virtlogd = {
@@ -194,6 +188,7 @@ in {
194188 systemd.services.virtlogd = {
195189 description = "Virtual machine log manager";
196190 serviceConfig.ExecStart = "@${pkgs.libvirt}/sbin/virtlogd virtlogd";
191+ restartIfChanged = false;
197192 };
198193
199194 systemd.sockets.virtlockd = {
@@ -205,6 +200,7 @@ in {
205200 systemd.services.virtlockd = {
206201 description = "Virtual machine lock manager";
207202 serviceConfig.ExecStart = "@${pkgs.libvirt}/sbin/virtlockd virtlockd";
203+ restartIfChanged = false;
208204 };
209205 };
210206}