Skip to content

Automatically locks or unlocks the screen based on proximity to trusted devices devices and inactivity. Similar to Android's Smart Lock feature.

License

Notifications You must be signed in to change notification settings

edouard-lopez/smart-lock.fish

Repository files navigation

smart-lock.fish ci-status sponsors MIT-img

Automatically locks or unlocks the screen based on proximity to trusted devices (Wi-Fi, Bluetooth).

preview in right prompt (light mode) preview in right prompt (dark mode)

Installation

fisher install edouard-lopez/smart-lock.fish
💡 Requirements
  • icons-in-terminal for gray icons support (optional) ;
  • Fisher - Fish plugin manager
  • xprintidle - X11 idle time detection
  • nmcli - NetworkManager CLI for Wi-Fi detection (via network-manager)
  • bluetoothctl - Bluetooth device detection (via bluez package)
  • cinnamon-screensaver-command (or modify for your DE)
  • xset: to manage display power

For Debian/Ubuntu-based systems:

apt install network-manager bluez xprintidle x11-xserver-utils cinnamon-screensaver

What to do after installation:

  • Configure the trusted devices and idle timeout variables ;
  • Run smart_lock_toggle periodically (e.g., via cron or systemd timer) ;
  • Display the lock status in your prompt.

Configuration

Trusted Devices & Idle Timeout

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 SMART_LOCK_BSSIDS "AA:BB:CC:DD:EE:FF" "BB:CC:DD:EE:FF:AA" 
set --universal SMART_LOCK_DEVICES_MACS "CC:DD:EE:FF:AA:BB"
set --universal SMART_LOCK_AFTER 300

Usage

You 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 Timer (Recommended)

Systemd handles environment variables and logging better than cron.

Create a user-level systemd timer

mkdir -p ~/.config/systemd/user/

Edit ~/.config/systemd/user/smart-lock.service

[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'

Edit ~/.config/systemd/user/smart-lock.timer

[Unit]
Description=Smart Lock Toggle Timer

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
Unit=smart-lock.service

[Install]
WantedBy=timers.target

Enable and start the timer

systemctl --user enable --now smart-lock.timer

Crontab

If you prefer cron, use the following.

ℹ️ Note that you may need to set DISPLAY and DBUS_SESSION_BUS_ADDRESS manually 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'

You can always run it manually

smart_lock_toggle

Finally Prompt

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 our

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 version

cp $__fish_config_dir/functions/{__smart_lock_,}fish_right_prompt.fish

About

Automatically locks or unlocks the screen based on proximity to trusted devices devices and inactivity. Similar to Android's Smart Lock feature.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published