summaryrefslogtreecommitdiff
path: root/custom/libvirtd-guests.nix
diff options
context:
space:
mode:
Diffstat (limited to 'custom/libvirtd-guests.nix')
-rw-r--r--custom/libvirtd-guests.nix84
1 files changed, 84 insertions, 0 deletions
diff --git a/custom/libvirtd-guests.nix b/custom/libvirtd-guests.nix
new file mode 100644
index 00000000..36ffa3c9
--- /dev/null
+++ b/custom/libvirtd-guests.nix
@@ -0,0 +1,84 @@
1{ config, pkgs, lib, utils, ... }:
2
3with utils;
4with lib;
5
6let
7 cfg = virtualisation.libvirtd;
8
9 textfile = with types; coercedTo str (pkgs.writeText "spec.xml") path;
10
11 domain = {
12 options = {
13 xml = mkOption {
14 type =
15 };
16
17 autostart = mkOption {
18 type = types.bool;
19 default = true;
20 };
21 };
22 };
23
24 define = let
25 python = pkgs.python27.withPackages (ps: with ps; [ libvirt ]);
26 in dCfg: ''
27 #!${python}/bin/python
28
29 import libvirt
30 import sys
31
32 conn = libvirt.open(None);
33 if conn == None:
34 print('Failed to open connection to hypervisor', file=sys.stderr)
35 sys.exit(1)
36
37 xmlFile = open(${escapeShellArg dCfg.xml}, 'r')
38 dom = conn.defineXML(xmlFile.read(), 0)
39 xmlFile.close()
40 if dom == None:
41 print('Failed to define domain', file=sys.stderr)
42 sys.exit(1)
43
44 dom.setAutostart(${if dCfg.autostart then "1" else "0"})
45
46 conn.close()
47 sys.exit(0)
48 '';
49in {
50 options = {
51 virtualisation.libvirtd = {
52 domains = mkOption {
53 type = with types; attrsOf (submodule guest);
54 default = {};
55 };
56 };
57 };
58
59 config = mkIf (cfg.domains != {}) {
60 systemd.services."libvirtd-guest@" = {
61 after = [ "libvirtd.service" ];
62 bindsTo = [ "libvirtd.service" ];
63
64 before = [ "libvirt-guests.service" ];
65
66 serviceConfig = {
67 Type = "oneshot";
68 RemainAfterExit = true;
69 };
70
71 path = with pkgs; [ libvirtd ];
72 };
73
74 systemd.services = mapAttrs' (dName: dCfg: nameValuePair ("libvirtd-guest@" + escapeSystemdPath dName + ".service") {
75 serviceConfig = {
76 ExecStart = pkgs.writeScript (dName + ".py") (define dCfg);
77 };
78 }) cfg.domains;
79
80 systemd.services."libvirt-guests.service" = {
81 wants = mapAttrsToList (dName: dCfg: "libvirtd-guest@" + escapeSystemdPath dName + ".service") cfg.domains;
82 };
83 };
84}