Jump to content

Swayidle: Difference between revisions

From Official NixOS Wiki
Klinger (talk | contribs)
m link to swayidle and category wayland
Phobos (talk | contribs)
m Added install through config/sway
 
Line 3: Line 3:
== Installation ==
== Installation ==


Install via [[Home Manager]]:
=== Standalone ===
{{File|3=environment.systemPackages = with pkgs; [
  swayidle
];|name=/etc/nixos/configuration.nix|lang=nix}}


{{file|~/.config/home-manager/home.nix|nix|3=
=== Home Manager - Standalone ===
services.swayidle.enable = true;
{{File|3=services.swayidle.enable = true;|name=/etc/nixos/home.nix|lang=nix}}
}}
 
=== Home Manager - Through Sway ===
{{File|3=programs.sway.extraPackages = with pkgs; [swayidle];|name=/etc/nixos/home.nix|lang=nix}}


== Configuration ==
== Configuration ==
Line 18: Line 23:
# 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.
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.
 
Additional options may be found within the Home Manager Appendix under [https://nix-community.github.io/home-manager/options.xhtml#opt-services.swayidle.enable services.swayidle].


{{file|~/.config/home-manager/home.nix|nix|3=
{{file|~/.config/home-manager/home.nix|nix|3=

Latest revision as of 03:29, 1 January 2026

Swayidle is an idle management daemon for Wayland.

Installation

Standalone

❄︎ /etc/nixos/configuration.nix
environment.systemPackages = with pkgs; [
  swayidle
];

Home Manager - Standalone

❄︎ /etc/nixos/home.nix
services.swayidle.enable = true;

Home Manager - Through Sway

❄︎ /etc/nixos/home.nix
programs.sway.extraPackages = with pkgs; [swayidle];

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.

Additional options may be found within the Home Manager Appendix under services.swayidle.

❄︎ ~/.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.