Automatically locks or unlocks the screen based on proximity to trusted devices (Wi-Fi, Bluetooth).
fisher install edouard-lopez/smart-lock.fish💡 Requirements
| Tool | Purpose | Cinnamon (X11) |
KDE/GNOME (Wayland) |
KDE/GNOME (X11) |
Other (X11) |
|---|---|---|---|---|---|
icons-in-terminal |
nicer icons | ✓ | ✓ | ✓ | ✓ |
nmcli (via NetworkManager) |
request network status | ✓ | ✓ | ✓ | ✓ |
bluetoothctl |
request Bluetooth status | ✓ | ✓ | ✓ | ✓ |
loginctl |
control lock/unlock | ✓ | ✓ | ✓ | ✓ |
xprintidle |
get/set idle time | ✓ | — | ✓ | ✓ |
xset |
get/set X server settings | ✓ | — | ✓ | ✓ |
cinnamon-screensaver-command |
control lock/unlock | ✓ | — | — | — |
For Debian/Ubuntu-based systems (X11):
apt install network-manager bluez xprintidle x11-xserver-utilsFor Fedora/RHEL-based systems with KDE/GNOME (Wayland):
dnf install NetworkManager bluez #For Fedora/RHEL-based systems with X11:
dnf install NetworkManager bluez xorg-x11-server-utils xprintidle- 1️⃣✅ Configure the trusted devices and idle timeout variables ;
- 2️⃣✅ Usage = run
smart_lock_toggleperiodically (e.g., viasystemdtimer orcron) ; - 3️⃣✅ Display the lock status in your prompt.
Set environment variables to define trusted devices and idle timeout.
| Variable | Default | Description |
|---|---|---|
SMART_LOCK_BSSIDS |
- | List of trusted Wi-Fi BSSIDs as MAC addresses e.g. home Wi-Fi, office Wi-Fi |
SMART_LOCK_DEVICES_MACS |
- | List of trusted Bluetooth as MAC addresses e.g. smartphone, mouse) |
SMART_LOCK_AFTER |
180 |
Idle timeout in seconds |
Example configuration in config.fish:
set --universal --export SMART_LOCK_BSSIDS "AA:BB:CC:DD:EE:FF" "BB:CC:DD:EE:FF:AA"
set --universal --export SMART_LOCK_DEVICES_MACS "CC:DD:EE:FF:AA:BB"
set --universal --export SMART_LOCK_AFTER 300You need to run smart_lock_toggle periodically to check the proximity of trusted devices to update SMART_LOCK_STATUS and trigger screen lock and unlock the screen accordingly.
Systemd handles environment variables and logging better than cron.
Create systemd service and timer
mkdir -p ~/.config/systemd/user/[Unit]
Description=Smart Lock Toggle Service
[Service]
Type=oneshot
ExecStart=/usr/bin/fish -c 'source $__fish_config_dir/functions/smart_lock_toggle.fish && smart_lock_toggle'[Unit]
Description=Smart Lock Toggle Timer
[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
Unit=smart-lock.service
[Install]
WantedBy=timers.targetsystemctl --user enable --now smart-lock.timerIf you prefer cron, use the following.
ℹ️ Note that you may need to set
DISPLAYandDBUS_SESSION_BUS_ADDRESSmanually if they are not picked up.
# give some context to cron jobs
DISPLAY=:0
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
# run every minute
* * * * * fish -c 'source $__fish_config_dir/functions/smart_lock_toggle.fish && smart_lock_toggle'Add the following to your prompt to show lock status:
echo $SMART_LOCK_STATUSℹ️ If you didn't customize your fish_right_prompt.fish, you can use ours:
💡 Install our fish_right_prompt
-
Backup existing
fish_right_prompt.fish:cp $__fish_config_dir/functions/{,__backup_}fish_right_prompt.fish
-
Replace with
smart_lock's version:cp $__fish_config_dir/functions/{__smart_lock_,}fish_right_prompt.fish
You can always run it manually with Fish debugging
set fish_trace 1
smart_lock_toggle
set --erase fish_trace
