diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2018-06-02 18:40:43 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2018-06-02 18:40:43 +0200 |
commit | 656f65c78eb6b2e72711acc35e2b936f4279238f (patch) | |
tree | 0693c39484d8e080ca8c2326aadf4d4edff46c84 /custom/libvirtd-guests.nix | |
parent | cd12041e267ff1a2c1d7cd0fabea1364bc587ec4 (diff) | |
download | nixos-656f65c78eb6b2e72711acc35e2b936f4279238f.tar nixos-656f65c78eb6b2e72711acc35e2b936f4279238f.tar.gz nixos-656f65c78eb6b2e72711acc35e2b936f4279238f.tar.bz2 nixos-656f65c78eb6b2e72711acc35e2b936f4279238f.tar.xz nixos-656f65c78eb6b2e72711acc35e2b936f4279238f.zip |
uucp-notifyclient
Diffstat (limited to 'custom/libvirtd-guests.nix')
-rw-r--r-- | custom/libvirtd-guests.nix | 84 |
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 | |||
3 | with utils; | ||
4 | with lib; | ||
5 | |||
6 | let | ||
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 | ''; | ||
49 | in { | ||
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 | } | ||