|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +with lib; |
| 4 | + |
| 5 | +let |
| 6 | + cfg = config.services.yubilock; |
| 7 | + |
| 8 | + # Script paths - users should copy scripts to their ~/.config/waybar/scripts/ |
| 9 | + yubilockScript = pkgs.writeShellScript "yubilock" '' |
| 10 | + STATE_FILE="$HOME/.cache/yubilock-state" |
| 11 | + LOG_FILE="$HOME/.cache/yubilock.log" |
| 12 | + PID_FILE="$HOME/.cache/yubilock.pid" |
| 13 | +
|
| 14 | + # Function to check if a YubiKey is currently plugged in |
| 15 | + check_yubikey() { |
| 16 | + if ${pkgs.usbutils}/bin/lsusb | ${pkgs.gnugrep}/bin/grep -i "yubikey" > /dev/null; then |
| 17 | + return 0 # device is present |
| 18 | + else |
| 19 | + return 1 # device is not present |
| 20 | + fi |
| 21 | + } |
| 22 | +
|
| 23 | + # Function to lock the screen |
| 24 | + lock_screen() { |
| 25 | + # Using loginctl for systemd-based systems |
| 26 | + ${pkgs.systemd}/bin/loginctl lock-session |
| 27 | + echo "Screen locked at $(date)" >> "$LOG_FILE" |
| 28 | + } |
| 29 | +
|
| 30 | + # Create state file if it doesn't exist |
| 31 | + if [ ! -f "$STATE_FILE" ]; then |
| 32 | + echo "off" > "$STATE_FILE" |
| 33 | + fi |
| 34 | +
|
| 35 | + # Record PID for later termination |
| 36 | + echo "$$" > "$PID_FILE" |
| 37 | +
|
| 38 | + # Main monitoring loop |
| 39 | + echo "YubiKey monitoring started at $(date)" >> "$LOG_FILE" |
| 40 | +
|
| 41 | + while true; do |
| 42 | + # Check if monitoring is still enabled |
| 43 | + if [ "$(cat "$STATE_FILE")" != "on" ]; then |
| 44 | + echo "YubiKey monitoring stopped at $(date)" >> "$LOG_FILE" |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | +
|
| 48 | + if check_yubikey; then |
| 49 | + echo "YubiKey detected at $(date)" >> "$LOG_FILE" |
| 50 | +
|
| 51 | + # Wait until the YubiKey is removed |
| 52 | + while check_yubikey && [ "$(cat "$STATE_FILE")" = "on" ]; do |
| 53 | + sleep 1 |
| 54 | + done |
| 55 | +
|
| 56 | + # If we exited because service was disabled, exit gracefully |
| 57 | + if [ "$(cat "$STATE_FILE")" != "on" ]; then |
| 58 | + echo "YubiKey monitoring stopped at $(date)" >> "$LOG_FILE" |
| 59 | + exit 0 |
| 60 | + fi |
| 61 | +
|
| 62 | + echo "YubiKey removed at $(date)" >> "$LOG_FILE" |
| 63 | + lock_screen |
| 64 | + else |
| 65 | + echo "No YubiKey detected. Checking again in 10 seconds..." >> "$LOG_FILE" |
| 66 | + # Check less frequently to reduce system load |
| 67 | + sleep 10 |
| 68 | + fi |
| 69 | + done |
| 70 | + ''; |
| 71 | + |
| 72 | + yubilockRestoreScript = pkgs.writeShellScript "yubilock-restore" '' |
| 73 | + STATE_FILE="$HOME/.cache/yubilock-state" |
| 74 | + LOG_FILE="$HOME/.cache/yubilock-restore.log" |
| 75 | + |
| 76 | + echo "[$(date)] Checking yubilock state on login" >> "$LOG_FILE" |
| 77 | + |
| 78 | + # Create state file if it doesn't exist |
| 79 | + if [ ! -f "$STATE_FILE" ]; then |
| 80 | + echo "off" > "$STATE_FILE" |
| 81 | + echo "[$(date)] No state file found, defaulting to off" >> "$LOG_FILE" |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | + |
| 85 | + # Read the saved state |
| 86 | + saved_state=$(cat "$STATE_FILE") |
| 87 | + echo "[$(date)] Saved state: $saved_state" >> "$LOG_FILE" |
| 88 | + |
| 89 | + # If it was enabled before, re-enable it |
| 90 | + if [ "$saved_state" = "on" ]; then |
| 91 | + if ! ${pkgs.systemd}/bin/systemctl --user is-active yubilock.service > /dev/null 2>&1; then |
| 92 | + echo "[$(date)] Restoring yubilock service" >> "$LOG_FILE" |
| 93 | + ${pkgs.systemd}/bin/systemctl --user start yubilock.service |
| 94 | + echo "[$(date)] Yubilock service restored" >> "$LOG_FILE" |
| 95 | + else |
| 96 | + echo "[$(date)] Yubilock service already running" >> "$LOG_FILE" |
| 97 | + fi |
| 98 | + fi |
| 99 | + ''; |
| 100 | + |
| 101 | +in { |
| 102 | + options.services.yubilock = { |
| 103 | + enable = mkEnableOption "YubiKey screen lock monitor"; |
| 104 | + |
| 105 | + autoRestore = mkOption { |
| 106 | + type = types.bool; |
| 107 | + default = true; |
| 108 | + description = '' |
| 109 | + Automatically restore yubilock state on login. |
| 110 | + If enabled, the yubilock service will be restarted on login |
| 111 | + if it was running when you last logged out. |
| 112 | + ''; |
| 113 | + }; |
| 114 | + }; |
| 115 | + |
| 116 | + config = mkIf cfg.enable { |
| 117 | + # Systemd user service for yubilock |
| 118 | + systemd.user.services.yubilock = { |
| 119 | + Unit = { |
| 120 | + Description = "YubiKey lock screen monitor"; |
| 121 | + After = [ "graphical-session.target" ]; |
| 122 | + PartOf = [ "graphical-session.target" ]; |
| 123 | + }; |
| 124 | + Service = { |
| 125 | + Type = "simple"; |
| 126 | + ExecStart = "${yubilockScript}"; |
| 127 | + Restart = "on-failure"; |
| 128 | + RestartSec = "5s"; |
| 129 | + # Ensure state persists |
| 130 | + ExecStartPre = "${pkgs.coreutils}/bin/mkdir -p %h/.cache"; |
| 131 | + # Clean state on stop |
| 132 | + ExecStopPost = "${pkgs.bash}/bin/bash -c 'echo off > %h/.cache/yubilock-state'"; |
| 133 | + }; |
| 134 | + Install = { |
| 135 | + WantedBy = [ "graphical-session.target" ]; |
| 136 | + }; |
| 137 | + }; |
| 138 | + |
| 139 | + # Systemd user service to restore yubilock state on login |
| 140 | + systemd.user.services.yubilock-restore = mkIf cfg.autoRestore { |
| 141 | + Unit = { |
| 142 | + Description = "Restore YubiKey monitor state on login"; |
| 143 | + After = [ "graphical-session.target" ]; |
| 144 | + }; |
| 145 | + Service = { |
| 146 | + Type = "oneshot"; |
| 147 | + ExecStart = "${yubilockRestoreScript}"; |
| 148 | + RemainAfterExit = false; |
| 149 | + }; |
| 150 | + Install = { |
| 151 | + WantedBy = [ "graphical-session.target" ]; |
| 152 | + }; |
| 153 | + }; |
| 154 | + |
| 155 | + # Ensure required packages are available |
| 156 | + home.packages = with pkgs; [ |
| 157 | + usbutils # for lsusb command |
| 158 | + ]; |
| 159 | + }; |
| 160 | +} |
0 commit comments