summaryrefslogtreecommitdiff
path: root/sys/virt/libvirt.nix
blob: 1cc42a9ef5095b43c644da87a04ce5f883a1d037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{ config, flakes, lib, pkgs, ... }:
with lib; let
  cfg = config.local.virt;

  inherit (config.lib.local) importAll;

  doms = mapAttrs (_: dom: dom { inherit config lib pkgs; }) (importAll { root = ./dom; });
in
{
  options.local.virt = {
    enable = mkEnableOption "hypervisor support";

    dom = mapAttrs
      (name: _: {
        enable = mkEnableOption "domain ${name}";
      })
      doms;
  };

  config = mkIf cfg.enable {
    local.boot.impermanence.directories = [
      { directory = "/var/dom"; user = "root"; group = "qemu-libvirtd"; mode = "u=rwx,g=rx,o="; }
    ];

    virtualisation = {
      libvirt = {
        enable = any (dom: dom.enable) (attrValues cfg.dom);

        connections."qemu:///system".domains =
          let
            makeDomain = def: {
              active = true;
              restart = false;
              definition = flakes.nixvirt.lib.domain.writeXML def;
            };
          in
          map makeDomain (attrValues (filterAttrs (name: _: cfg.dom.${name}.enable) doms));

        swtpm.enable = true;
      };

      libvirtd = {
        enable = true;

        qemu = {
          runAsRoot = false;

          ovmf.enable = true;
          swtpm.enable = true;
        };
      };
    };
  };
}