diff --git a/modules/services/swayidle.nix b/modules/services/swayidle.nix index 481dd4406..5af0b4678 100644 --- a/modules/services/swayidle.nix +++ b/modules/services/swayidle.nix @@ -42,21 +42,30 @@ in }; }; - eventModule = { + eventsModule = { options = { - event = mkOption { - type = types.enum [ - "before-sleep" - "after-resume" - "lock" - "unlock" - ]; - description = "Event name."; + before-sleep = mkOption { + type = types.nullOr types.str; + default = null; + description = "Command to run before suspending."; }; - command = mkOption { - type = types.str; - description = "Command to run when event occurs."; + 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."; }; }; }; @@ -80,13 +89,31 @@ in }; events = mkOption { - type = with types; listOf (submodule eventModule); + 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 '' - [ - { event = "before-sleep"; command = "''${pkgs.swaylock}/bin/swaylock -fF"; } - { event = "lock"; command = "lock"; } - ] + { + "before-sleep" = "''${pkgs.swaylock}/bin/swaylock -fF"; + "lock" = "lock"; + } ''; description = "Run command on occurrence of a event."; }; @@ -144,13 +171,17 @@ in t.resumeCommand ]; - mkEvent = e: [ - e.event - e.command + mkEvent = event: command: [ + event + command ]; + nonemptyEvents = lib.filterAttrs (event: command: command != null) cfg.events; + args = - cfg.extraArgs ++ (lib.concatMap mkTimeout cfg.timeouts) ++ (lib.concatMap mkEvent cfg.events); + cfg.extraArgs + ++ (lib.concatMap mkTimeout cfg.timeouts) + ++ (lib.flatten (lib.mapAttrsToList mkEvent nonemptyEvents)); in "${lib.getExe cfg.package} ${lib.escapeShellArgs args}"; }; diff --git a/tests/modules/services/swayidle/basic-configuration.nix b/tests/modules/services/swayidle/basic-configuration.nix index b5534c75a..5da059b1d 100644 --- a/tests/modules/services/swayidle/basic-configuration.nix +++ b/tests/modules/services/swayidle/basic-configuration.nix @@ -19,16 +19,10 @@ resumeCommand = ''swaymsg "output * dpms on"''; } ]; - events = [ - { - event = "before-sleep"; - command = "swaylock -fF"; - } - { - event = "lock"; - command = "swaylock -fF"; - } - ]; + events = { + before-sleep = "swaylock -fF"; + lock = "swaylock -fF"; + }; }; nmt.script = '' diff --git a/tests/modules/services/swayidle/default.nix b/tests/modules/services/swayidle/default.nix index 62db035ba..75c88d284 100644 --- a/tests/modules/services/swayidle/default.nix +++ b/tests/modules/services/swayidle/default.nix @@ -2,4 +2,5 @@ lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { swayidle-basic-configuration = ./basic-configuration.nix; + swayidle-legacy-configuration = ./legacy-configuration.nix; } diff --git a/tests/modules/services/swayidle/legacy-configuration.nix b/tests/modules/services/swayidle/legacy-configuration.nix new file mode 100644 index 000000000..e07834fa7 --- /dev/null +++ b/tests/modules/services/swayidle/legacy-configuration.nix @@ -0,0 +1,51 @@ +{ config, ... }: +{ + services.swayidle = { + enable = true; + package = config.lib.test.mkStubPackage { outPath = "@swayidle@"; }; + events = [ + { + event = "lock"; + command = "swaylock -fF"; + } + { + event = "before-sleep"; + command = "swaylock -fF"; + } + ]; + }; + + test.asserts.evalWarnings.expected = [ + '' + 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. + '' + ]; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/swayidle.service + + assertFileExists "$serviceFile" + + serviceFileNormalized="$(normalizeStorePaths "$serviceFile")" + + assertFileContent "$serviceFileNormalized" ${builtins.toFile "expected.service" '' + [Install] + WantedBy=graphical-session.target + + [Service] + Environment=PATH=@bash-interactive@/bin + ExecStart=@swayidle@/bin/dummy -w before-sleep 'swaylock -fF' lock 'swaylock -fF' + Restart=always + Type=simple + + [Unit] + After=graphical-session.target + ConditionEnvironment=WAYLAND_DISPLAY + Description=Idle manager for Wayland + Documentation=man:swayidle(1) + PartOf=graphical-session.target + ''} + ''; +}