From 63ee06c308ca98c171e22e3a45892e13e6d46ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Barnouin?= Date: Wed, 16 Oct 2024 14:33:27 +0200 Subject: [PATCH] Separate microvm and LXC config --- systems/default.nix | 81 +++------------------------------------------ systems/lxc.nix | 19 +++++++++++ systems/microvm.nix | 81 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 76 deletions(-) create mode 100644 systems/lxc.nix create mode 100644 systems/microvm.nix diff --git a/systems/default.nix b/systems/default.nix index 11d68d3..33c1d8b 100644 --- a/systems/default.nix +++ b/systems/default.nix @@ -1,78 +1,7 @@ -{ inputs, lib, config, microvm, ... }: -let - cfg = config.services.vm; -in +{ inputs, ... }: { - options.services.vm = { - enable = lib.mkEnableOption "Enable minimal config"; - hostname = lib.mkOption { - type = lib.types.str; - description = "The VM hostname"; - }; - vm_ip = lib.mkOption { - type = lib.types.str; - description = "The VM IP address"; - }; - macAddr = lib.mkOption { - type = lib.types.str; - description = "The VM MAC Address"; - }; - vm_mem = lib.mkOption { - type = lib.types.int; - description = "The VM memory count"; - default = 0; - }; - vm_cpu = lib.mkOption { - type = lib.types.int; - description = "The VM CPU count"; - default = 1; - }; - }; - config = lib.mkIf cfg.enable { - microvm = { - vcpu = cfg.vm_cpu; - balloonMem = cfg.vm_mem; - volumes = [ - { - mountPoint = "/var"; - image = "/var/lib/microvms/${cfg.hostname}/var.img"; - size = 8192; - } - ]; - shares = [ - { - proto = "virtiofs"; - tag = "ro-store"; - source = "/nix/store"; - mountPoint = "/nix/.ro-store"; - } - { - proto = "virtiofs"; - tag = "${cfg.hostname}-env"; - source = "/var/lib/microvms/${cfg.hostname}/env"; - mountPoint = "/run/secrets/${cfg.hostname}"; - } - ]; - interfaces = [ { - type = "tap"; - id = "vm-${cfg.hostname}"; - mac = "${cfg.macAddr}"; - } ]; - - hypervisor = "qemu"; - socket = "control.socket"; - }; - systemd.network.enable = true; - - systemd.network.networks."20-lan" = { - matchConfig.Type = "ether"; - networkConfig = { - Address = ["${cfg.vm_ip}/24"]; - Gateway = "192.168.1.254"; - DNS = ["192.168.1.254"]; - IPv6AcceptRA = true; - DHCP = "no"; - }; - }; - }; + imports = [ + ./microvm.nix + ./lxc.nix + ]; } diff --git a/systems/lxc.nix b/systems/lxc.nix new file mode 100644 index 0000000..a505e17 --- /dev/null +++ b/systems/lxc.nix @@ -0,0 +1,19 @@ +{ inputs, lib, config, modulesPath, ... }: +let + cfg = config.services.vm; +in +{ + options.services.ct = { + enable = lib.mkEnableOption "Enable LXC container config"; + }; + config = lib.mkIf cfg.enable { + + imports = [ (modulesPath + "/virtualisation/proxmox-lxc.nix") ]; + systemd.suppressedSystemUnits = [ + "dev-mqueue.mount" + "sys-kernel-debug.mount" + "sys-fs-fuse-connections.mount" + ]; + + }; +} diff --git a/systems/microvm.nix b/systems/microvm.nix new file mode 100644 index 0000000..9dbc77f --- /dev/null +++ b/systems/microvm.nix @@ -0,0 +1,81 @@ +{ inputs, lib, config, microvm, modulesPath, ... }: +let + cfg = config.services.vm; +in +{ + options.services.ct = { + enable = lib.mkEnableOption "Enable LXC container config"; + }; + options.services.vm = { + enable = lib.mkEnableOption "Enable NixOS microvm config"; + hostname = lib.mkOption { + type = lib.types.str; + description = "The VM hostname"; + }; + vm_ip = lib.mkOption { + type = lib.types.str; + description = "The VM IP address"; + }; + macAddr = lib.mkOption { + type = lib.types.str; + description = "The VM MAC Address"; + }; + vm_mem = lib.mkOption { + type = lib.types.int; + description = "The VM memory count"; + default = 0; + }; + vm_cpu = lib.mkOption { + type = lib.types.int; + description = "The VM CPU count"; + default = 1; + }; + }; + config = lib.mkIf cfg.enable { + microvm = { + vcpu = cfg.vm_cpu; + balloonMem = cfg.vm_mem; + volumes = [ + { + mountPoint = "/var"; + image = "/var/lib/microvms/${cfg.hostname}/var.img"; + size = 8192; + } + ]; + shares = [ + { + proto = "virtiofs"; + tag = "ro-store"; + source = "/nix/store"; + mountPoint = "/nix/.ro-store"; + } + { + proto = "virtiofs"; + tag = "${cfg.hostname}-env"; + source = "/var/lib/microvms/${cfg.hostname}/env"; + mountPoint = "/run/secrets/${cfg.hostname}"; + } + ]; + interfaces = [ { + type = "tap"; + id = "vm-${cfg.hostname}"; + mac = "${cfg.macAddr}"; + } ]; + + hypervisor = "qemu"; + socket = "control.socket"; + }; + systemd.network.enable = true; + + systemd.network.networks."20-lan" = { + matchConfig.Type = "ether"; + networkConfig = { + Address = ["${cfg.vm_ip}/24"]; + Gateway = "192.168.1.254"; + DNS = ["192.168.1.254"]; + IPv6AcceptRA = true; + DHCP = "no"; + }; + }; + }; +}