refactor(nixos): isolate SSH fleet configuration

This commit is contained in:
2026-10-07 20:05:55 -04:00
parent 879d17dce8
commit dbd3fc8bae

View File

@@ -5,70 +5,83 @@
... ...
}: }:
# Configures inbound and outbound SSH access. # Configures inbound and outbound SSH access.
# Keep bindings grouped by purpose rather than dependency traversal.
# NOLINT(let_order)
let let
cfg = config.modules.network.ssh; cfg = config.modules.network.ssh; # NOLINT(let_order)
user = config.user.name;
workstationNames = [
"neogaia"
"zeus"
];
serverNames = [ "pikachu" ];
managedHostNames = workstationNames ++ serverNames;
isWorkstation = lib.elem config.networking.hostName workstationNames;
isServer = lib.elem config.networking.hostName serverNames;
clientKeyFor = host: builtins.readFile (../../hosts + "/${host}/ssh_user_ed25519_key.pub");
hostKeysFor =
host:
map builtins.readFile [
(../../hosts + "/${host}/ssh_host_ed25519_key.pub")
(../../hosts + "/${host}/ssh_host_rsa_key.pub")
];
managedTargets = lib.genAttrs managedHostNames (name: {
hostName = registry.address.${name}.self;
clientKey = clientKeyFor name;
hostKeys = hostKeysFor name;
});
extraTargets = {
# TODO: Pin Gitea's host key after migrating it from Proxmox.
gitea = {
hostName = "git.alexion.dev";
port = 2022;
user = "gitea";
aliases = [
"gitea"
"git.alexion.dev"
];
};
};
mkTarget = name: target: {
hostName = target.hostName or name;
user = target.user or user;
port = target.port or 22;
aliases = target.aliases or [ name ];
clientKey = target.clientKey or null;
hostKeys = target.hostKeys or [ ];
};
targets = lib.mapAttrs mkTarget (managedTargets // extraTargets);
clientKeysFor = names: lib.filter (key: key != null) (map (name: targets.${name}.clientKey) names);
defaultAuthorizedKeys = lib.flatten ( defaultAuthorizedKeys = lib.flatten (
clientKeysFor workstationNames ++ lib.optional isServer (clientKeysFor serverNames) clientKeysFor fleet.workstationNames
++ lib.optional fleet.isServer (clientKeysFor fleet.serverNames)
); );
outboundTargetNames = clientKeysFor =
names: lib.filter (key: key != null) (map (name: fleet.targets.${name}.clientKey) names);
fleet =
# Keep fleet policy and target construction in conceptual order.
# NOLINT(let_order)
let let
groupTargets = workstationNames = [
if isServer then "neogaia"
[ "gitea" ] ++ serverNames "zeus"
else if isWorkstation then ];
[ "gitea" ] ++ serverNames ++ workstationNames serverNames = [ "pikachu" ];
else managedHostNames = workstationNames ++ serverNames;
[ "gitea" ]; isWorkstation = lib.elem config.networking.hostName workstationNames;
isServer = lib.elem config.networking.hostName serverNames;
clientKeyFor = host: builtins.readFile (../../hosts + "/${host}/ssh_user_ed25519_key.pub");
hostKeysFor =
host:
map builtins.readFile [
(../../hosts + "/${host}/ssh_host_ed25519_key.pub")
(../../hosts + "/${host}/ssh_host_rsa_key.pub")
];
managedTargets = lib.genAttrs managedHostNames (name: {
hostName = registry.address.${name}.self;
clientKey = clientKeyFor name;
hostKeys = hostKeysFor name;
});
extraTargets = {
# TODO: Pin Gitea's host key after migrating it from Proxmox.
gitea = {
hostName = "git.alexion.dev";
port = 2022;
user = "gitea";
aliases = [
"gitea"
"git.alexion.dev"
];
};
};
mkTarget = name: target: {
hostName = target.hostName or name;
user = target.user or user;
port = target.port or 22;
aliases = target.aliases or [ name ];
clientKey = target.clientKey or null;
hostKeys = target.hostKeys or [ ];
};
targets = lib.mapAttrs mkTarget (managedTargets // extraTargets);
outboundTargetNames =
let
groupTargets =
if isServer then
[ "gitea" ] ++ serverNames
else if isWorkstation then
[ "gitea" ] ++ serverNames ++ workstationNames
else
[ "gitea" ];
in
lib.filter (name: name != config.networking.hostName) groupTargets;
in in
lib.filter (name: name != config.networking.hostName) groupTargets; {
inherit
isServer
outboundTargetNames
serverNames
targets
workstationNames
;
};
user = config.user.name;
knownHosts = lib.listToAttrs ( knownHosts = lib.listToAttrs (
lib.concatLists ( lib.concatLists (
@@ -81,7 +94,7 @@ let
publicKey = keyWithoutComment key; publicKey = keyWithoutComment key;
} }
) target.hostKeys ) target.hostKeys
) targets ) fleet.targets
) )
); );
knownHostNamesFor = knownHostNamesFor =
@@ -96,7 +109,7 @@ let
sshSettingsFor = sshSettingsFor =
name: name:
let let
target = targets.${name}; target = fleet.targets.${name};
in in
{ {
header = "Host ${lib.concatStringsSep " " target.aliases}"; header = "Host ${lib.concatStringsSep " " target.aliases}";
@@ -161,7 +174,7 @@ in
enable = true; enable = true;
enableDefaultConfig = false; enableDefaultConfig = false;
settings = lib.mapAttrs (name: _: sshSettingsFor name) ( settings = lib.mapAttrs (name: _: sshSettingsFor name) (
lib.genAttrs outboundTargetNames (name: name) lib.genAttrs fleet.outboundTargetNames (name: name)
); );
}; };
} }