Banged my head against this for quite a while. For this to work, you need to ensure that you:
- foward an ssh agent (
-Aor-o ForwardAgent=yes)… - …which has your keys added to it (run
ssh-addonce before, or use-o AddKeysToAgent=yes):
(EDIT: Or put the equivalent in ~/.ssh/config as mentined above)
{
security = {
# Allow passwordless sudo when connecting via ssh (we already auth'd via public key, that's enough)
# see https://discourse.nixos.org/t/nixos-rebuild-remote-deployments-non-root-pam/50477/19?u=nobodyinperson
# (rssh instead of sshAgentAuth, which apparently does not support ed25519 keys)
# When connecting via ssh, make sure to:
# • use options `-A` or `-o ForwardAgent=yes`
# • Add your ssh key to your local agent before connecting, e.g.
# • manually: ssh-add;ssh -o ForwardAgent=yes ...
# • automatically: ssh -o ForwardAgent=yes -o AddKeysToAgent=yes ...
pam.rssh.enable = true;
pam.rssh.settings.auth_key_file = "/etc/ssh/authorized_keys.d/$ruser";
pam.services.sudo.rssh = true;
};
}