{ config, lib, pkgs, ... }: let cfg.host = config.krebs.build.host; nets = lib.optional (cfg.host.nets?mycelium) cfg.host.nets.mycelium ++ lib.optional (cfg.host.nets?retiolum) cfg.host.nets.retiolum ++ lib.optional (cfg.host.nets?wiregrill) cfg.host.nets.wiregrill; cfg.ssh.port = 11423; keyType = "application/x-openssh-private-key; algorithm=ed25519"; keySlot = config.enstate.channels.secrets.slots."ssh.id_ed25519"; fallbackKey = "/etc/ssh/ssh_host_ed25519_key"; keyValidator = pkgs.writers.writeDash "validate-openssh-ed25519-private-key" '' set -efu # Not in a pipe, where set -e would miss a failure of jq. addresses=$(${pkgs.jq}/bin/jq -r -f ${pkgs.writeJq "slots-of-type.jq" '' .slots | to_entries[] | select(.value.type == $ARGS.named.type) | .key ''} --arg type ${lib.escapeShellArg keyType} "$1"/invocation.json) for address in $addresses; do ${pkgs.jq}/bin/jq -Rsc -f ${pkgs.writeJq "check-openssh-private-key.jq" '' "-----BEGIN OPENSSH PRIVATE KEY-----" as $begin | "-----END OPENSSH PRIVATE KEY-----" as $end | split("\n") | if .[-1] == "" then .[:-1] else . end | if length == 0 then "is empty" elif .[0] != $begin then "does not start with \($begin)" elif .[-1] != $end then "does not end with \($end)" elif length < 3 then "has no key between its first and last line" else empty end | { address: $ARGS.named.address, message: "the key \(.)" } ''} --arg address "$address" "$1"/channels/"$address" done ''; in lib.mkMerge [ { services.openssh = { enable = true; }; hrm.nftables.input.internet.tcp = [ "ssh" ]; hrm.nftables.tables.nat.chains = { # Port 22 is only reachable on the host's own network addresses; on # other addresses, port 22 goes nowhere and cfg.ssh.port leads to sshd. prerouting.rules = { ssh-own-addresses = { order = 900; daddr = map (net: net.ip4.addr) (builtins.filter (net: net.ip4 != null) nets) ++ map (net: net.ip6.addr) (builtins.filter (net: net.ip6 != null) nets); tcp.dport = [ "ssh" ]; verdict = "accept"; }; ssh-hide = { tcp.dport = [ "ssh" ]; verdict = "redirect to :0"; }; ssh-port = { tcp.dport = [ cfg.ssh.port ]; verdict = "redirect to :22"; }; }; output.rules.ssh-port = { oifname = "lo"; tcp.dport = [ cfg.ssh.port ]; verdict = "redirect to :22"; }; }; } # sshd reads its host key itself, so it gets ordered after enstate.service # here, and reloads, which reads the key again, when it changes. (lib.mkIf config.enstate.enable { enstate.types.${keyType}.validator = keyValidator; enstate.channels.secrets.slots."ssh.id_ed25519" = { type = keyType; secret = true; source = lib.mkDefault { secrets.key = "ssh.id_ed25519"; }; reactions.reload = lib.optional (!config.services.openssh.startWhenNeeded) "sshd.service"; }; # enstate's key first: sshd uses it whenever it is there. The generated # one is for while enstate has none; sshd skips a key it can't load. # Not by generateHostKeys, which would write a key to enstate's path. services.openssh.generateHostKeys = false; services.openssh.hostKeys = lib.mkForce [ { type = "ed25519"; path = keySlot.path; } { type = "ed25519"; path = fallbackKey; } ]; systemd.services.sshd-fallback-key = { description = "SSH fallback host key generation"; wantedBy = [ "multi-user.target" ]; unitConfig.ConditionFileNotEmpty = "!${fallbackKey}"; serviceConfig.Type = "oneshot"; script = '' rm -f ${fallbackKey} ${fallbackKey}.pub ${config.services.openssh.package}/bin/ssh-keygen -t ed25519 -N "" -f ${fallbackKey} ''; }; # Only the unit there is: sshd, or sshd@ with startWhenNeeded. systemd.services.${if config.services.openssh.startWhenNeeded then "sshd@" else "sshd"} = { after = [ "enstate.service" "sshd-fallback-key.service" ]; wants = [ "enstate.service" "sshd-fallback-key.service" ]; }; }) ]