# SSH, both halves: # NixOS key-only OpenSSH server; its host key is also the agenix identity. # home-manager the owner's client config, agent and keys. Private keys are # agenix secrets decrypted into /run/agenix (security/secrets.nix); # this only links them into ~/.ssh, reading what the NixOS side # declared through `osConfig.age.secrets`. { config, ... }: let inherit (config.meta) owner secretsDir; hmSsh = config.flake.modules.homeManager.ssh; in { flake.modules.nixos.ssh = { home-manager.users.${owner.name}.imports = [ hmSsh ]; services.openssh = { enable = true; openFirewall = true; settings = { PasswordAuthentication = false; KbdInteractiveAuthentication = false; PermitRootLogin = "no"; }; hostKeys = [ { type = "ed25519"; path = "/etc/ssh/ssh_host_ed25519_key"; } ]; }; # home-manager's ssh-agent service provides the agent. programs.ssh.startAgent = false; }; flake.modules.homeManager.ssh = { config, lib, osConfig, ... }: let # Keys that have a secret on this host. Without one nothing is linked, so # a fresh install still works and ssh uses whatever key the user makes. keys = lib.filter (k: osConfig.age.secrets ? "ssh-${k}") owner.sshKeys; pubFile = k: secretsDir + "/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 { 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. services.ssh-agent.enable = true; home.file = keyFiles // pubFiles; }; }