{ config, lib, pkgs, ... }:

{
  # Enable virtualisation
  virtualisation.libvirtd = {
    enable = true;
    qemuRunAsRoot = false;
    onBoot = "ignore";
    onShutdown = "shutdown";
    allowedBridges = [ "br0" ];
  };

  # Declarative configuration of the VMs
  systemd.services.sica = {
    description = "SICA database";
    wantedBy = [ "default.target" ];
    script = ''
      disk=/vault/VMs/sica.qcow2
      sock=/run/qemu-sica.mon.sock
      ${pkgs.qemu}/bin/qemu-kvm -m 1G -nic bridge,br=br0,model=virtio --hda $disk -monitor unix:$sock,server,nowait -nographic
    '';
    preStop = ''
      echo 'system_powerdown' | ${pkgs.socat}/bin/socat - UNIX-CONNECT:/run/qemu-sica.mon.sock
      sleep 10
    '';
  };
}