config/profiles/known-hosts.nix

134 lines
4.7 KiB
Nix
Raw Permalink Normal View History

2024-10-12 16:07:20 +02:00
{ config, lib, pkgs, ... }:
2024-04-16 06:10:04 +02:00
2024-10-19 04:05:19 +02:00
# TODO: should max-builds be enforced on thisHost as well?
2024-04-16 06:10:04 +02:00
let
2024-10-12 03:11:48 +02:00
inherit (builtins)
map
fromTOML
toString
readFile
elem
attrNames
attrValues
;
2024-04-16 06:10:04 +02:00
2024-10-12 16:07:20 +02:00
# TODO: test ssh-ng
# https://discourse.nixos.org/t/wrapper-to-restrict-builder-access-through-ssh-worth-upstreaming/25834
nix-ssh-wrapper = pkgs.writeShellScript "nix-ssh-wrapper" ''
case $SSH_ORIGINAL_COMMAND in
"nix-daemon --stdio")
exec ${config.nix.package}/bin/nix-daemon --stdio
;;
"nix-store --serve --write")
exec ${config.nix.package}/bin/nix-store --serve --write
;;
*)
echo "Access only allowed for using the nix remote builder" 1>&2
exit
esac
'';
known-hosts = let
known-hosts' = lib.importTOML ../hosts/known-hosts.toml; # TODO: eww
in
lib.pipe known-hosts' [
(lib.flip lib.removeAttrs ["__default__"])
(lib.mapAttrs (fqdn: host:
2024-10-12 03:11:48 +02:00
lib.recursiveUpdate (known-hosts'."__default__" or {}) host
))
2024-10-12 03:11:48 +02:00
(lib.mapAttrsToList (fqdn: host:
let
allHostnames = [ fqdn ] ++ host.aliases;
2024-10-12 03:11:48 +02:00
in
lib.forEach allHostnames (alias:
lib.nameValuePair
alias
(host // {
aliases = lib.remove alias allHostnames;
isAlias = fqdn != alias;
})
)
))
lib.flatten
lib.listToAttrs
];
2024-10-04 08:30:58 +02:00
hostNames = attrNames known-hosts;
thisHost = known-hosts.${config.networking.fqdn};
2024-10-07 11:22:08 +02:00
thisHostIsBuilder = thisHost.buildMachine.maxJobs > 0;
2024-10-12 03:11:48 +02:00
thisHostIsBuildee = thisHost.ssh ? userPublicKey;
thisHostIsHopHost = elem config.networking.fqdn (lib.forEach (attrValues known-hosts) (host: host.ssh.proxyJump or null));
2024-04-16 06:10:04 +02:00
mkRemoteConfig = fqdn: let
2024-10-04 08:30:58 +02:00
thatHost = known-hosts.${fqdn};
thatJump = known-hosts.${thatHost.ssh.proxyJump};
buildMachine = thatHost.buildMachine // {
2024-04-16 06:10:04 +02:00
hostName = fqdn;
2024-10-04 08:30:58 +02:00
sshUser = thatHost.ssh.listenUser;
2024-04-16 06:10:04 +02:00
};
2024-10-04 08:30:58 +02:00
thatHostIsBuilder = thatHost.buildMachine.maxJobs > 0;
2024-10-12 03:11:48 +02:00
thatHostIsBuildee = thatHost.ssh ? userPublicKey && thisHostIsBuilder;
thatHostIsThis = elem config.networking.fqdn ([ fqdn ] ++ thatHost.aliases);
in lib.mkIf (!thatHostIsThis) ( lib.mkMerge [
2024-04-16 06:10:04 +02:00
# out
2024-10-12 03:11:48 +02:00
(lib.mkIf (thisHostIsBuildee && thatHostIsBuilder) {
2024-07-10 00:34:38 +02:00
# TODO: Allow setting speedFactor for local builds, as local is currently fixed to 0
# https://github.com/NixOS/nix/issues/2457
nix.distributedBuilds = true;
# useful when the builder has a faster internet connection than i do
nix.settings.builders-use-substitutes = true;
nix.buildMachines = lib.mkIf (!thatHost.isAlias) [ buildMachine ];
2024-04-16 06:10:04 +02:00
2024-07-10 00:34:38 +02:00
})
# out or jump
2024-10-12 03:11:48 +02:00
(lib.mkIf (thisHostIsBuildee && thatHost.ssh ? listenPublicKey) {
2024-10-04 08:30:58 +02:00
programs.ssh.knownHosts.${fqdn}.publicKey = thatHost.ssh.listenPublicKey;
2024-07-31 01:57:47 +02:00
# TODO: use nix.buildMachines.*.publicHostKey ?
2024-04-16 06:10:04 +02:00
2024-08-18 03:16:17 +02:00
# timeouts are great when remote is unresponsive. nix doesn't care, lix is way and tests each remote only once
2024-07-10 00:34:38 +02:00
programs.ssh.extraConfig = ''
Host ${fqdn}
2024-10-12 03:11:48 +02:00
ConnectTimeout ${toString thatHost.ssh.connectTimeout}
Port ${toString thatHost.ssh.listenPort}
2024-10-04 08:30:58 +02:00
${lib.optionalString (thatHost.ssh ? proxyJump) ''
2024-10-12 03:11:48 +02:00
ProxyJump ${thatJump.ssh.listenUser}@${thatHost.ssh.proxyJump}:${toString thatJump.ssh.listenPort}
2024-07-10 00:34:38 +02:00
''}
2024-10-04 08:30:58 +02:00
${lib.optionalString (thatHost.ssh ? userPrivateKey) ''
IdentityFile ${thatHost.ssh.userPrivateKey}
2024-08-18 03:16:17 +02:00
''}
2024-07-10 00:34:38 +02:00
'';
2024-10-04 08:30:58 +02:00
sops.secrets = lib.mkIf (lib.hasPrefix "/run/secrets/" (thatHost.ssh.userPrivateKey or "")) {
2024-10-10 19:20:20 +02:00
"${lib.removePrefix "/run/secrets/" thatHost.ssh.userPrivateKey}" = { };
2024-10-04 08:30:58 +02:00
};
2024-10-08 15:17:08 +02:00
2024-07-10 00:34:38 +02:00
})
2024-04-16 06:10:04 +02:00
# in
2024-10-12 16:07:20 +02:00
(lib.mkIf ((thisHostIsBuilder || thisHostIsHopHost) && thatHostIsBuildee && !thatHost.isAlias) {
# TODO: ensure the user is "nixbld-remote"?
users.groups.${thisHost.ssh.listenUser} = { };
2024-07-10 00:34:38 +02:00
users.users.${thisHost.ssh.listenUser} = {
2024-04-27 23:31:32 +02:00
isSystemUser = lib.mkDefault (!config.users.users.${thisHost.ssh.listenUser}.isNormalUser);
2024-10-12 16:07:20 +02:00
useDefaultShell = lib.mkDefault true;
openssh.authorizedKeys.keys = [
# https://man.archlinux.org/man/core/openssh/sshd.8.en#AUTHORIZED_KEYS_FILE_FORMAT
# TODO: lib.getExe
''restrict,pty,command="${nix-ssh-wrapper}" ${thatHost.ssh.userPublicKey}''
];
group = lib.mkOverride 1499 "${thisHost.ssh.listenUser}"; # mkOptionDefault - 1,
2024-04-16 06:10:04 +02:00
};
2024-07-31 21:35:42 +02:00
})
2024-10-12 16:07:20 +02:00
(lib.mkIf (thisHostIsBuilder && thatHostIsBuildee && !thatHost.isAlias) {
2024-07-31 21:35:42 +02:00
nix.settings.allowed-users = [ thisHost.ssh.listenUser ];
nix.settings.trusted-users = [ thisHost.ssh.listenUser ];
2024-07-10 00:34:38 +02:00
})
]);
2024-04-16 06:10:04 +02:00
in {
imports = lib.forEach hostNames mkRemoteConfig;
}