Files
NixOsConfig/modules/home/ssh.nix
T
2026-10-12 18:15:38 +03:00

82 lines
2.4 KiB
Nix
Executable File

# SSH client, agent and the user's keys.
# Private keys are agenix secrets (decrypted at boot into /run/agenix); this
# module only links them into ~/.ssh so ssh finds them at the usual path.
{
config,
lib,
osConfig,
...
}:
let
cfg = config.my.home.ssh;
secrets = osConfig.my.secrets;
# Keys that actually have an encrypted secret in the repo. Without a secret
# nothing is linked, so a fresh install still boots and ssh falls back to
# whatever key the user generates by hand.
keys = lib.filter (k: osConfig.age.secrets ? "ssh-${k}") secrets.userSshKeys;
pubFile = k: secrets.dir + "/ssh-${k}.pub";
# The decrypted file lives outside the store, so link it by absolute path.
keyFiles = lib.listToAttrs (
map (
k:
lib.nameValuePair ".ssh/${k}" {
source = config.lib.file.mkOutOfStoreSymlink osConfig.age.secrets."ssh-${k}".path;
}
) keys
);
# Public keys are not secret, so they are plain files from the repo.
pubFiles = lib.listToAttrs (
map (k: lib.nameValuePair ".ssh/${k}.pub" { source = pubFile k; }) (
lib.filter (k: builtins.pathExists (pubFile k)) keys
)
);
in
{
options.my.home.ssh.enable = lib.mkEnableOption "SSH client, agent and agenix-provided keys";
config = lib.mkIf cfg.enable {
programs.ssh = {
enable = true;
# Spell the defaults out below instead of relying on the deprecated ones.
enableDefaultConfig = false;
settings = {
"*" = {
# Load a key into the agent on first use, so the passphrase (if any)
# is typed once per login.
AddKeysToAgent = "yes";
ServerAliveInterval = 60;
ServerAliveCountMax = 3;
HashKnownHosts = false;
UserKnownHostsFile = "~/.ssh/known_hosts";
};
"github.com" = {
User = "git";
IdentityFile = "~/.ssh/id_ed25519";
# Offer only this key, not everything in the agent.
IdentitiesOnly = true;
};
"git.protei.ru" = {
User = "git";
IdentityFile = "~/.ssh/id_ed25519_work";
IdentitiesOnly = true;
};
};
};
# A plain user service rather than gnome-keyring's agent: no keyring to
# unlock, and it sets SSH_AUTH_SOCK for shells, systemd and D-Bus. The
# system must not start another agent (programs.ssh.startAgent).
services.ssh-agent.enable = true;
home.file = keyFiles // pubFiles;
};
}