According to man configuration.nix, enabling services.yubikey-agent should start yubikey-agent on login:
services.yubikey-agent.enable
Whether to start yubikey-agent when you log in. Also sets SSH_AUTH_SOCK to point
at yubikey-agent.
Note that yubikey-agent will use whatever pinentry is specified in
programs.gnupg.agent.pinentryFlavor.
Type: boolean
Default: false
Declared by:
<nixpkgs/nixos/modules/services/security/yubikey-agent.nix>
But this doesn’t seem to be working any more for me, even after restart:
➤ grep yubikey-agent /etc/nixos/configuration.nix
services.yubikey-agent.enable = true; # used for SSH agent
➤ ssh-add -l
Error connecting to agent: Connection refused
➤ pgrep -f yubikey || echo "not found"
not found
Further confusing me is that systemctl can’t even find the yubikey-agent.service:
➤ systemctl start yubikey-agent.service
Failed to start yubikey-agent.service: Unit yubikey-agent.service not found.
Reading up on systemd’s search path in man systemd.unit, it doesn’t look like there’s any reason for it to look in /run/current-system/sw/lib/systemd. It looks in /run/current-system/sw/share/systemd because of XDG_DATA_DIRS, but nothing points it at lib. It does look in /usr/lib/systemd, but that’s a hardcoded path in systemd, and doesn’t get redirected on nixos.
Looking at the code for the module you’re using, it looks like it adds the service through the systemd.user.services option, which should place a unit file in /etc/systemd/user, where it should be picked up normally. The one that comes from the upstream package is intentionally ignored.
Well, systemctl --user start yubikey-agent should absolutely be finding that file, unless some kind of containerization technology is changing the view of the filesystem… I’m out of ideas.
Checked journalctl after a reboot and saw a complaint about ~/.config/systemd/user/yubikey-agent.service:
➤ journalctl -S 2023-04-10 -g yubikey
-- Boot 8f2a505387e3406bb1c2cc54bdcdc5e9 --
Apr 10 08:47:00 johrlac kernel: usb 1-3: Product: YubiKey OTP+FIDO+CCID
Apr 10 08:47:00 johrlac kernel: input: Yubico YubiKey OTP+FIDO+CCID as /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/0003:1050:0407.0001/input/input9
Apr 10 08:47:00 johrlac kernel: hid-generic 0003:1050:0407.0001: input,hidraw0: USB HID v1.10 Keyboard [Yubico YubiKey OTP+FIDO+CCID] on usb-0000:00:14.0-3/input0
Apr 10 08:47:00 johrlac kernel: hid-generic 0003:1050:0407.0002: hiddev96,hidraw1: USB HID v1.10 Device [Yubico YubiKey OTP+FIDO+CCID] on usb-0000:00:14.0-3/input1
Apr 10 08:47:40 johrlac systemd[1908]: yubikey-agent.service: Failed to open /home/rampion/.config/systemd/user/yubikey-agent.service: No such file or directory
~/.config/systemd/user/yubikey-agent.service was a symlink to a missing file in my nix store.
➤ readlink ~/.config/systemd/user/yubikey-agent.service
/nix/store/irkv07kbhilji142b4fmh6hlfqayn9p0-yubikey-agent-0.1.5/lib/systemd/user/yubikey-agent.service
➤ ls $(readlink ~/.config/systemd/user/yubikey-agent.service)
ls: cannot access '/nix/store/irkv07kbhilji142b4fmh6hlfqayn9p0-yubikey-agent-0.1.5/lib/systemd/user/yubikey-agent.service': No such file or directory
After another NixOS user told me they didn’t have any such file, I deleted it and rebooted, and yubikey-agent was started successfully.
From checking my diary, this symlink was created back when I ran systemctl --user enable --now yubikey-agent when originally setting up my yubikey, rather than just setting services.yubikey-agent.enable = true in my configuration.nix.