1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-29 13:19:47 +01:00
home-manager/modules/services/swayidle.nix

137 lines
3.5 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.swayidle;
mkTimeout = t:
[ "timeout" (toString t.timeout) (escapeShellArg t.command) ]
++ optionals (t.resumeCommand != null) [
"resume"
(escapeShellArg t.resumeCommand)
];
mkEvent = e: [ e.event (escapeShellArg e.command) ];
args = cfg.extraArgs ++ (concatMap mkTimeout cfg.timeouts)
++ (concatMap mkEvent cfg.events);
in {
meta.maintainers = [ maintainers.c0deaddict ];
options.services.swayidle = let
timeoutModule = { ... }: {
options = {
timeout = mkOption {
type = types.ints.positive;
example = 60;
2022-01-06 09:22:42 +01:00
description = "Timeout in seconds.";
};
command = mkOption {
type = types.str;
2022-01-06 09:22:42 +01:00
description = "Command to run after timeout seconds of inactivity.";
};
resumeCommand = mkOption {
type = with types; nullOr str;
default = null;
2022-01-06 09:22:42 +01:00
description = "Command to run when there is activity again.";
};
};
};
eventModule = { ... }: {
options = {
event = mkOption {
type = types.enum [ "before-sleep" "after-resume" "lock" "unlock" ];
2022-01-06 09:22:42 +01:00
description = "Event name.";
};
command = mkOption {
type = types.str;
2022-01-06 09:22:42 +01:00
description = "Command to run when event occurs.";
};
};
};
in {
2022-01-06 09:22:42 +01:00
enable = mkEnableOption "idle manager for Wayland";
package = mkOption {
type = types.package;
default = pkgs.swayidle;
defaultText = literalExpression "pkgs.swayidle";
description = "Swayidle package to install.";
};
timeouts = mkOption {
type = with types; listOf (submodule timeoutModule);
default = [ ];
2022-01-06 09:22:42 +01:00
example = literalExpression ''
[
{ timeout = 60; command = "${pkgs.swaylock}/bin/swaylock -fF"; }
{ timeout = 90; command = "${pkgs.systemd}/bin/systemctl suspend"; }
]
'';
2022-01-06 09:22:42 +01:00
description = "List of commands to run after idle timeout.";
};
events = mkOption {
type = with types; listOf (submodule eventModule);
default = [ ];
2022-01-06 09:22:42 +01:00
example = literalExpression ''
[
{ event = "before-sleep"; command = "${pkgs.swaylock}/bin/swaylock -fF"; }
{ event = "lock"; command = "lock"; }
]
'';
2022-12-03 05:20:00 +01:00
description = "Run command on occurrence of a event.";
};
extraArgs = mkOption {
type = with types; listOf str;
default = [ ];
2022-01-06 09:22:42 +01:00
description = "Extra arguments to pass to swayidle.";
};
systemdTarget = mkOption {
type = types.str;
default = "graphical-session.target";
example = "sway-session.target";
description = ''
Systemd target to bind to.
'';
};
};
config = mkIf cfg.enable {
2022-04-24 16:25:54 +02:00
assertions = [
(hm.assertions.assertPlatform "services.swayidle" pkgs platforms.linux)
];
systemd.user.services.swayidle = {
Unit = {
Description = "Idle manager for Wayland";
Documentation = "man:swayidle(1)";
PartOf = [ "graphical-session.target" ];
};
Service = {
Type = "simple";
Restart = "always";
# swayidle executes commands using "sh -c", so the PATH needs to contain a shell.
Environment = [ "PATH=${makeBinPath [ pkgs.bash ]}" ];
ExecStart =
"${cfg.package}/bin/swayidle -w ${concatStringsSep " " args}";
};
Install = { WantedBy = [ cfg.systemdTarget ]; };
};
};
}