summaryrefslogtreecommitdiff
path: root/modules/programs/ssh/default.nix
diff options
context:
space:
mode:
authorMichael Hoang <enzime@users.noreply.github.com>2023-05-31 22:22:02 +1000
committerMichael Hoang <enzime@users.noreply.github.com>2023-06-08 15:44:07 +1000
commit257b5c199490aa073ab60c1d22d213025661dd44 (patch)
treec0cc3949060087fcf09a534657376c62b80d72b8 /modules/programs/ssh/default.nix
parentb8c286c82c6b47826a6c0377e7017052ad91353c (diff)
ssh: fix public keys in home directory not working
Added `services.openssh.authorizedKeysFiles` option from NixOS.
Diffstat (limited to 'modules/programs/ssh/default.nix')
-rw-r--r--modules/programs/ssh/default.nix51
1 files changed, 40 insertions, 11 deletions
diff --git a/modules/programs/ssh/default.nix b/modules/programs/ssh/default.nix
index f1dde9a..a230dde 100644
--- a/modules/programs/ssh/default.nix
+++ b/modules/programs/ssh/default.nix
@@ -1,4 +1,4 @@
-{ config, lib, pkgs, ... }:
+{ config, lib, ... }:
with lib;
@@ -79,6 +79,7 @@ let
};
};
+
authKeysFiles = let
mkAuthKeyFile = u: nameValuePair "ssh/authorized_keys.d/${u.name}" {
copy = true;
@@ -91,22 +92,33 @@ let
length u.openssh.authorizedKeys.keys != 0 || length u.openssh.authorizedKeys.keyFiles != 0
));
in listToAttrs (map mkAuthKeyFile usersWithKeys);
- authKeysConfiguration =
- {
- "ssh/sshd_config.d/101-authorized-keys.conf" = {
- copy = true;
- text = "AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u\n";
- };
- };
+
+ oldAuthorizedKeysHash = "5a5dc1e20e8abc162ad1cc0259bfd1dbb77981013d87625f97d9bd215175fc0a";
in
{
options = {
-
+
users.users = mkOption {
type = with types; attrsOf (submodule userOptions);
};
+ services.openssh.authorizedKeysFiles = mkOption {
+ type = types.listOf types.str;
+ default = [];
+ description = lib.mdDoc ''
+ Specify the rules for which files to read on the host.
+
+ This is an advanced option. If you're looking to configure user
+ keys, you can generally use [](#opt-users.users._name_.openssh.authorizedKeys.keys)
+ or [](#opt-users.users._name_.openssh.authorizedKeys.keyFiles).
+
+ These are paths relative to the host root file system or home
+ directories and they are subject to certain token expansion rules.
+ See AuthorizedKeysFile in man sshd_config for details.
+ '';
+ };
+
programs.ssh.knownHosts = mkOption {
default = {};
type = types.attrsOf (types.submodule host);
@@ -135,13 +147,30 @@ in
(data.publicKey != null && data.publicKeyFile == null);
message = "knownHost ${name} must contain either a publicKey or publicKeyFile";
});
-
- environment.etc = authKeysFiles // authKeysConfiguration //
+
+ services.openssh.authorizedKeysFiles = [ "%h/.ssh/authorized_keys" "/etc/ssh/authorized_keys.d/%u" ];
+
+ environment.etc = authKeysFiles //
{ "ssh/ssh_known_hosts".text = (flip (concatMapStringsSep "\n") knownHosts
(h: assert h.hostNames != [];
concatStringsSep "," h.hostNames + " "
+ (if h.publicKey != null then h.publicKey else readFile h.publicKeyFile)
)) + "\n";
+
+ "ssh/sshd_config.d/101-authorized-keys.conf" = {
+ text = "AuthorizedKeysFile ${toString config.services.openssh.authorizedKeysFiles}\n";
+ # Allows us to automatically migrate from using a file to a symlink
+ knownSha256Hashes = [ oldAuthorizedKeysHash ];
+ };
};
+
+ # Clean up .orig file left over from using knownSha256Hashes
+ system.activationScripts.etc.text = ''
+ auth_keys_orig=/etc/ssh/sshd_config.d/101-authorized-keys.conf.orig
+
+ if [ -e "$auth_keys_orig" ] && [ "$(shasum -a 256 $auth_keys_orig | cut -d ' ' -f 1)" = "${oldAuthorizedKeysHash}" ]; then
+ rm "$auth_keys_orig"
+ fi
+ '';
};
}