2024-05-22 20:47:55 +00:00
|
|
|
# Enables virtualization via QEMU/KVM
|
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
2024-12-06 16:46:10 +00:00
|
|
|
namespace,
|
2024-05-22 20:47:55 +00:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-12-06 16:46:10 +00:00
|
|
|
cfg = config.${namespace}.services.virtualization;
|
2024-05-22 20:47:55 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-12-06 16:46:10 +00:00
|
|
|
${namespace}.services.virtualization = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables virtualization tools on this host.";
|
2024-06-25 18:13:15 +00:00
|
|
|
host = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables virtual machine hosting.";
|
2024-06-25 18:13:15 +00:00
|
|
|
user = lib.mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The default user to add as a KVM admin.";
|
|
|
|
};
|
|
|
|
vmBuilds = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables builds via `nixos-rebuild build-vm` on this host.";
|
2024-06-25 18:13:15 +00: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 20:47:55 +00:00
|
|
|
};
|
2024-06-25 18:13:15 +00:00
|
|
|
|
2024-05-22 20:47:55 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-06-25 18:13:15 +00:00
|
|
|
config = lib.mkMerge [
|
2024-08-02 21:55:48 +00:00
|
|
|
{ programs.virt-manager.enable = cfg.enable; }
|
2024-06-25 18:13:15 +00: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 20:12:40 +00:00
|
|
|
ovmf = {
|
|
|
|
enable = true;
|
|
|
|
packages = [ pkgs.OVMFFull.fd ];
|
|
|
|
};
|
2024-06-25 18:13:15 +00:00
|
|
|
};
|
2024-05-22 20:47:55 +00:00
|
|
|
};
|
2024-06-25 18:13:15 +00:00
|
|
|
spiceUSBRedirection.enable = true;
|
2024-05-22 20:47:55 +00:00
|
|
|
};
|
|
|
|
|
2024-06-25 18:13:15 +00:00
|
|
|
users.users.${cfg.host.user}.extraGroups = [ "libvirtd" ];
|
2024-05-22 20:47:55 +00:00
|
|
|
|
2024-06-25 18:13:15 +00:00
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
spice
|
|
|
|
spice-gtk
|
|
|
|
spice-protocol
|
|
|
|
];
|
2024-05-22 23:31:30 +00:00
|
|
|
|
2024-06-25 18:13:15 +00: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 20:47:55 +00:00
|
|
|
}
|