From 3a61c7522d2bf7b295ef5661a48af85138ab5da4 Mon Sep 17 00:00:00 2001 From: ahuston-0 Date: Sat, 25 May 2024 15:19:01 -0400 Subject: [PATCH] add basic guest config (warning CI is definitely failing lol) Signed-off-by: ahuston-0 --- lib/default.nix | 2 +- lib/microvms.nix | 63 ++++++++++++++++++++++++++++++ modules/opt/k3s-agent.nix | 1 + modules/opt/k3s-server.nix | 1 + modules/opt/microvm-host.nix | 9 +---- systems/palatine-hill/microvms.nix | 18 +++++++++ 6 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 lib/microvms.nix create mode 100644 modules/opt/k3s-agent.nix create mode 100644 modules/opt/k3s-server.nix diff --git a/lib/default.nix b/lib/default.nix index 35d23a8..b0b4b63 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -3,7 +3,7 @@ # create rad-dev namespace for lib rad-dev = rec { systems = import ./systems.nix { inherit lib; }; - + microvm = import ./microvms.nix { inherit lib; }; # any(), but checks if any value in the list is true # # args: diff --git a/lib/microvms.nix b/lib/microvms.nix new file mode 100644 index 0000000..edc6145 --- /dev/null +++ b/lib/microvms.nix @@ -0,0 +1,63 @@ +{ lib, ... }: +rec { + genK3SFromList = + server-config: agent-config: + { + host, + ipv4, + server ? false, + }@args: + lib.mapAttrs ( + args: + lib.rad-dev.microvms.genMicroVM args.host args.ipv4 "x86_64-linux" ( + if server then import server-config else import agent-config + ) + ) args; + + genMicroVM = + hostName: ipv4: system: vm-config: + # microvm refers to microvm.nixosModules + + { + config, + pkgs, + lib, + ... + }: + { + ${hostName} = { + # The package set to use for the microvm. This also determines the microvm's architecture. + # Defaults to the host system's package set if not given. + pkgs = import pkgs { inherit system; }; + + # (Optional) A set of special arguments to be passed to the MicroVM's NixOS modules. + #specialArgs = {}; + + # The configuration for the MicroVM. + # Multiple definitions will be merged as expected. + config = { + # It is highly recommended to share the host's nix-store + # with the VMs to prevent building huge images. + microvm.shares = [ + { + source = "/nix/store"; + mountPoint = "/nix/.ro-store"; + tag = "ro-store"; + proto = "virtiofs"; + } + ]; + + networking = { + inherit hostName; + interfaces.ether.ipv4.addreses = { + address = ipv4; + prefixLength = 24; + }; + }; + + # Any other configuration for your MicroVM + # [...] + } // vm-config; + }; + }; +} diff --git a/modules/opt/k3s-agent.nix b/modules/opt/k3s-agent.nix new file mode 100644 index 0000000..c915eb0 --- /dev/null +++ b/modules/opt/k3s-agent.nix @@ -0,0 +1 @@ +{ ... }: { } diff --git a/modules/opt/k3s-server.nix b/modules/opt/k3s-server.nix new file mode 100644 index 0000000..c915eb0 --- /dev/null +++ b/modules/opt/k3s-server.nix @@ -0,0 +1 @@ +{ ... }: { } diff --git a/modules/opt/microvm-host.nix b/modules/opt/microvm-host.nix index 42c9a0f..e5128c1 100644 --- a/modules/opt/microvm-host.nix +++ b/modules/opt/microvm-host.nix @@ -12,7 +12,7 @@ in imports = [ microvm.host ]; options.rad-dev.microvm-host = { vms = lib.mkOption { - type = lib.types.attrset; + type = lib.types.attrs; default = { }; description = "A list of VMs to construct on the host"; }; @@ -20,13 +20,6 @@ in config = { networking.useNetworkd = true; microvm.vms = cfg.vms; - microvm.shares = [ - { - tag = "ro-store"; - source = "/nix/store"; - mountPoint = "/nix/.ro-store"; - } - ]; # TODO: deprecate this once we have syslog forwarders systemd.tmpfiles.rules = map ( diff --git a/systems/palatine-hill/microvms.nix b/systems/palatine-hill/microvms.nix index 9cad8d2..fdcb139 100644 --- a/systems/palatine-hill/microvms.nix +++ b/systems/palatine-hill/microvms.nix @@ -2,8 +2,26 @@ config, lib, pkgs, + src, ... }: +let + inherit (lib.rad-dev.microvm) genFromList; +in + { # rad-dev.microvm-host.enable = true; + rad-dev.microvm-host.vms = ( + genFromList (src + "/modules/opt/k3s-server.nix") (src + "/modules/opt/k3s-agent.nix") [ + { + host = "ph-server-1"; + ipv4 = "192.168.69.10"; + server = true; + } + { + host = "ph-agent-1"; + ipv4 = "192.168.69.30"; + } + ] + ); }