infrastructure/nixos/util.nix

59 lines
1.8 KiB
Nix
Raw Normal View History

2022-09-21 18:00:51 +02:00
{ nixpkgs, home-manager, hyprland, mailserver, ... }:
2022-08-21 11:42:17 +02:00
let
2022-10-15 13:44:27 +02:00
inherit (builtins) filter attrValues concatLists;
2022-09-25 23:49:26 +02:00
# Helper function to resolve what should be imported depending on the type of config (lxc, vm, bare metal)
resolve_imports =
let
# lookup table
import_cases = {
"lxc" = [
"${nixpkgs}/nixos/modules/virtualisation/lxc-container.nix"
2022-10-08 17:05:22 +02:00
./common/generic-lxc.nix
2022-09-25 23:49:26 +02:00
];
"vm" = [
2022-10-08 17:05:22 +02:00
./common/generic-vm.nix
2022-09-25 23:49:26 +02:00
];
"local" = [
hyprland.nixosModules.default
];
};
in
{ hostname, realm, profile ? hostname, type ? "lxc", ... }: [
2022-10-18 17:20:14 +02:00
home-manager.nixosModules.home-manager
2022-09-25 23:49:26 +02:00
mailserver.nixosModules.mailserver
2022-10-08 17:05:22 +02:00
./common
"${./.}/hosts/${realm}/${profile}/configuration.nix"
2022-09-25 23:49:26 +02:00
] ++ import_cases.${type};
2022-08-21 11:42:17 +02:00
in
2022-09-25 23:49:26 +02:00
{
# Add to whatever realm a host belong to its list of tags
2022-12-01 22:13:05 +01:00
add_realm_to_tags = realm: map ({ tags ? [ ], ... }@host: host // { tags = [ realm ] ++ tags; inherit realm; });
2022-09-25 23:49:26 +02:00
# Flatten all hosts to a single list
2022-08-21 11:42:17 +02:00
flatten_hosts = hosts: concatLists (attrValues hosts);
2022-09-25 23:49:26 +02:00
# Filter out all hosts which aren't nixos
2022-12-01 22:13:05 +01:00
filter_nix_hosts = filter ({ nix ? true, ... }: nix);
2022-08-21 11:42:17 +02:00
2022-09-25 23:49:26 +02:00
# Helper function to build a colmena host definition
2022-09-10 19:44:16 +02:00
mkColmenaHost = { ip ? null, hostname, tags, realm, type ? "lxc", ... }@host:
let
2022-09-25 23:49:26 +02:00
# this makes local apply work a bit nicer
2022-09-10 19:44:16 +02:00
name = if realm == "thalassa" then hostname else "${hostname}.${realm}";
in
{
"${name}" = {
imports = resolve_imports host;
networking = {
hostName = hostname;
domain = realm;
};
deployment = {
inherit tags;
targetHost = ip;
2022-12-01 22:13:05 +01:00
allowLocalDeployment = type == "local";
2022-09-10 19:44:16 +02:00
targetUser = null; # Defaults to $USER
};
2022-08-21 11:42:17 +02:00
};
};
}