2023-10-13 21:04:24 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-05-05 12:38:40 +02:00
|
|
|
}: let
|
2023-10-13 21:04:24 +02:00
|
|
|
cfg = config.myOptions.services.ssh;
|
2024-05-05 12:38:40 +02:00
|
|
|
|
2024-07-05 00:41:59 +02:00
|
|
|
inherit (lib) getExe' mkEnableOption mkIf mkMerge mkOption;
|
2024-05-05 12:38:40 +02:00
|
|
|
inherit (lib.types) attrsOf bool nullOr number str submodule;
|
|
|
|
inherit (lib.strings) concatStrings;
|
|
|
|
inherit (lib.attrsets) mapAttrsToList;
|
2024-07-05 00:41:59 +02:00
|
|
|
|
|
|
|
ksshaskpass = getExe' pkgs.libsForQt5.ksshaskpass "ksshaskpass";
|
|
|
|
ssh-agent = getExe' pkgs.openssh "ssh-agent";
|
2023-10-13 21:04:24 +02:00
|
|
|
in {
|
|
|
|
options.myOptions.services.ssh = {
|
|
|
|
daemon = mkOption {
|
|
|
|
description = "sshd options";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = submodule {
|
2023-10-13 21:04:24 +02:00
|
|
|
options = {
|
2024-04-05 22:59:32 +02:00
|
|
|
enable = mkEnableOption "sshd";
|
2023-10-13 21:04:24 +02:00
|
|
|
passwordAuth = mkOption {
|
|
|
|
description = "allow password auth";
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
allowRoot = mkOption {
|
|
|
|
description = "allow root login";
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
agent = mkOption {
|
|
|
|
description = "ssh agent options";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = submodule {
|
2023-10-13 21:04:24 +02:00
|
|
|
options = {
|
2024-04-05 22:59:32 +02:00
|
|
|
enable = mkEnableOption "ssh-agent";
|
2023-10-13 21:04:24 +02:00
|
|
|
hostAliases = mkOption {
|
|
|
|
description = "host aliases";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = attrsOf (submodule {
|
2023-10-13 21:04:24 +02:00
|
|
|
options = {
|
|
|
|
hostName = mkOption {
|
|
|
|
description = "hostname to ssh into";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = str;
|
2023-10-13 21:04:24 +02:00
|
|
|
};
|
2024-02-05 22:24:04 +01:00
|
|
|
port = mkOption {
|
|
|
|
description = "port to ssh into";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = nullOr number;
|
2024-02-05 22:24:04 +01:00
|
|
|
default = null;
|
|
|
|
};
|
2023-10-13 21:04:24 +02:00
|
|
|
user = mkOption {
|
|
|
|
description = "ssh user";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = str;
|
2023-10-13 21:04:24 +02:00
|
|
|
default = "git";
|
|
|
|
};
|
2024-03-06 12:11:58 +01:00
|
|
|
publicKey = mkOption {
|
|
|
|
description = "public key used for picking the correct key from the ssh-agent";
|
2024-05-05 12:38:40 +02:00
|
|
|
type = nullOr str;
|
2024-03-05 17:36:26 +01:00
|
|
|
default = null;
|
2023-10-13 21:04:24 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2024-03-06 12:03:08 +01:00
|
|
|
default = {};
|
2023-10-13 21:04:24 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf cfg.daemon.enable {
|
|
|
|
services.openssh = {
|
|
|
|
enable = true;
|
|
|
|
settings = {
|
|
|
|
PasswordAuthentication = false;
|
|
|
|
PermitRootLogin = "no";
|
2024-07-01 13:21:26 +02:00
|
|
|
LoginGraceTime = 0;
|
2023-10-13 21:04:24 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(mkIf cfg.agent.enable {
|
2024-03-03 02:25:43 +01:00
|
|
|
programs.ssh = {
|
|
|
|
enableAskPassword = true;
|
2024-07-05 00:41:59 +02:00
|
|
|
askPassword = ksshaskpass;
|
2024-03-03 02:25:43 +01:00
|
|
|
extraConfig = ''
|
|
|
|
AddKeysToAgent yes
|
2023-10-13 21:04:24 +02:00
|
|
|
|
2024-03-03 02:25:43 +01:00
|
|
|
${concatStrings (mapAttrsToList (name: value: ''
|
|
|
|
Host ${name}
|
|
|
|
HostName ${value.hostName}
|
|
|
|
User ${value.user}
|
2024-03-06 12:11:58 +01:00
|
|
|
${
|
|
|
|
if value.port != null then
|
|
|
|
"Port ${toString value.port}"
|
|
|
|
else ""
|
|
|
|
}
|
|
|
|
${
|
|
|
|
if value.publicKey != null then
|
2024-03-06 12:21:02 +01:00
|
|
|
"IdentityFile ${pkgs.writeText "${name}.pub" value.publicKey}"
|
2024-03-06 12:11:58 +01:00
|
|
|
else ""
|
|
|
|
}
|
2024-03-03 02:25:43 +01:00
|
|
|
'') cfg.agent.hostAliases)}
|
|
|
|
'';
|
|
|
|
};
|
2023-10-13 21:04:24 +02:00
|
|
|
|
|
|
|
systemd.user.services.ssh-agent = {
|
|
|
|
enable = true;
|
|
|
|
description = "SSH key agent";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
2024-07-05 00:41:59 +02:00
|
|
|
ExecStart = "${ssh-agent} -D -a $SSH_AUTH_SOCK";
|
2023-10-13 21:04:24 +02:00
|
|
|
};
|
|
|
|
environment = {
|
|
|
|
SSH_AUTH_SOCK = "%t/ssh-agent.socket";
|
|
|
|
DISPLAY = ":0";
|
|
|
|
};
|
|
|
|
wantedBy = [ "default.target" ];
|
|
|
|
};
|
|
|
|
|
2023-10-13 23:18:35 +02:00
|
|
|
environment.sessionVariables = {
|
|
|
|
SSH_AUTH_SOCK = "\$XDG_RUNTIME_DIR/ssh-agent.socket";
|
|
|
|
};
|
2023-10-13 21:04:24 +02:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|