forked from NixOS/nixops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirtd.nix
161 lines (134 loc) · 4.18 KB
/
libvirtd.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
{ config, pkgs, lib, ... }:
with lib;
let
sz = toString config.deployment.libvirtd.baseImageSize;
base_image = import ./libvirtd-image.nix { size = sz; };
the_key = builtins.getEnv "NIXOPS_LIBVIRTD_PUBKEY";
ssh_image = pkgs.vmTools.runInLinuxVM (
pkgs.runCommand "libvirtd-ssh-image"
{ memSize = 768;
preVM =
''
mkdir $out
diskImage=$out/image
${pkgs.vmTools.qemu}/bin/qemu-img create -f qcow2 -b ${base_image}/disk.qcow2 $diskImage
'';
buildInputs = [ pkgs.utillinux ];
postVM =
''
mv $diskImage $out/disk.qcow2
'';
}
''
mkdir /mnt
mount /dev/vda1 /mnt
mkdir -p /mnt/etc/ssh/authorized_keys.d
echo '${the_key}' > /mnt/etc/ssh/authorized_keys.d/root
umount /mnt
''
);
in
{
###### interface
options = {
deployment.libvirtd.storagePool = mkOption {
type = types.str;
default = "default";
description = ''
The storage pool where the virtual disk is be created.
'';
};
deployment.libvirtd.URI = mkOption {
type = types.str;
default = "qemu:///system";
description = ''
Connection URI.
'';
};
deployment.libvirtd.vcpu = mkOption {
default = 1;
type = types.int;
description = ''
Number of Virtual CPUs.
'';
};
deployment.libvirtd.memorySize = mkOption {
default = 512;
type = types.int;
description = ''
Memory size (M) of virtual machine.
'';
};
deployment.libvirtd.headless = mkOption {
default = false;
description = ''
If set VM is started in headless mode,
i.e., without a visible display on the host's desktop.
'';
};
deployment.libvirtd.baseImageSize = mkOption {
default = 10;
type = types.int;
description = ''
The size (G) of base image of virtual machine.
'';
};
deployment.libvirtd.baseImage = mkOption {
default = null;
example = "/home/alice/base-disk.qcow2";
type = with types; nullOr path;
description = ''
The disk is created using the specified
disk image as a base.
'';
};
deployment.libvirtd.networks = mkOption {
default = [ "default" ];
type = types.listOf types.str;
description = "Names of libvirt networks to attach the VM to.";
};
deployment.libvirtd.extraDevicesXML = mkOption {
default = "";
type = types.str;
description = "Additional XML appended at the end of device tag in domain xml. See https://libvirt.org/formatdomain.html";
};
deployment.libvirtd.extraDomainXML = mkOption {
default = "";
type = types.str;
description = "Additional XML appended at the end of domain xml. See https://libvirt.org/formatdomain.html";
};
deployment.libvirtd.domainType = mkOption {
default = "kvm";
type = types.str;
description = "Specify the type of libvirt domain to create (see '$ virsh capabilities | grep domain' for valid domain types";
};
deployment.libvirtd.cmdline = mkOption {
default = "";
type = types.str;
description = "Specify the kernel cmdline (valid only with the kernel setting).";
};
deployment.libvirtd.initrd = mkOption {
default = "";
type = types.str;
description = "Specify the kernel initrd (valid only with the kernel setting).";
};
deployment.libvirtd.kernel = mkOption {
default = "";
type = types.str; # with types; nullOr path;
description = "Specify the host kernel to launch (valid for kvm).";
};
};
###### implementation
config = mkIf (config.deployment.targetEnv == "libvirtd") {
deployment.libvirtd.baseImage = mkDefault ssh_image;
nixpkgs.system = mkOverride 900 "x86_64-linux";
fileSystems."/".device = "/dev/disk/by-label/nixos";
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/sda";
boot.loader.timeout = 0;
services.openssh.enable = true;
services.openssh.startWhenNeeded = false;
services.openssh.extraConfig = "UseDNS no";
deployment.hasFastConnection = true;
};
}