2024-05-22 20:47:55 +00:00
|
|
|
# Enables virtualization via QEMU/KVM
|
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-06-24 15:38:28 +00:00
|
|
|
cfg = config.aux.system.services.virtualization;
|
2024-05-22 20:47:55 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-06-24 15:38:28 +00:00
|
|
|
aux.system.services.virtualization = {
|
2024-06-25 18:13:15 +00:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Enables virtualization tools on this host.");
|
|
|
|
host = {
|
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Enables virtual machine hosting.");
|
|
|
|
user = lib.mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The default user to add as a KVM admin.";
|
|
|
|
};
|
|
|
|
vmBuilds = {
|
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Enables builds via `nixos-rebuild build-vm` on this host.");
|
|
|
|
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;
|
|
|
|
ovmf.enable = true;
|
|
|
|
ovmf.packages = [ pkgs.OVMFFull.fd ];
|
|
|
|
};
|
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
|
|
|
}
|