2023-01-10 02:38:06 +01:00
|
|
|
# This setups a SSH server.
|
|
|
|
{ outputs, config, lib, ... }:
|
|
|
|
let
|
|
|
|
# Record containing all the hosts
|
|
|
|
hosts = outputs.nixosConfigurations;
|
|
|
|
|
|
|
|
# Name of the current hostname
|
|
|
|
hostname = config.networking.hostName;
|
|
|
|
|
|
|
|
# Function from hostname to relative path to public ssh key
|
|
|
|
pubKey = host: ../../${host}/ssh_host_ed25519_key.pub;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
services.openssh = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
|
2023-06-15 20:08:20 +02:00
|
|
|
settings = {
|
|
|
|
# Forbid root login through SSH.
|
|
|
|
PermitRootLogin = "no";
|
|
|
|
|
|
|
|
# Use keys only. Remove if you want to SSH using password (not recommended)
|
|
|
|
PasswordAuthentication = false;
|
|
|
|
};
|
2023-01-10 02:38:06 +01:00
|
|
|
|
|
|
|
# Automatically remove stale sockets
|
|
|
|
extraConfig = ''
|
|
|
|
StreamLocalBindUnlink yes
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Generate ssh key
|
2023-04-27 01:08:20 +02:00
|
|
|
hostKeys =
|
|
|
|
let mkKey = type: path: extra: { inherit type path; } // extra;
|
|
|
|
in
|
|
|
|
[
|
2023-06-09 13:17:34 +02:00
|
|
|
(mkKey "ed25519" "/persist/state/etc/ssh/ssh_host_ed25519_key" { })
|
|
|
|
(mkKey "rsa" "/persist/state/etc/ssh/ssh_host_rsa_key" { bits = 4096; })
|
2023-04-27 01:08:20 +02:00
|
|
|
];
|
2023-01-10 02:38:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# Passwordless sudo when SSH'ing with keys
|
|
|
|
security.pam.enableSSHAgentAuth = true;
|
|
|
|
|
|
|
|
# Add each host in this repo to the knownHosts list
|
|
|
|
programs.ssh = {
|
2023-05-28 02:00:10 +02:00
|
|
|
knownHosts = lib.pipe hosts [
|
|
|
|
# attrsetof host -> attrsetof { ... }
|
|
|
|
(builtins.mapAttrs
|
|
|
|
# string -> host -> { ... }
|
|
|
|
(name: _: {
|
|
|
|
publicKeyFile = pubKey name;
|
|
|
|
extraHostNames = lib.optional (name == hostname) "localhost";
|
|
|
|
}))
|
|
|
|
|
|
|
|
# attrsetof { ... } -> attrsetof { ... }
|
2023-05-28 05:24:36 +02:00
|
|
|
(lib.attrsets.filterAttrs
|
2023-05-28 02:00:10 +02:00
|
|
|
# string -> { ... } -> bool
|
|
|
|
(_: { publicKeyFile, ... }: builtins.pathExists publicKeyFile))
|
|
|
|
];
|
2023-01-10 02:38:06 +01:00
|
|
|
};
|
|
|
|
}
|