about summary refs log tree commit diff
path: root/nix/modules/libvirtnix/default.nix
blob: a9989664d74e0d8dc7fa12c2d7fe1d2bcdfb52e4 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.emile.libvirtnix;
in
{
  options.services.emile.libvirtnix = with lib; {
    enable = mkEnableOption "enable libvirtnix";
    instances = mkOption {
      type = types.attrsOf (
        types.submodule (
          { name, ... }:
          {
            options = {
              enable = mkEnableOption "enable this instance";
              domain = mkOption {
                type = types.submodule (import ./domain.nix);
              };
            };
          }
        )
      );
      default = { };
      description = ''
        A full libvirt config, statically defined using nix.
      '';
      example = ''
        {
          domain = {
            name = "vm";
          };
        }
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services = lib.mapAttrs' (
      name: guest:
      lib.nameValuePair "libvirtd-guest-${name}" {
        after = [ "libvirtd.service" ];
        requires = [ "libvirtd.service" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = "yes";
        };
        script =
          let
            xml =
              pkgs.writeText "libvirt-guest-${name}.xml" ''<domain ''
              + (lib.optionalString (guest.domain.type != "") "type='${guest.domain.type}' ")
              + (lib.optionalString (guest.domain.id != 0) "id='${guest.domain.type}'")
              + ''>''
              + (lib.optionalString (guest.domain.name != "") "<name>${guest.domain.name}</name>")
              + (lib.optionalString (guest.domain.name != "") "<name>${guest.domain.name}</name>")
              + (lib.optionalString (guest.domain.uuid != "") "<uuid>${guest.domain.uuid}</uuid>")
              + (lib.optionalString (guest.domain.genid != "") "<genid>${guest.domain.genid}</genid>")
              + (lib.optionalString (guest.domain.title != "") "<title>${guest.domain.title}</title>")
              + (lib.optionalString (
                guest.domain.description != ""
              ) "<description>${guest.domain.description}</description>")
              + (lib.optionalString (guest.domain.metadata != "") "${guest.domain.metadata}")
              + ''
                  <os>
                    <type>hvm</type>
                  </os>
                  <memory unit="GiB">${toString guest.domain.memory}</memory>
                  <devices>
                    <disk type="volume">
                      <source volume="guest-${name}"/>
                      <target dev="vda" bus="virtio"/>
                    </disk>
                    <graphics type="spice" autoport="yes"/>
                    <input type="keyboard" bus="usb"/>
                  </devices>
                  <features>
                    <acpi/>
                  </features>
                </domain>
              '';
          in
          ''
            uuid="$(${pkgs.libvirt}/bin/virsh domuuid '${name}' || true)"
            ${pkgs.libvirt}/bin/virsh define <(sed "s/UUID/$uuid/" '${xml}')
            ${pkgs.libvirt}/bin/virsh start '${name}'
          '';
        preStop = ''
          ${pkgs.libvirt}/bin/virsh shutdown '${name}'
          let "timeout = $(date +%s) + 10"
          while [ "$(${pkgs.libvirt}/bin/virsh list --name | grep --count '^${name}$')" -gt 0 ]; do
            if [ "$(date +%s)" -ge "$timeout" ]; then
              # Meh, we warned it...
              ${pkgs.libvirt}/bin/virsh destroy '${name}'
            else
              # The machine is still running, let's give it some time to shut down
              sleep 0.5
            fi
          done
        '';
      }
    ) config.services.emile.libvirtnix.instances;
  };
}