Jump to content

Swayidle: Difference between revisions

From Official NixOS Wiki
34j (talk | contribs)
Add Swayidle
 
Fix swaymsg package (prior to this change: `swaymsg: command not found`)
 
(8 intermediate revisions by 2 users not shown)
Line 14: Line 14:


# Send notification before doing the following
# Send notification before doing the following
# Lock screen
# Lock screen (typically [[Swaylock]] is used)
# Turn off the screen (Note that the command may differ among window managers)
# Turn off the screen (Note that the command may differ among window managers)
# Suspend the system
# Suspend the system
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.


{{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";
  # TODO: modify "display" function based on your window manager
   # Sway
   # Sway
   display = status: "swaymsg 'output * power ${status}'"; \
   display = status: "${pkgs.sway}/bin/swaymsg 'output * power ${status}'";
   # Hyprland
   # Hyprland
   # display = status: "hyprctl dispatch dpms ${status}";
   # display = status: "hyprctl dispatch dpms ${status}";
Line 32: Line 37:
   timeouts = [
   timeouts = [
     {
     {
       timeout = 15;
       timeout = 15; # in seconds
       command = "${pkgs.libnotify}/bin/notify-send 'Locking in 5 seconds' -t 5000";
       command = "${pkgs.libnotify}/bin/notify-send 'Locking in 5 seconds' -t 5000";
     }
     }
     {
     {
       timeout = 20;
       timeout = 20;
       command = "${pkgs.swaylock}/bin/swaylock --daemonize";
       command = lock;
     }
     }
     {
     {
Line 52: Line 57:
     {
     {
       event = "before-sleep";
       event = "before-sleep";
       command = display "off";
      # adding duplicated entries for the same event may not work
    }
       command = (display "off") + "; " + lock;
    {
      event = "before-sleep";
      command = "${pkgs.swaylock}/bin/swaylock --daemonize";
     }
     }
     {
     {
Line 64: Line 66:
     {
     {
       event = "lock";
       event = "lock";
       command = display "off";
       command = (display "off") + "; " + lock;
     }
     }
     {
     {
Line 72: Line 74:
   ];
   ];
};
};
}}
|name=~/.config/home-manager/home.nix|lang=nix}}
 
See [https://man.archlinux.org/man/extra/swayidle/swayidle.1.en the man page] for further information.

Latest revision as of 23:16, 27 November 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 (typically Swaylock is used)
  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";
  # TODO: modify "display" function based on your window manager
  # Sway
  display = status: "${pkgs.sway}/bin/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; # in seconds
      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";
      # adding duplicated entries for the same event may not work
      command = (display "off") + "; " + lock;
    }
    {
      event = "after-resume";
      command = display "on";
    }
    {
      event = "lock";
      command = (display "off") + "; " + lock;
    }
    {
      event = "unlock";
      command = display "on";
    }
  ];
};

See the man page for further information.