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 (mdDoc "tmate ssh server");
23 description = mdDoc "The package containing tmate-ssh-server";
24 defaultText = literalExpression "pkgs.tmate-ssh-server";
25 default = pkgs.tmate-ssh-server;
30 description = mdDoc "External host name";
31 defaultText = lib.literalExpression "config.networking.domain or config.networking.hostName ";
33 if domain == null then
34 config.networking.hostName
41 description = mdDoc "Listen port for the ssh server";
45 openFirewall = mkOption {
48 description = mdDoc "Whether to automatically open the specified ports in the firewall.";
51 advertisedPort = mkOption {
53 description = mdDoc "External port advertised to clients";
57 type = with types; nullOr str;
58 description = mdDoc "Directory containing ssh keys, defaulting to auto-generation";
63 config = mkIf cfg.enable {
65 networking.firewall.allowedTCPPorts = optionals cfg.openFirewall [ cfg.port ];
67 services.tmate-ssh-server = {
68 advertisedPort = mkDefault cfg.port;
71 environment.systemPackages =
73 tmate-config = pkgs.writeText "tmate.conf"
75 set -g tmate-server-host "${cfg.host}"
76 set -g tmate-server-port ${toString cfg.port}
77 set -g tmate-server-ed25519-fingerprint "@ed25519_fingerprint@"
78 set -g tmate-server-rsa-fingerprint "@rsa_fingerprint@"
82 (pkgs.writeShellApplication {
83 name = "tmate-client-config";
84 runtimeInputs = with pkgs;[ openssh coreutils sd ];
86 RSA_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_rsa_key.pub" | cut -d ' ' -f 2)"
87 ED25519_SIG="$(ssh-keygen -l -E SHA256 -f "${keysDir}/ssh_host_ed25519_key.pub" | cut -d ' ' -f 2)"
88 sd -sp '@ed25519_fingerprint@' "$ED25519_SIG" ${tmate-config} | \
89 sd -sp '@rsa_fingerprint@' "$RSA_SIG"
94 systemd.services.tmate-ssh-server = {
95 description = "tmate SSH Server";
96 after = [ "network.target" ];
97 wantedBy = [ "multi-user.target" ];
99 ExecStart = "${cfg.package}/bin/tmate-ssh-server -h ${cfg.host} -p ${toString cfg.port} -q ${toString cfg.advertisedPort} -k ${keysDir}";
101 preStart = mkIf (cfg.keysDir == null) ''
102 if [[ ! -d ${defaultKeysDir} ]]
104 mkdir -p ${defaultKeysDir}
106 if [[ ! -f ${edKey} ]]
108 ${pkgs.openssh}/bin/ssh-keygen -t ed25519 -f ${edKey} -N ""
110 if [[ ! -f ${rsaKey} ]]
112 ${pkgs.openssh}/bin/ssh-keygen -t rsa -f ${rsaKey} -N ""
119 maintainers = with maintainers; [ jlesquembre ];