Update - 08-13-26

A friend recently brought up that the version of the script below only checks that a yubikey has been connected not necessarily my yubikey. This is a very good point and luckily, is easily fixable. Instead of detecting the USB device like I was doing previously I am now using ykman. With this I can query for the serial number of the connected yubikey. Ultimately this method is still flawed as there are multiple ways to bypass this check but this is more an interesting concept than an actual security config. The updated config can be found below this with the original, unedited blog post below that.

...
  environment.systemPackages = with pkgs; [
    yubikey-manager
  ];
  ...
 systemd.user.services."yubikey-relock" = {
   description = "Re-lock screen if YubiKey is absent";
   serviceConfig = {
     Type = "simple";
     ExecStart = pkgs.writeShellScript "yubikey-relock" ''
       while true; do
         if ! ${pkgs.yubikey-manager}/bin/ykman info | grep -q "SERIAL_NUMBER"; then
           echo "YubiKey not detected, attempting lock..."
           if /etc/profiles/per-user/krode/bin/noctalia msg status &>/dev/null; then
             echo "Noctalia responding, locking..."
             /etc/profiles/per-user/krode/bin/noctalia msg session lock
           else
             echo "Noctalia not responding, skipping lock"
           fi
         else
           echo "YubiKey present"
         fi
         sleep 5
       done
     '';
           Restart = "always";
   };
   environment = {
     WAYLAND_DISPLAY = "wayland-1";
     XDG_RUNTIME_DIR = "/run/user/1000";
   };
   wantedBy = [ "default.target" ];
 };

Original Blog content

Recently I finally pulled myself into the 21st Century and got myself a YubiKey. As I was going through setting up all my access I had the idea that it would be cool if when I removed the key from my computer the machine should lock automatically. Conceptually this shouldn’t be too complicated. The machine needs to identify when the YubiKey is and isn’t connected, and lock the computer if it isn’t seen. Additionally, the machine needs to be in a loop of looking for the key otherwise it won’t relock after entering the password. The other important piece is that my machines all run NixOS. Therefore, this will all be defined declaratively in my config files.

Identifying YubiKey Status

This part is very simple. lsusb can be used to identify all USB devices currently connected to the system. If we run that while the YubiKey is connected we can get the vendor and device ids. These unique ids will be used in our next steps to detect when we need to lock the screen. The below command should let you identify your specific YubiKey info.

$ lsusb | grep Yubico
Bus 005 Device 014: ID 1050:0407 Yubico.com Yubikey 4/5 OTP+U2F+CCID

Creating the Search Loop

The simplest way I could think of to do this was a systemd user service. The service is currently set to start and check if the YubiKey is present. If it is, nothing happens and we move on, wait 5 seconds then start the service again to complete the loop. If the YubiKey isn’t present, then we run the lock screen command, wait 5 seconds and the service restarts. The full definition from my nix config can be seen below.

systemd.user.services."yubikey-relock" = {
   description = "Re-lock screen if YubiKey is absent";
   serviceConfig = {
     Type = "simple";
     ExecStart = pkgs.writeShellScript "yubikey-relock" ''
       while true; do
         if ! ${pkgs.usbutils}/bin/lsusb | grep -q "1050:0407"; then
           echo "YubiKey not detected, attempting lock..."
           if /etc/profiles/per-user/krode/bin/noctalia msg status &>/dev/null; then
             echo "Noctalia responding, locking..."
             /etc/profiles/per-user/krode/bin/noctalia msg session lock
           else
             echo "Noctalia not responding, skipping lock"
           fi
         else
           echo "YubiKey present"
         fi
         sleep 5
       done
     '';
           Restart = "always";
   };
   environment = {
     WAYLAND_DISPLAY = "wayland-1";
     XDG_RUNTIME_DIR = "/run/user/1000";
   };
   wantedBy = [ "default.target" ];
 };

The other environment pieces are specific to my use of noctalia instead of a traditional desktop environment. Because noctalia operates as a windows manager, additional environment variables need to be passed so that the correct wayland session and user are used to run the lock command

Conclusion

And that’s it, unplug the YubiKey and within 5 seconds the device is locked. This time can always be shortened by adjusting the sleep portion of the service but for my use case and risk summary it’s been more than sufficient.