-
Notifications
You must be signed in to change notification settings - Fork 7
/
default.nix
111 lines (96 loc) · 3.36 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
100
101
102
103
104
105
106
107
108
109
110
111
{ config, lib, pkgs, ... }:
with lib;
with import <nixpkgs/nixos/lib/qemu-flags.nix> { inherit pkgs; };
let
system = config.nixpkgs.localSystem.system;
qemu = qemuBinary pkgs.qemu;
vmOptions = import ./vm-options.nix { inherit lib system; };
in {
options = {
boot.isVirtualMachine = mkOption {
type = types.bool;
default = false;
description = ''
Whether this NixOS machine is a QEMU virtual machine running
in another NixOS system.
'';
};
boot.enableVirtualMachines = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable support for NixOS virtual machines.
'';
};
virtualMachines = mkOption {
type = types.attrsOf (types.submodule vmOptions);
default = {};
example = literalExample
''
{ database =
{ config =
{ config, pkgs, ... }:
{ services.postgresql.enable = true;
system.stateVersion = "19.03";
};
};
}
'';
description = ''
A set of NixOS system configurations to be run as virtual
machine. Each VM appears as a service
<literal>vm-<replaceable>name</replaceable></literal>
on the host system, allowing it to be started and stopped via
<command>systemctl</command>.
'';
};
};
config = let
mkBackingImage = cfg: import <nixpkgs/nixos/lib/make-disk-image.nix> {
inherit lib pkgs;
config = cfg.config;
diskSize = cfg.baseImageSize;
format = cfg.baseImageFormat;
};
mkBackingImagePath = cfg: "${mkBackingImage cfg}/nixos.${cfg.baseImageFormat}";
defaultService = {
description = "Virtual Machine '%i'";
path = [ pkgs.qemu_kvm ];
restartIfChanged = mkDefault false;
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/true";
};
mkQemuCommand = cfg: let
switches = cfg.qemuSwitches ++ [
''drive file=${cfg.rootImagePath},if=virtio,aio=threads,format=${cfg.rootImageFormat}''
];
options = map (v: "-${v}") switches;
in concatStringsSep " " ([ qemu ] ++ options);
mkService = cfg: defaultService // {
enable = cfg.autoStart;
wantedBy = optional cfg.autoStart "machines.target";
preStart = let
backingFile = mkBackingImagePath cfg;
# TODO: See if rebase can be made smarter and only do it when neccesary
imgOpts = [
"-f ${cfg.rootImageFormat}" "-F ${cfg.baseImageFormat}" "-b ${backingFile}"
];
imgCreateArgs = [ "${cfg.rootImagePath}" "${toString cfg.rootImageSize}M" ];
imgRebaseArgs = [ "${cfg.rootImagePath}" ];
in ''
if [ ! -f "$root" ]; then
mkdir -p "$(dirname ${cfg.rootImagePath})"
qemu-img create ${concatStringsSep " " (imgOpts ++ imgCreateArgs)}
else
qemu-img rebase ${concatStringsSep " " (imgOpts ++ imgRebaseArgs)}
fi
'';
serviceConfig.ExecStart = mkQemuCommand cfg;
};
mkNamedService = name: cfg: nameValuePair "vm@${name}" (mkService cfg);
in mkIf (config.boot.enableVirtualMachines) {
systemd.targets."multi-user".wants = [ "machines.target" ];
systemd.services = listToAttrs (
[{ name = "vm@"; value = defaultService; }]
++ mapAttrsToList mkNamedService config.virtualMachines);
};
}