From cbcd3c943bad3bb40457c20df897c1f97fb5296a Mon Sep 17 00:00:00 2001 From: oddlama Date: Sun, 21 May 2023 15:08:27 +0200 Subject: [PATCH] refactor: centralize definition of MicroVM node names --- flake.nix | 16 +++++++--------- modules/microvms.nix | 28 ++++++++++++++++++++++++---- modules/repo.nix | 5 +++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/flake.nix b/flake.nix index 03117e0..36bcacc 100644 --- a/flake.nix +++ b/flake.nix @@ -106,15 +106,13 @@ colmena = import ./nix/colmena.nix inputs; colmenaNodes = ((colmena.lib.makeHive self.colmena).introspect (x: x)).nodes; - microvmNodes = - nixpkgs.lib.concatMapAttrs - (nodeName: nodeAttrs: - nixpkgs.lib.mapAttrs' - # TODO This is duplicated three times. This is microvm naming #3 - # TODO maybe use microvm.vms..compoundName - (n: nixpkgs.lib.nameValuePair "${nodeName}-${n}") - (self.colmenaNodes.${nodeName}.config.microvm.vms or {})) - self.colmenaNodes; + # Collect all defined microvm nodes from each colmena node + microvmNodes = nixpkgs.lib.concatMapAttrs (_: node: + nixpkgs.lib.mapAttrs' + (vm: def: nixpkgs.lib.nameValuePair def.nodeName node.config.microvm.vms.${vm}) + (node.config.extra.microvms.vms or {})) + self.colmenaNodes; + # Expose all nodes in a single attribute nodes = self.colmenaNodes // self.microvmNodes; # Collect installer packages diff --git a/modules/microvms.nix b/modules/microvms.nix index 2233a5b..119ebe4 100644 --- a/modules/microvms.nix +++ b/modules/microvms.nix @@ -59,14 +59,24 @@ ''; microvm.vms.${vmName} = let + # Loads configuration from a subfolder of this nodes configuration, if it exists. + configPath = + if nodePath == null + then null + else nodePath + "/microvms/${vmName}"; + node = (import ../nix/generate-node.nix inputs) - # TODO This is duplicated three times. This is microvm naming #1 - "${nodeName}-${vmName}" + vmCfg.nodeName { inherit (vmCfg) system; - # TODO make this configurable (or even disableable) - config = nodePath + "/microvms/${vmName}"; + # Load configPath, if it exists. + ${ + if configPath != null && builtins.pathExists configPath + then "config" + else null + } = + configPath; }; mac = net.mac.addPrivate vmCfg.id cfg.networking.baseMac; in { @@ -292,6 +302,16 @@ in { ... }: { options = { + nodeName = mkOption { + type = types.str; + default = "${nodeName}-${name}"; + description = mdDoc '' + The name of the resulting node. By default this will be a compound name + of the host's name and the vm's name to avoid name clashes. Can be + overwritten to designate special names to specific vms. + ''; + }; + id = mkOption { type = types.addCheck types.int (x: x > 1) diff --git a/modules/repo.nix b/modules/repo.nix index e32d617..b844eb0 100644 --- a/modules/repo.nix +++ b/modules/repo.nix @@ -11,6 +11,7 @@ attrNames literalExpression mapAttrs + mdDoc mkIf mkOption types @@ -43,7 +44,7 @@ in { defineNixExtraBuiltins = mkOption { default = false; type = types.bool; - description = '' + description = mdDoc '' Add nix-plugins and the correct extra-builtin-files definition to this host's nix configuration, so that it can be used to decrypt the secrets in this repository. ''; @@ -53,7 +54,7 @@ in { default = {}; type = types.attrsOf types.path; example = literalExpression "{ local = ./secrets.nix.age; }"; - description = '' + description = mdDoc '' This file manages the origin for this machine's repository-secrets. Anything that is technically not a secret in the classical sense (i.e. that it has to be protected after it has been deployed), but something you want to keep secret from the public;