1
0
Fork 0
nix-configuration/modules/services/virtualization.nix

76 lines
2.1 KiB
Nix
Raw Normal View History

2024-05-22 20:47:55 +00:00
# Enables virtualization via QEMU/KVM
{
config,
lib,
pkgs,
...
}:
let
cfg = config.aux.system.services.virtualization;
2024-05-22 20:47:55 +00:00
in
{
options = {
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 [
({ programs.virt-manager.enable = cfg.enable; })
(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-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
}