2022-10-05 18:34:30 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.tmate-ssh-server;
|
|
|
|
|
|
|
|
defaultKeysDir = "/etc/tmate-ssh-server-keys";
|
|
|
|
edKey = "${defaultKeysDir}/ssh_host_ed25519_key";
|
|
|
|
rsaKey = "${defaultKeysDir}/ssh_host_rsa_key";
|
|
|
|
|
|
|
|
keysDir =
|
|
|
|
if cfg.keysDir == null
|
|
|
|
then defaultKeysDir
|
|
|
|
else cfg.keysDir;
|
|
|
|
|
|
|
|
domain = config.networking.domain;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.tmate-ssh-server = {
|
2024-04-13 14:54:15 +02:00
|
|
|
enable = mkEnableOption "tmate ssh server";
|
2022-10-05 18:34:30 +02:00
|
|
|
|
2023-11-27 01:19:27 +01:00
|
|
|
package = mkPackageOption pkgs "tmate-ssh-server" { };
|
2022-10-05 18:34:30 +02:00
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "External host name";
|
2023-01-21 21:46:53 +01:00
|
|
|
defaultText = lib.literalExpression "config.networking.domain or config.networking.hostName";
|
2022-10-05 18:34:30 +02:00
|
|
|
default =
|
|
|
|
if domain == null then
|
|
|
|
config.networking.hostName
|
|
|
|
else
|
|
|
|
domain;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Listen port for the ssh server";
|
2022-10-05 18:34:30 +02:00
|
|
|
default = 2222;
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
2022-12-04 13:50:48 +01:00
|
|
|
default = false;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Whether to automatically open the specified ports in the firewall.";
|
2022-10-05 18:34:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
advertisedPort = mkOption {
|
|
|
|
type = types.port;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "External port advertised to clients";
|
2022-10-05 18:34:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
keysDir = mkOption {
|
|
|
|
type = with types; nullOr str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Directory containing ssh keys, defaulting to auto-generation";
|
2022-10-05 18:34:30 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = optionals cfg.openFirewall [ cfg.port ];
|
|
|
|
|
|
|
|
services.tmate-ssh-server = {
|
|
|
|
advertisedPort = mkDefault cfg.port;
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages =
|
|
|
|
let
|
|
|
|
tmate-config = pkgs.writeText "tmate.conf"
|
|
|
|
''
|
|
|
|
set -g tmate-server-host "${cfg.host}"
|
|
|
|
set -g tmate-server-port ${toString cfg.port}
|
|
|
|
set -g tmate-server-ed25519-fingerprint "@ed25519_fingerprint@"
|
|
|
|
set -g tmate-server-rsa-fingerprint "@rsa_fingerprint@"
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
[
|
|
|
|
(pkgs.writeShellApplication {
|
|
|
|
name = "tmate-client-config";
|
2023-11-25 19:14:10 +01:00
|
|
|
runtimeInputs = with pkgs;[ openssh coreutils ];
|
2022-10-05 18:34:30 +02:00
|
|
|
text = ''
|
|
|
|
RSA_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_rsa_key.pub" | cut -d ' ' -f 2)"
|
|
|
|
ED25519_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_ed25519_key.pub" | cut -d ' ' -f 2)"
|
2023-11-25 19:14:10 +01:00
|
|
|
sed "s|@ed25519_fingerprint@|$ED25519_SIG|g" ${tmate-config} | \
|
|
|
|
sed "s|@rsa_fingerprint@|$RSA_SIG|g"
|
2022-10-05 18:34:30 +02:00
|
|
|
'';
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.tmate-ssh-server = {
|
|
|
|
description = "tmate SSH Server";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/tmate-ssh-server -h ${cfg.host} -p ${toString cfg.port} -q ${toString cfg.advertisedPort} -k ${keysDir}";
|
|
|
|
};
|
|
|
|
preStart = mkIf (cfg.keysDir == null) ''
|
|
|
|
if [[ ! -d ${defaultKeysDir} ]]
|
|
|
|
then
|
|
|
|
mkdir -p ${defaultKeysDir}
|
|
|
|
fi
|
|
|
|
if [[ ! -f ${edKey} ]]
|
|
|
|
then
|
|
|
|
${pkgs.openssh}/bin/ssh-keygen -t ed25519 -f ${edKey} -N ""
|
|
|
|
fi
|
|
|
|
if [[ ! -f ${rsaKey} ]]
|
|
|
|
then
|
|
|
|
${pkgs.openssh}/bin/ssh-keygen -t rsa -f ${rsaKey} -N ""
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
maintainers = with maintainers; [ jlesquembre ];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|