1 { config, lib, pkgs, ... }:
4 cfg = config.services.tmate-ssh-server;
6 defaultKeysDir = "/etc/tmate-ssh-server-keys";
7 edKey = "${defaultKeysDir}/ssh_host_ed25519_key";
8 rsaKey = "${defaultKeysDir}/ssh_host_rsa_key";
11 if cfg.keysDir == null
15 domain = config.networking.domain;
18 options.services.tmate-ssh-server = {
19 enable = mkEnableOption "tmate ssh server";
21 package = mkPackageOption pkgs "tmate-ssh-server" { };
25 description = "External host name";
26 defaultText = lib.literalExpression "config.networking.domain or config.networking.hostName";
28 if domain == null then
29 config.networking.hostName
36 description = "Listen port for the ssh server";
40 openFirewall = mkOption {
43 description = "Whether to automatically open the specified ports in the firewall.";
46 advertisedPort = mkOption {
48 description = "External port advertised to clients";
52 type = with types; nullOr str;
53 description = "Directory containing ssh keys, defaulting to auto-generation";
58 config = mkIf cfg.enable {
60 networking.firewall.allowedTCPPorts = optionals cfg.openFirewall [ cfg.port ];
62 services.tmate-ssh-server = {
63 advertisedPort = mkDefault cfg.port;
66 environment.systemPackages =
68 tmate-config = pkgs.writeText "tmate.conf"
70 set -g tmate-server-host "${cfg.host}"
71 set -g tmate-server-port ${toString cfg.port}
72 set -g tmate-server-ed25519-fingerprint "@ed25519_fingerprint@"
73 set -g tmate-server-rsa-fingerprint "@rsa_fingerprint@"
77 (pkgs.writeShellApplication {
78 name = "tmate-client-config";
79 runtimeInputs = with pkgs;[ openssh coreutils ];
81 RSA_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_rsa_key.pub" | cut -d ' ' -f 2)"
82 ED25519_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_ed25519_key.pub" | cut -d ' ' -f 2)"
83 sed "s|@ed25519_fingerprint@|$ED25519_SIG|g" ${tmate-config} | \
84 sed "s|@rsa_fingerprint@|$RSA_SIG|g"
89 systemd.services.tmate-ssh-server = {
90 description = "tmate SSH Server";
91 after = [ "network.target" ];
92 wantedBy = [ "multi-user.target" ];
94 ExecStart = "${cfg.package}/bin/tmate-ssh-server -h ${cfg.host} -p ${toString cfg.port} -q ${toString cfg.advertisedPort} -k ${keysDir}";
96 preStart = mkIf (cfg.keysDir == null) ''
97 if [[ ! -d ${defaultKeysDir} ]]
99 mkdir -p ${defaultKeysDir}
101 if [[ ! -f ${edKey} ]]
103 ${pkgs.openssh}/bin/ssh-keygen -t ed25519 -f ${edKey} -N ""
105 if [[ ! -f ${rsaKey} ]]
107 ${pkgs.openssh}/bin/ssh-keygen -t rsa -f ${rsaKey} -N ""
114 maintainers = with maintainers; [ jlesquembre ];