2024-02-01 21:57:18 +00:00
|
|
|
{ pkgs, lib, config, hypervisorConfig, ... }: {
|
2024-02-07 22:15:48 +00:00
|
|
|
imports = [ ./docker_swarm.nix ];
|
|
|
|
|
2024-01-30 21:32:09 +00:00
|
|
|
options.lab.vm = {
|
2024-02-01 21:57:18 +00:00
|
|
|
baseMACAddress = lib.mkOption {
|
|
|
|
default = "BA:DB:EE:F0:00:00";
|
2024-01-29 21:21:15 +00:00
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
2024-02-01 21:57:18 +00:00
|
|
|
Base MAC address for VMs in the DMZ.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
id = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
description = ''
|
|
|
|
Unique identifier of this VM from wich the MAC address is derived.
|
2024-01-29 21:21:15 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-30 21:32:09 +00:00
|
|
|
staticNetworking = lib.mkOption {
|
2024-01-29 21:21:15 +00:00
|
|
|
default = false;
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = ''
|
2024-01-30 21:32:09 +00:00
|
|
|
Whether this VM has static networking configuration applied.
|
|
|
|
Routing is prepopulated, but IP addresses have to be set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
staticIPv4 = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
Static IPv4 address for the VM.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
staticIPv6 = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
Static IPv6 address for the VM.
|
2024-01-29 21:21:15 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-02-07 22:15:48 +00:00
|
|
|
|
|
|
|
shares = lib.mkOption {
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Directories mounted on the VM using VirtioFS.
|
|
|
|
'';
|
|
|
|
type = lib.types.listOf (lib.types.submodule ({ config, ... }: {
|
|
|
|
options = {
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
The name of the directory share.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mountPoint = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
The mount point of the directory share inside the virtual machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
2024-01-28 11:06:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
system.stateVersion = hypervisorConfig.system.stateVersion;
|
|
|
|
|
2024-02-07 22:15:48 +00:00
|
|
|
lab.vm.shares = [{
|
|
|
|
name = "host_keys";
|
|
|
|
mountPoint = "/etc/ssh/host_keys";
|
|
|
|
}];
|
|
|
|
|
2024-02-07 21:22:10 +00:00
|
|
|
services.openssh = {
|
|
|
|
hostKeys = [{
|
|
|
|
path = "/etc/ssh/host_keys/ssh_host_ed25519_key";
|
|
|
|
type = "ed25519";
|
2024-01-28 11:06:30 +00:00
|
|
|
}];
|
|
|
|
|
2024-02-07 21:22:10 +00:00
|
|
|
extraConfig = ''
|
|
|
|
HostKey /etc/ssh/host_keys/ssh_host_ed25519_key
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
microvm = {
|
2024-02-08 22:44:36 +00:00
|
|
|
# TODO: make this dependent on the host CPU
|
|
|
|
vcpu = 4;
|
|
|
|
|
2024-02-07 22:15:48 +00:00
|
|
|
shares = [{
|
|
|
|
source = "/nix/store";
|
|
|
|
mountPoint = "/nix/.ro-store";
|
|
|
|
tag = "ro-store";
|
|
|
|
proto = "virtiofs";
|
|
|
|
}] ++ map
|
|
|
|
(share: {
|
|
|
|
source = "/var/lib/microvms/${config.networking.hostName}/shares/${share.name}";
|
|
|
|
mountPoint = share.mountPoint;
|
|
|
|
tag = share.name;
|
2024-02-07 21:22:10 +00:00
|
|
|
proto = "virtiofs";
|
2024-02-07 22:15:48 +00:00
|
|
|
})
|
|
|
|
config.lab.vm.shares;
|
2024-02-07 21:22:10 +00:00
|
|
|
|
2024-01-28 11:06:30 +00:00
|
|
|
interfaces = [{
|
|
|
|
type = "tap";
|
2024-01-28 11:55:58 +00:00
|
|
|
id = "vm-${config.networking.hostName}";
|
2024-02-01 21:57:18 +00:00
|
|
|
mac = pkgs.lib.net.mac.add config.lab.vm.id config.lab.vm.baseMACAddress;
|
2024-01-28 11:06:30 +00:00
|
|
|
}];
|
|
|
|
};
|
2024-01-29 21:21:15 +00:00
|
|
|
|
2024-01-30 21:32:09 +00:00
|
|
|
networking.useDHCP = false;
|
2024-01-29 21:21:15 +00:00
|
|
|
|
2024-01-31 20:58:23 +00:00
|
|
|
systemd.network =
|
|
|
|
let
|
|
|
|
cfg = config.lab.networking;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
enable = true;
|
2024-01-29 21:21:15 +00:00
|
|
|
|
2024-01-31 20:58:23 +00:00
|
|
|
networks = {
|
|
|
|
"30-main-nic" = {
|
|
|
|
matchConfig.Name = "en*";
|
2024-01-29 21:21:15 +00:00
|
|
|
|
2024-01-31 20:58:23 +00:00
|
|
|
networkConfig = {
|
|
|
|
IPv6AcceptRA = ! config.lab.vm.staticNetworking;
|
|
|
|
DHCP = lib.mkIf (! config.lab.vm.staticNetworking) "yes";
|
2024-01-30 21:32:09 +00:00
|
|
|
|
2024-01-31 20:58:23 +00:00
|
|
|
Address = lib.mkIf config.lab.vm.staticNetworking [
|
|
|
|
"${ config.lab.vm.staticIPv4}/${cfg.dmz.ipv4.prefixLength}"
|
|
|
|
"${config.lab.vm.staticIPv6}/${cfg.dmz.ipv6.prefixLength}"
|
|
|
|
];
|
|
|
|
|
|
|
|
DNS = lib.mkIf config.lab.vm.staticNetworking [
|
|
|
|
cfg.dmz.ipv4.router
|
|
|
|
cfg.dmz.ipv6.router
|
|
|
|
];
|
|
|
|
};
|
2024-01-30 21:32:09 +00:00
|
|
|
|
2024-01-31 20:58:23 +00:00
|
|
|
routes = lib.mkIf config.lab.vm.staticNetworking [
|
|
|
|
{
|
|
|
|
routeConfig = {
|
|
|
|
Gateway = cfg.dmz.ipv4.router;
|
|
|
|
Destination = "0.0.0.0/0";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
routeConfig = {
|
|
|
|
Gateway = cfg.dmz.ipv6.router;
|
|
|
|
Destination = "::/0";
|
|
|
|
};
|
|
|
|
}
|
2024-01-30 21:32:09 +00:00
|
|
|
];
|
2024-01-29 21:21:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-01-28 11:06:30 +00:00
|
|
|
};
|
|
|
|
}
|