Clean up provisioning code by moving some logic into
All checks were successful
/ terraform-providers (push) Successful in 1m23s
/ systems (push) Successful in 4m8s
/ dev-shell (push) Successful in 47s
/ rust-packages (push) Successful in 52s
/ check (push) Successful in 2m34s

readOnly options
This commit is contained in:
Kaare Hoff Skovgaard 2025-08-04 22:23:47 +02:00
parent eec5e02770
commit 7adc4a20bd
Signed by: khs
GPG key ID: C7D890804F01E9F0
5 changed files with 67 additions and 78 deletions

View file

@ -10,4 +10,15 @@
shorthandOnlyDefinesConfig = true;
modules = lib.toList { inherit options; };
};
mkSubmodule' =
fn:
lib.types.submodule (
{ config, ... }:
let
data = fn { inherit config; };
in
{
inherit (data) options;
}
);
}

View file

@ -1,19 +1,20 @@
{
config,
lib,
inputs,
pkgs,
...
}:
let
cfg = config.khscodes.infrastructure.provisioning;
provisioning = {
modules = lib.mkOption {
type = lib.types.listOf lib.types.anything;
description = "Modules used to bring up the needed resources";
default = [ ];
};
};
terranixConfig =
cfg:
if lib.lists.length cfg.modules > 0 then
inputs.terranix.lib.terranixConfiguration {
system = pkgs.hostPlatform.system;
modules = cfg.modules;
extraArgs = { inherit lib inputs; };
}
else
null;
usesEndpoint =
search: endpoint: config:
if lib.strings.hasInfix search (builtins.readFile config) then [ endpoint ] else [ ];
@ -49,31 +50,49 @@ let
[ ]
else
lib.lists.flatten (lib.lists.map (c: usesEndpoint c.search c.endpoint config) endpointsMaps);
preConfig =
if lib.lists.length cfg.pre.modules > 0 then
inputs.terranix.lib.terranixConfiguration {
system = pkgs.hostPlatform.system;
modules = cfg.pre.modules;
extraArgs = { inherit lib inputs; };
provisioning = lib.khscodes.mkSubmodule' (
{ config }:
{
description = "Module for handling provisioning";
options = {
modules = lib.mkOption {
type = lib.types.listOf lib.types.anything;
description = "Modules used to bring up the needed resources";
default = [ ];
};
config = lib.mkOption {
type = lib.types.nullOr lib.types.path;
readOnly = true;
default = terranixConfig config;
};
endpoints = lib.mkOption {
type = lib.types.listOf (
lib.types.enum [
"openstack"
"aws"
"unifi"
"hcloud"
"cloudflare"
"vault"
]
);
readOnly = true;
default = endpointsUsed config.config;
};
};
}
else
null;
preEndpoints = endpointsUsed preConfig;
postConfig =
if lib.lists.length cfg.post.modules > 0 then
inputs.terranix.lib.terranixConfiguration {
system = pkgs.hostPlatform.system;
modules = cfg.post.modules;
extraArgs = { inherit lib inputs; };
}
else
null;
postEndpoints = endpointsUsed postConfig;
);
in
{
options.khscodes.infrastructure.provisioning = {
pre = provisioning;
post = provisioning;
pre = lib.mkOption {
type = provisioning;
default = { };
};
post = lib.mkOption {
type = provisioning;
default = { };
};
secretsSource = lib.mkOption {
type = lib.types.enum [
"vault"
@ -87,53 +106,12 @@ in
description = "User data that should be added to the instance during provisioning";
default = "";
};
preConfig = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = "The generated config for the pre provisioning, if any was specified";
};
preEndpoints = lib.mkOption {
type = lib.types.listOf (
lib.types.enum [
"openstack"
"aws"
"unifi"
"hcloud"
"cloudflare"
"vault"
]
);
description = "Needed endpoints to be used during provisioning";
default = [ ];
};
preImageUsername = lib.mkOption {
type = lib.types.str;
description = "The username for the image being deployed before being swapped for NixOS";
default = "root";
};
postConfig = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = "The generated config for the post provisioning, if any was specified";
};
postEndpoints = lib.mkOption {
type = lib.types.listOf (
lib.types.enum [
"openstack"
"aws"
"unifi"
"hcloud"
"cloudflare"
"vault"
]
);
description = "Needed endpoints to be used during provisioning";
default = [ ];
};
};
config = {
khscodes.infrastructure.provisioning.preConfig = preConfig;
khscodes.infrastructure.provisioning.preEndpoints = preEndpoints;
khscodes.infrastructure.provisioning.postConfig = postConfig;
khscodes.infrastructure.provisioning.postEndpoints = postEndpoints;
};
config = { };
}

View file

@ -19,7 +19,7 @@ pkgs.writeShellApplication {
nix build --no-link '${inputs.self}#nixosConfigurations."'"$hostname"'".config.system.build.toplevel'
fi
baseAttr='${inputs.self}#nixosConfigurations."'"$hostname"'".config.khscodes.infrastructure'
config="$(nix build --no-link --print-out-paths "''${baseAttr}.provisioning.preConfig")"
config="$(nix build --no-link --print-out-paths "''${baseAttr}.provisioning.pre.config")"
preScript="$(nix eval --raw "''${baseAttr}.nixos-install.preScript")"
username="$(nix eval --raw "''${baseAttr}.provisioning.preImageUsername")"
if [[ "$config" == "null" ]]; then

View file

@ -15,9 +15,9 @@ pkgs.writeShellApplication {
hostname="$1"
cmd="''${2:-apply}"
baseAttr='${inputs.self}#nixosConfigurations."'"$hostname"'".config.khscodes.infrastructure.provisioning'
config="$(nix build --no-link --print-out-paths "''${baseAttr}.postConfig")"
config="$(nix build --no-link --print-out-paths "''${baseAttr}.post.config")"
secretsSource="$(nix eval --raw "''${baseAttr}.secretsSource")"
endpoints="$(nix eval --show-trace --json "''${baseAttr}.postEndpoints")"
endpoints="$(nix eval --show-trace --json "''${baseAttr}.post.endpoints")"
if [[ "$config" == "null" ]]; then
echo "No postprovisioning needed"
exit 0

View file

@ -16,9 +16,9 @@ pkgs.writeShellApplication {
hostname="$1"
cmd="''${2:-apply}"
baseAttr='${inputs.self}#nixosConfigurations."'"$hostname"'".config.khscodes.infrastructure.provisioning'
config="$(nix build --no-link --print-out-paths "''${baseAttr}.preConfig")"
config="$(nix build --no-link --print-out-paths "''${baseAttr}.pre.config")"
secretsSource="$(nix eval --raw "''${baseAttr}.secretsSource")"
endpoints="$(nix eval --show-trace --json "''${baseAttr}.preEndpoints")"
endpoints="$(nix eval --show-trace --json "''${baseAttr}.pre.endpoints")"
if [[ "$config" == "null" ]]; then
echo "No preprovisioning needed"
exit 0