Files
nix-config/home/base/tui/ssh.nix
T
ryan4yin | 二花 7cfda39c2c feat: log in and sudo over SSH with a CanoKey touch
The CanoKey resident SSH key (`ryan@canokey-moe`, the backup once a new primary key is enrolled) is now authorized for ryan and root on every host, and sudo on server and agent hosts accepts its touch through the forwarded agent (`pam_rssh`). Without the key or a forwarded agent, sudo asks for the password and login falls back to the file keys. Only `canokeySshKeys` can satisfy sudo.

Risks to weigh:

- Colmena deploys need one touch per host connection, since the agent offers the CanoKey key first.
- Desktops now forward the agent to every `hostsAddr` alias, as they already did for `192.168.*`; while a session is open, root there can request signatures, each needing a touch.

### Verification

- `just test` exits 0; CI passed on the first commit.
- Evaluated: sudo PAM stack `rssh` → `unix` → `deny` on shoryu, kana, akane and k3s-test-1-master-1, ai-niri keeps `pam_u2f`; shoryu root and ryan list `ryan@canokey-moe`.
- Not yet verified on a running host.

### Manual step after merge

Deploy the servers, then `just niri`; close old multiplexed connections (`ssh -O exit <host>`) and try `sudo true` over SSH.

AI assistance: DeepSeek Harness (claude-opus-5-5).

Assisted-by: DeepSeek Harness (claude-opus-5-5)
2026-11-06 03:26:59 -06:00

96 lines
3.2 KiB
Nix

{
config,
lib,
pkgs,
mysecrets,
myvars,
...
}:
let
# WA-035: settings for the home LAN, which the desktop reaches over a Mesh
# WiFi backhaul.
lanHost = {
# A lost first SYN must not fail the connection; a LAN handshake is ~2 ms,
# so the connect is cut short and retried instead of hanging.
ConnectTimeout = 1;
ConnectionAttempts = 3;
# One persistent master per host keeps a session (and with it the mesh's
# forwarding entry for that peer) open, so later connections reuse it
# instead of resolving and handshaking again. Three missed keepalives reap a
# master whose path died.
ControlMaster = "auto";
ControlPersist = "30m";
ServerAliveInterval = 15;
};
in
{
home.file.".ssh/romantic.pub".source = "${mysecrets}/public/romantic.pub";
# One SSH key for GitHub and every host lives on the CanoKey as a resident
# FIDO2 credential, exported on each desktop to ~/.ssh/id_ed25519_sk (ssh's
# default identity); every use needs a touch. The per-host keys stay
# authorized as the backup. Setup: docs/security-keys.md.
programs.ssh = {
enable = true;
enableDefaultConfig = false;
# Keep this one definition: Nix merges a `settings."<pattern>"` attribute
# path into `settings = ...` only while that value stays an attrset literal,
# so the generated LAN patterns are merged inside it with `//` instead.
settings = {
"*" = {
ForwardAgent = false;
AddKeysToAgent = "yes";
Compression = true;
ServerAliveInterval = 0;
ServerAliveCountMax = 3;
HashKnownHosts = false;
UserKnownHostsFile = "~/.ssh/known_hosts";
ControlMaster = "no";
ControlPath = "~/.ssh/master-%r@%n:%p";
ControlPersist = "no";
};
"github.com" = {
# WA-028: pinned to IPv4 until the mihomo v6 fake-ip pool is proven here
AddressFamily = "inet";
HostName = "ssh.github.com";
Port = 443;
User = "git";
IdentitiesOnly = true;
};
"192.168.*" = {
ForwardAgent = true;
}
// lanHost;
}
# Aliases forward the agent like their addresses do; servers ask it for sudo
# (modules/nixos/server/ssh-agent-sudo.nix).
// lib.genAttrs (builtins.attrNames myvars.networking.hostsAddr) (
_:
{
ForwardAgent = true;
}
// lanHost
);
};
# Keep ~/.ssh/config a real, user-owned file rather than a /nix/store symlink.
#
# OpenSSH accepts a config owned by root or the invoking user, but
# bubblewrap-based sandboxes (`--unshare-user`) expose root-owned store files
# as `nobody`, which trips the check (`Bad owner or permissions`). Disabling
# the home.file entry keeps Home Manager's linker out of the way; `install`
# writes a fresh 0600 copy on each activation.
#
# Linux only: macOS' Seatbelt sandbox keeps store ownership intact, so the
# normal symlink is fine there.
home.file.".ssh/config".enable = lib.mkIf pkgs.stdenv.hostPlatform.isLinux false;
home.activation.sshConfigRegularFile = lib.mkIf pkgs.stdenv.hostPlatform.isLinux (
lib.hm.dag.entryAfter [ "linkGeneration" ] ''
run install -D -m 600 ${config.home.file.".ssh/config".source} "$HOME/.ssh/config"
''
);
}