Jump to content

Swayidle: Difference between revisions

From NixOS Wiki
34j (talk | contribs)
mNo edit summary
34j (talk | contribs)
No edit summary
Line 18: Line 18:
# Suspend the system
# Suspend the system


which could be set using {{ic|services.swayidle.timeouts}}.
which could be set using {{ic|services.swayidle.timeouts}}. {{ic|services.swayidle.events}} is useful to make the behavior consistent with the above in case {{ic|systemctl suspend}}, {{ic|loginctl lock-session}} are manually run.
 
{{ic|services.swayidle.events}} is useful in case commands such as {{ic|systemctl suspend}} are manually run.


{{file|~/.config/home-manager/home.nix|nix|3=
{{file|~/.config/home-manager/home.nix|nix|3=
services.swayidle =
services.swayidle =
let
let
  # Lock command
  lock = "${pkgs.swaylock}/bin/swaylock --daemonize";
   # Sway
   # Sway
   display = status: "swaymsg 'output * power ${status}'"; \
   display = status: "swaymsg 'output * power ${status}'"; \
Line 41: Line 41:
     {
     {
       timeout = 20;
       timeout = 20;
       command = "${pkgs.swaylock}/bin/swaylock --daemonize";
       command = lock;
     }
     }
     {
     {
Line 60: Line 60:
     {
     {
       event = "before-sleep";
       event = "before-sleep";
       command = "${pkgs.swaylock}/bin/swaylock --daemonize";
       command = lock;
     }
     }
     {
     {
       event = "after-resume";
       event = "after-resume";
       command = display "on";
       command = display "on";
    }
    {
      event = "lock";
      command = lock;
     }
     }
     {
     {

Revision as of 15:22, 4 July 2025

Swayidle is an idle management daemon for Wayland.

Installation

Install via Home Manager:

❄︎ ~/.config/home-manager/home.nix
services.swayidle.enable = true;

Configuration

Swayidle has a lot of flexibility in its setup, but what is usually done is as follows:

  1. Send notification before doing the following
  2. Lock screen
  3. Turn off the screen (Note that the command may differ among window managers)
  4. Suspend the system

which could be set using services.swayidle.timeouts. services.swayidle.events is useful to make the behavior consistent with the above in case systemctl suspend, loginctl lock-session are manually run.

❄︎ ~/.config/home-manager/home.nix
services.swayidle =
let
  # Lock command
  lock = "${pkgs.swaylock}/bin/swaylock --daemonize";
  # Sway
  display = status: "swaymsg 'output * power ${status}'"; \
  # Hyprland
  # display = status: "hyprctl dispatch dpms ${status}";
  # Niri
  # display = status: "${pkgs.niri}/bin/niri msg action power-${status}-monitors";
in
{
  enable = true;
  timeouts = [
    {
      timeout = 15;
      command = "${pkgs.libnotify}/bin/notify-send 'Locking in 5 seconds' -t 5000";
    }
    {
      timeout = 20;
      command = lock;
    }
    {
      timeout = 25;
      command = display "off";
      resumeCommand = display "on";
    }
    {
      timeout = 30;
      command = "${pkgs.systemd}/bin/systemctl suspend";
    }
  ];
  events = [
    {
      event = "before-sleep";
      command = display "off";
    }
    {
      event = "before-sleep";
      command = lock;
    }
    {
      event = "after-resume";
      command = display "on";
    }
    {
      event = "lock";
      command = lock;
    }
    {
      event = "lock";
      command = display "off";
    }
    {
      event = "unlock";
      command = display "on";
    }
  ];
};