nixpkgs/nixos/modules/services/networking/tmate-ssh-server.nix
Silvan Mosberger 374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00

117 lines
3.3 KiB
Nix

{
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 = {
enable = mkEnableOption "tmate ssh server";
package = mkPackageOption pkgs "tmate-ssh-server" { };
host = mkOption {
type = types.str;
description = "External host name";
defaultText = lib.literalExpression "config.networking.domain or config.networking.hostName";
default = if domain == null then config.networking.hostName else domain;
};
port = mkOption {
type = types.port;
description = "Listen port for the ssh server";
default = 2222;
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = "Whether to automatically open the specified ports in the firewall.";
};
advertisedPort = mkOption {
type = types.port;
description = "External port advertised to clients";
};
keysDir = mkOption {
type = with types; nullOr str;
description = "Directory containing ssh keys, defaulting to auto-generation";
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";
runtimeInputs = with pkgs; [
openssh
coreutils
];
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)"
sed "s|@ed25519_fingerprint@|$ED25519_SIG|g" ${tmate-config} | \
sed "s|@rsa_fingerprint@|$RSA_SIG|g"
'';
})
];
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 ];
};
}