config/profiles/remote-builders.nix

82 lines
2.6 KiB
Nix

{ 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); # TODO: 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;
thisHostIsHopHost = builtins.elem config.networking.fqdn (lib.forEach hosts (host: host.ssh.proxyJump or null));
thisHostIsConsumer = thisHost.ssh ? userPublicKey;
mkRemoteConfig = fqdn: let
host = hosts.${fqdn};
jump = hosts.${host.ssh.proxyJump};
buildMachine = (lib.filterAttrs (key: _: !elem key ["ssh"]) host) // {
hostName = fqdn;
sshUser = host.ssh.listenUser;
};
isBuilder = host.maxJobs > 0;
isConsumer = host.ssh ? userPublicKey && thisHostIsBuilder;
isThis = fqdn == config.networking.fqdn;
in mkIf (!isThis) ( lib.mkMerge [
# out
(lib.mkIf (isBuilder && thisHostIsConsumer) {
nix.buildMachines = [ buildMachine ];
})
# out or jump
(lib.mkIf (host.ssh ? listenPublicKey && thisHostIsConsumer) {
programs.ssh.knownHosts.${fqdn}.publicKey = host.ssh.listenPublicKey;
# timeouts are great when remote is unresponsive. nix doesn't care
programs.ssh.extraConfig = ''
Host ${fqdn}
ConnectTimeout 3
Port ${builtins.toString host.ssh.listenPort}
${lib.optionalString (host.ssh ? proxyJump) ''
ProxyJump ${host.ssh.proxyJump}
''}
'';
})
# in
(mkIf (isConsumer && (thisHostIsBuilder || thisHostIsHopHost) ) {
nix.settings.allowed-users = [ thisHost.ssh.listenUser ];
nix.settings.trusted-users = [ thisHost.ssh.listenUser ];
users.users.${thisHost.ssh.listenUser} = {
isSystemUser = lib.mkDefault (!config.users.users.${thisHost.ssh.listenUser}.isNormalUser);
openssh.authorizedKeys.keys = [ host.ssh.userPublicKey ];
group = lib.mkOptionDefault "nogroup";
};
})
]);
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;
}