config/profiles/remote-builders.nix

78 lines
2.3 KiB
Nix
Raw Normal View History

2024-04-16 06:10:04 +02:00
{}
/** /
{ config, lib, ... }:
# TODO: make a remote-build user on nixos boxes, instead of giving access to pbsds
# TODO: https://exozy.me/quickstart
# TODO: https://github.com/winterqt/darwin-build-box
let
inherit (builtins) map fromTOML readFile elem attrNames;
inherit (lib) mkIf;
hosts' = fromTOML (readFile ../../hosts/known-hosts.toml); # eww
hosts = lib.pipe hosts' [
(lib.filterAttrs (name: host: name != "default"))
(lib.mapAttrs (name: host:
lib.recursiveUpdate (hosts'."default" or {}) host
))
];
hostNames = attrNames hosts;
thisHost = hosts.${config.networking.fqdn};
thisHostIsBuilder = thisHost.maxJobs > 0;
mkRemoteConfig = fqdn: let
host = hosts.${fqdn};
jump = hosts.${host.ssh.proxyJump};
buildMachine = (lib.filterAttrs (key: _: !elem key ["ssh"]) host) // {
hostName = fqdn;
2024-04-27 23:31:32 +02:00
sshUser = host.ssh.listenUser;
2024-04-16 06:10:04 +02:00
};
isBuilder = host.maxJobs > 0;
isConsumer = host.ssh ? publicKeyUser && thisHostIsBuilder;
isThis = fqdn == config.networking.fqdn;
in mkIf (!isThis) {
# out
nix.buildMachines = mkIf isBuilder [ buildMachine ];
2024-04-27 23:31:32 +02:00
programs.ssh.knownHosts.${fqdn}.publicKey = mkIf isBuilder host.ssh.listenPublicKey;
2024-04-16 06:10:04 +02:00
# timeout is great when remote is unresponsive. nix doesn't care
programs.ssh.extraConfig = ''
Host ${fqdn}
ConnectTimeout 3
2024-04-27 23:31:32 +02:00
Port ${builtins.toString (host.ssh.listenPort or 22)}
2024-04-16 06:10:04 +02:00
${lib.optionalString (host.ssh ? proxyJump) ''
ProxyJump ${host.ssh.proxyJump}
''}
'';
# in
users = mkIf isConsumer {
2024-04-27 23:31:32 +02:00
users.${thisHost.ssh.listenUser} = {
isSystemUser = lib.mkDefault (!config.users.users.${thisHost.ssh.listenUser}.isNormalUser);
2024-04-16 06:10:04 +02:00
openssh.authorizedKeys.keys = [
2024-04-27 23:31:32 +02:00
host.ssh.userPublicKey
2024-04-16 06:10:04 +02:00
];
group = lib.mkDefault "nogroup";
};
};
2024-04-27 23:31:32 +02:00
nix.settings.allowed-users = mkIf isConsumer [ thisHost.ssh.listenUser ];
nix.settings.trusted-users = mkIf isConsumer [ thisHost.ssh.listenUser ];
2024-04-16 06:10:04 +02:00
};
in {
nix.distributedBuilds = true;
# TODO: Allow setting speedFactor for local builds, as local is currently fixed to 0
# https://github.com/NixOS/nix/issues/2457
# useful when the builder has a faster internet connection than i do
nix.settings.builders-use-substitutes = true;
imports = lib.forEach hostNames mkRemoteConfig;
}
/**/