2024-05-22 16:47:55 -04:00
|
|
|
# Enables virtualization via QEMU/KVM
|
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-06-24 11:38:28 -04:00
|
|
|
cfg = config.aux.system.services.virtualization;
|
2024-05-22 16:47:55 -04:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-06-24 11:38:28 -04:00
|
|
|
aux.system.services.virtualization = {
|
2024-09-08 11:58:56 -04:00
|
|
|
enable = lib.mkEnableOption "Enables virtualization tools on this host.";
|
2024-06-25 14:13:15 -04:00
|
|
|
host = {
|
2024-09-08 11:58:56 -04:00
|
|
|
enable = lib.mkEnableOption "Enables virtual machine hosting.";
|
2024-06-25 14:13:15 -04:00
|
|
|
user = lib.mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The default user to add as a KVM admin.";
|
|
|
|
};
|
|
|
|
vmBuilds = {
|
2024-09-08 11:58:56 -04:00
|
|
|
enable = lib.mkEnableOption "Enables builds via `nixos-rebuild build-vm` on this host.";
|
2024-06-25 14:13:15 -04:00
|
|
|
cores = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
description = "How many cores to assign to `nixos-rebuild build-vm` builds. Defaults to 2.";
|
|
|
|
default = 2;
|
|
|
|
};
|
|
|
|
ram = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
description = "How much RAM (in MB) to assign to `nixos-rebuild build-vm` builds. Defaults to 2GB.";
|
|
|
|
default = 2048;
|
|
|
|
};
|
|
|
|
};
|
2024-05-22 16:47:55 -04:00
|
|
|
};
|
2024-06-25 14:13:15 -04:00
|
|
|
|
2024-05-22 16:47:55 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-06-25 14:13:15 -04:00
|
|
|
config = lib.mkMerge [
|
2024-08-02 17:55:48 -04:00
|
|
|
{ programs.virt-manager.enable = cfg.enable; }
|
2024-06-25 14:13:15 -04:00
|
|
|
(lib.mkIf (cfg.host.enable || cfg.host.vmBuilds.enable) {
|
|
|
|
virtualisation = {
|
|
|
|
libvirtd = {
|
|
|
|
enable = true;
|
|
|
|
qemu = {
|
|
|
|
package = pkgs.qemu_kvm;
|
|
|
|
swtpm.enable = true;
|
2024-10-16 16:12:40 -04:00
|
|
|
ovmf = {
|
|
|
|
enable = true;
|
|
|
|
packages = [ pkgs.OVMFFull.fd ];
|
|
|
|
};
|
2024-06-25 14:13:15 -04:00
|
|
|
};
|
2024-05-22 16:47:55 -04:00
|
|
|
};
|
2024-06-25 14:13:15 -04:00
|
|
|
spiceUSBRedirection.enable = true;
|
2024-05-22 16:47:55 -04:00
|
|
|
};
|
|
|
|
|
2024-06-25 14:13:15 -04:00
|
|
|
users.users.${cfg.host.user}.extraGroups = [ "libvirtd" ];
|
2024-05-22 16:47:55 -04:00
|
|
|
|
2024-06-25 14:13:15 -04:00
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
spice
|
|
|
|
spice-gtk
|
|
|
|
spice-protocol
|
|
|
|
];
|
2024-05-22 19:31:30 -04:00
|
|
|
|
2024-06-25 14:13:15 -04:00
|
|
|
# Allow the default bridge interface to access the network
|
|
|
|
networking.firewall.trustedInterfaces = [ "virbr0" ];
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.host.vmBuilds.enable {
|
|
|
|
virtualisation.vmVariant.virtualisation = {
|
|
|
|
memorySize = cfg.host.vmBuilds.ram;
|
|
|
|
cores = cfg.host.vmBuilds.cores;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2024-05-22 16:47:55 -04:00
|
|
|
}
|