1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-06 00:51:04 +01:00
home-manager/modules/services/swayidle.nix
Patrick Steinhardt 281e9398cc swayidle: improve confusing "events" configuration
With swayidle one can configure two different kinds of hooks:

  - Idle timeouts are executed after the session has been idle for a
    specific amount of time.

  - Events are executed when systemd notifies us that for example the
    user session is locked or that the device is about to suspend.

While not obvious, there is a significant difference between how these
two kinds are configured: there can be several timeouts with separate
commands to be executed, but each event can only be specified once. If
an event is specified multiple times, then the last command wins.

This can be very easy to miss in swayidle's documentation. Furthermore,
because the config is a list of `{ event = "..."; command = "..."; }`
attrset, we double down on this confusion and make it seem like having
multiple handlers for an event was actually supported.

Fix this by converting from a list of "event" submodules to an attrset
where the key is the event name and the value is the command to be
executed. This makes it impossible to specify multiple commands for a
single event by accident.

If a user _does_ want to have multiple commands executed on any event
they can for example use `pkgs.writeShellScript` and manually chain the
commands in that script.
2025-12-02 22:24:56 -06:00

194 lines
5 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkOption
types
literalExpression
;
cfg = config.services.swayidle;
in
{
meta.maintainers = [ lib.maintainers.c0deaddict ];
options.services.swayidle =
let
timeoutModule = {
options = {
timeout = mkOption {
type = types.ints.positive;
example = 60;
description = "Timeout in seconds.";
};
command = mkOption {
type = types.str;
description = "Command to run after timeout seconds of inactivity.";
};
resumeCommand = mkOption {
type = with types; nullOr str;
default = null;
description = "Command to run when there is activity again.";
};
};
};
eventsModule = {
options = {
before-sleep = mkOption {
type = types.nullOr types.str;
default = null;
description = "Command to run before suspending.";
};
after-resume = mkOption {
type = types.nullOr types.str;
default = null;
description = "Command to run after resuming.";
};
lock = mkOption {
type = types.nullOr types.str;
default = null;
description = "Command to run when the logind session is locked.";
};
unlock = mkOption {
type = types.nullOr types.str;
default = null;
description = "Command to run when the logind session is unlocked.";
};
};
};
in
{
enable = lib.mkEnableOption "idle manager for Wayland";
package = lib.mkPackageOption pkgs "swayidle" { };
timeouts = mkOption {
type = with types; listOf (submodule timeoutModule);
default = [ ];
example = literalExpression ''
[
{ timeout = 60; command = "''${pkgs.swaylock}/bin/swaylock -fF"; }
{ timeout = 90; command = "''${pkgs.systemd}/bin/systemctl suspend"; }
]
'';
description = "List of commands to run after idle timeout.";
};
events = mkOption {
type =
with types;
(coercedTo (listOf attrs)) (
events:
lib.warn
''
The syntax of services.swayidle.events has changed. While it
previously accepted a list of events, it now accepts an attrset
keyed by the event name.
''
(
lib.listToAttrs (
map (e: {
name = e.event;
value = e.command;
}) events
)
)
) (submodule eventsModule);
default = [ ];
example = literalExpression ''
{
"before-sleep" = "''${pkgs.swaylock}/bin/swaylock -fF";
"lock" = "lock";
}
'';
description = "Run command on occurrence of a event.";
};
extraArgs = mkOption {
type = with types; listOf str;
default = [ "-w" ];
description = "Extra arguments to pass to swayidle.";
};
systemdTarget = mkOption {
type = types.str;
default = config.wayland.systemd.target;
defaultText = literalExpression "config.wayland.systemd.target";
example = "sway-session.target";
description = ''
Systemd target to bind to.
'';
};
};
config = lib.mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "services.swayidle" pkgs lib.platforms.linux)
];
home.packages = [ cfg.package ];
systemd.user.services.swayidle = {
Unit = {
Description = "Idle manager for Wayland";
Documentation = "man:swayidle(1)";
ConditionEnvironment = "WAYLAND_DISPLAY";
PartOf = [ cfg.systemdTarget ];
After = [ cfg.systemdTarget ];
};
Service = {
Type = "simple";
Restart = "always";
# swayidle executes commands using "sh -c", so the PATH needs to contain a shell.
Environment = [ "PATH=${lib.makeBinPath [ pkgs.bash ]}" ];
ExecStart =
let
mkTimeout =
t:
[
"timeout"
(toString t.timeout)
t.command
]
++ lib.optionals (t.resumeCommand != null) [
"resume"
t.resumeCommand
];
mkEvent = event: command: [
event
command
];
nonemptyEvents = lib.filterAttrs (event: command: command != null) cfg.events;
args =
cfg.extraArgs
++ (lib.concatMap mkTimeout cfg.timeouts)
++ (lib.flatten (lib.mapAttrsToList mkEvent nonemptyEvents));
in
"${lib.getExe cfg.package} ${lib.escapeShellArgs args}";
};
Install = {
WantedBy = [ cfg.systemdTarget ];
};
};
};
}