feat: add microvm module to simplify setup

This commit is contained in:
oddlama 2023-05-08 17:13:40 +02:00
parent 72a34cac01
commit d842d25eb9
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
8 changed files with 99 additions and 28 deletions

2
flake.lock generated
View file

@ -258,7 +258,7 @@
}, },
"locked": { "locked": {
"lastModified": 1682972682, "lastModified": 1682972682,
"narHash": "sha256-IYInF92rLqqVk/dyugT2QVbVeEfYx1rbBJjbUlRD8oE=", "narHash": "sha256-+XyTwr4jZuDlT5l/0LynDVtvdWj8WmAwDEP/2rZ+1hk=",
"type": "git", "type": "git",
"url": "file:///root/projects/microvm.nix" "url": "file:///root/projects/microvm.nix"
}, },

View file

@ -105,7 +105,16 @@
}; };
colmena = import ./nix/colmena.nix inputs; colmena = import ./nix/colmena.nix inputs;
inherit ((colmena.lib.makeHive self.colmena).introspect (x: x)) nodes; colmenaNodes = ((colmena.lib.makeHive self.colmena).introspect (x: x)).nodes;
microvmNodes =
nixpkgs.lib.concatMapAttrs (
nodeName: nodeAttrs:
nixpkgs.lib.mapAttrs'
(n: nixpkgs.lib.nameValuePair "${nodeName}-microvm-${n}")
(self.colmenaNodes.${nodeName}.config.microvm.vms or {})
)
self.colmenaNodes;
nodes = self.colmenaNodes // self.microvmNodes;
# Collect installer packages # Collect installer packages
inherit inherit

View file

@ -11,6 +11,7 @@
./xdg.nix ./xdg.nix
../../../modules/wireguard.nix ../../../modules/wireguard.nix
../../../modules/microvms.nix
]; ];
home-manager = { home-manager = {

View file

@ -2,6 +2,7 @@
inputs, inputs,
lib, lib,
nodeName, nodeName,
nodePath,
... ...
}: { }: {
# IP address math library # IP address math library
@ -30,7 +31,7 @@
# current system due to yubikey availability. # current system due to yubikey availability.
forceRekeyOnSystem = builtins.extraBuiltins.unsafeCurrentSystem; forceRekeyOnSystem = builtins.extraBuiltins.unsafeCurrentSystem;
hostPubkey = let hostPubkey = let
pubkeyPath = ../.. + "/${nodeName}/secrets/host.pub"; pubkeyPath = "${nodePath}/secrets/host.pub";
in in
lib.mkIf (lib.pathExists pubkeyPath || lib.trace "Missing pubkey for ${nodeName}: ${toString pubkeyPath} not found, using dummy replacement key for now." false) lib.mkIf (lib.pathExists pubkeyPath || lib.trace "Missing pubkey for ${nodeName}: ${toString pubkeyPath} not found, using dummy replacement key for now." false)
pubkeyPath; pubkeyPath;

View file

@ -2,7 +2,6 @@
config, config,
inputs, inputs,
lib, lib,
microvm,
nixos-hardware, nixos-hardware,
pkgs, pkgs,
... ...
@ -11,8 +10,6 @@
nixos-hardware.common-cpu-intel nixos-hardware.common-cpu-intel
nixos-hardware.common-pc-ssd nixos-hardware.common-pc-ssd
microvm.host
../common/core ../common/core
../common/hardware/intel.nix ../common/hardware/intel.nix
../common/hardware/physical.nix ../common/hardware/physical.nix
@ -28,21 +25,7 @@
boot.initrd.availableKernelModules = ["xhci_pci" "ahci" "nvme" "usbhid" "usb_storage" "sd_mod" "sdhci_pci" "r8169"]; boot.initrd.availableKernelModules = ["xhci_pci" "ahci" "nvme" "usbhid" "usb_storage" "sd_mod" "sdhci_pci" "r8169"];
microvm.vms = { extra.microvms.test.system = "x86_64-linux";
test = let
node =
(import ../../nix/generate-node.nix inputs)
"ward-microvm-test" {
system = "x86_64-linux";
config = ./microvms/test;
};
in {
inherit (node) pkgs specialArgs;
config = {
inherit (node) imports;
};
};
};
#services.authelia.instances.main = { #services.authelia.instances.main = {
# enable = true; # enable = true;

View file

@ -8,8 +8,6 @@
... ...
}: { }: {
imports = [ imports = [
microvm.microvm
../../../common/core ../../../common/core
../../../../users/root ../../../../users/root

78
modules/microvms.nix Normal file
View file

@ -0,0 +1,78 @@
{
inputs,
config,
lib,
nodeName,
nodePath,
microvm,
pkgs,
...
}: let
inherit
(lib)
mapAttrs
mdDoc
mkDefault
mkForce
mkIf
mkOption
types
;
cfg = config.extra.microvms;
defineMicrovm = vmName: vmCfg: let
node =
(import ../nix/generate-node.nix inputs)
"${nodeName}-microvm-${vmName}" {
inherit (vmCfg) system;
config = nodePath + "/microvms/${vmName}";
};
in {
inherit (node) pkgs specialArgs;
config = {
imports = [microvm.microvm] ++ node.imports;
microvm = {
hypervisor = mkDefault "cloud-hypervisor";
# Share the nix-store of the host
shares = [
{
source = "/nix/store";
mountPoint = "/nix/.ro-store";
tag = "ro-store";
proto = "virtiofs";
}
];
};
# TODO change once microvms are compatible with stage-1 systemd
boot.initrd.systemd.enable = mkForce false;
};
};
in {
imports = [microvm.host];
options.extra.microvms = mkOption {
default = {};
description = "Provides a base configuration for MicroVMs.";
type = types.attrsOf (types.submodule {
options = {
system = mkOption {
type = types.str;
description = mdDoc "The system that this microvm should use";
};
};
});
};
config = {
microvm = {
host.enable = cfg != {};
declarativeUpdates = true;
restartIfChanged = true;
vms = mkIf (cfg != {}) (mapAttrs defineMicrovm cfg);
};
};
}

View file

@ -14,21 +14,22 @@
} @ inputs: let } @ inputs: let
inherit (nixpkgs.lib) optionals; inherit (nixpkgs.lib) optionals;
in in
nodeName: nodeMeta: { nodeName: nodeMeta: let
nodePath = nodeMeta.config or (../hosts + "/${nodeName}");
in {
inherit (nodeMeta) system; inherit (nodeMeta) system;
pkgs = self.pkgs.${nodeMeta.system}; pkgs = self.pkgs.${nodeMeta.system};
specialArgs = { specialArgs = {
inherit (nixpkgs) lib; inherit (nixpkgs) lib;
inherit (self) extraLib nodes stateVersion; inherit (self) extraLib nodes stateVersion;
inherit inputs; inherit inputs nodeName nodePath;
inherit nodeName;
secrets = self.secrets.content; secrets = self.secrets.content;
nodeSecrets = self.secrets.content.nodes.${nodeName}; nodeSecrets = self.secrets.content.nodes.${nodeName} or {};
nixos-hardware = nixos-hardware.nixosModules; nixos-hardware = nixos-hardware.nixosModules;
microvm = microvm.nixosModules; microvm = microvm.nixosModules;
}; };
imports = [ imports = [
(nodeMeta.config or (../hosts + "/${nodeName}")) nodePath # default module
agenix.nixosModules.default agenix.nixosModules.default
agenix-rekey.nixosModules.default agenix-rekey.nixosModules.default
disko.nixosModules.disko disko.nixosModules.disko