-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault.nix
99 lines (98 loc) · 2.88 KB
/
default.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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.kvms;
makeVMService = target: {
name = "kvms-"+target.name;
enable = target.autostart;
value = {
description = target.name+" VM";
serviceConfig = {
Restart = "on-failure";
RestartSec = 2;
};
wantedBy = [ "multi-user.target" ];
script = ''
mkdir -p /var/lib/kvms/
${target.qemubuild}/bin/run-${target.name}-vm
'';
};
};
in {
######### NixOS Options Interface
options = {
kvms = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Enable KVM NixOS VM's.
'';
};
vms = mkOption {
type = types.attrsOf (types.submodule (
{ config, options, name, ... }:
{
options = {
name = mkOption {
type = types.string;
default = name;
description = "VM name.";
};
autostart = mkOption {
type = types.bool;
default = false;
description = "Start this VM on boot.";
};
qemubuild = mkOption {
type = types.path;
description = "qemu-nix build";
};
config = mkOption {
description = "Configrations of NixOS VM's.";
type = lib.mkOptionType {
name = "Toplevel NixOS config for a KVM VM";
merge = loc: defs: (import <nixos/nixos/lib/eval-config.nix> {
modules = let extraConfig = {
networking.hostName = mkDefault name;
virtualisation.graphics = false;
virtualisation.diskImage = "/var/lib/kvms/${name}.qcow2";
imports = [ ./qemu-vm.nix ];
};
in [ extraConfig ] ++ (map (x: x.value) defs);
prefix = [ "kvms" name ];
}).config;
};
};
};
config = mkMerge [
(mkIf options.config.isDefined {
qemubuild = config.config.system.build.vm;
})
];
}
));
default = {};
example = literalExample ''
{ database = {
autostart = true;
config = { config, ... }:
{
services.postgresql.enable = true;
};
};
}
'';
description = ''
A set of NixOS system configurations to be run as KVM virtual machines.
'';
};
};
};
######### Implementation of the interface's options
config = let
VMServices = builtins.listToAttrs (map makeVMService (attrValues config.kvms.vms));
in mkIf cfg.enable {
systemd.services = VMServices;
};
}