-
Notifications
You must be signed in to change notification settings - Fork 21
/
modules.nix
174 lines (171 loc) · 6.82 KB
/
modules.nix
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{ packages, moduleHelperFile }:
let
module = isHomeManager: { config, lib, ... }:
let
cfg = config.virtualisation.libvirt;
defaultConnectionURI = if isHomeManager then "qemu:///session" else "qemu:///system";
mkObjectOption = with lib.types; { singular, plural, extraOptions ? { } }: lib.mkOption
{
type = nullOr (listOf (submodule
{
options = extraOptions //
{
definition = lib.mkOption
{
type = path;
description = "path to " + singular + " definition XML";
};
active = lib.mkOption
{
type = nullOr bool;
default = null;
description = "state to put the " + singular + " in (or null for ignore)";
};
restart = lib.mkOption
{
type = nullOr bool;
default = null;
description = "whether to restart on activation (or null to only restart when changed)";
};
};
}));
default = null;
description = "libvirt " + plural;
};
in
{
options.virtualisation.libvirt = with lib.types;
{
enable = lib.mkOption
{
type = bool;
default = false;
description = "Enable management of libvirt objects";
};
verbose = lib.mkOption
{
type = bool;
default = false;
description = "Verbose output during module activation (for debugging)";
};
swtpm.enable = lib.mkOption
{
type = bool;
default = false;
description = "Make software TPM emulator available to libvirt";
};
connections = lib.mkOption
{
type = attrsOf
(submodule
{
options =
{
domains = mkObjectOption
{
singular = "domain";
plural = "domains";
};
networks = mkObjectOption
{
singular = "network";
plural = "networks";
};
pools = mkObjectOption
{
singular = "pool";
plural = "pools";
extraOptions =
{
volumes = lib.mkOption
{
type = listOf
(submodule
{
options =
{
present = lib.mkOption
{
type = bool;
default = true;
description = "whether the volume should exist";
};
name = lib.mkOption
{
type = nullOr str;
description = "name of the volume (for present=false)";
default = null;
};
definition = lib.mkOption
{
type = nullOr path;
description = "path to volume definition XML";
default = null;
};
};
});
default = [ ];
description = "volumes to create if missing";
};
};
};
};
});
default = { };
description = "set of objects, keyed by hypervisor connection URI (e.g. \"" + defaultConnectionURI + "\")";
};
};
config = lib.mkIf cfg.enable
(
let
concatStr = builtins.concatStringsSep "";
concatStrMap = f: x: concatStr (builtins.map f x);
scriptForConnection = with builtins; connection:
let
opts = getAttr connection cfg.connections;
jsonFile = packages.writeText "nixvirt module script" (builtins.toJSON opts);
verboseFlag = if cfg.verbose then "-v" else "";
in
"${moduleHelperFile} ${verboseFlag} --connect ${connection} ${jsonFile}\n";
extraPackages = [ packages.qemu-utils ] ++ (if cfg.swtpm.enable then [ packages.swtpm ] else [ ]);
extraPaths = concatStrMap (p: "${p}/bin:") extraPackages;
script = "PATH=${extraPaths}$PATH\n" + concatStrMap scriptForConnection (builtins.attrNames cfg.connections);
in
if isHomeManager
then
{
home.packages = extraPackages;
home.activation.NixVirt = lib.hm.dag.entryAfter [ "installPackages" ] script;
}
else
{
environment.systemPackages = extraPackages;
virtualisation.libvirtd =
{
enable = true;
package = lib.mkDefault packages.libvirt;
qemu.swtpm =
if cfg.swtpm.enable then
{
enable = true;
package = packages.swtpm;
}
else { };
};
systemd.services.nixvirt =
{
serviceConfig.Type = "oneshot";
description = "Configure libvirt objects";
wantedBy = [ "multi-user.target" ];
requires = [ "libvirtd.service" ];
after = [ "libvirtd.service" ];
inherit script;
};
}
);
};
in
{
nixosModule = module false;
homeModule = module true;
}