2021-06-07 22:13:43 +02:00
|
|
|
# Wrapper around xidlehook.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.xidlehook;
|
|
|
|
|
|
|
|
notEmpty = list: filter (x: x != "" && x != null) (flatten list);
|
|
|
|
|
|
|
|
timers = let
|
|
|
|
toTimer = timer:
|
|
|
|
"--timer ${toString timer.delay} ${
|
|
|
|
escapeShellArgs [ timer.command timer.canceller ]
|
|
|
|
}";
|
|
|
|
in map toTimer (filter (timer: timer.command != null) cfg.timers);
|
|
|
|
|
|
|
|
script = pkgs.writeShellScript "xidlehook" ''
|
|
|
|
${concatStringsSep "\n"
|
|
|
|
(mapAttrsToList (name: value: "export ${name}=${value}")
|
|
|
|
cfg.environment or { })}
|
|
|
|
${concatStringsSep " " (notEmpty [
|
|
|
|
"${cfg.package}/bin/xidlehook"
|
|
|
|
(optionalString cfg.once "--once")
|
2022-08-24 21:56:45 +02:00
|
|
|
(optionalString cfg.detect-sleep "--detect-sleep")
|
2021-06-07 22:13:43 +02:00
|
|
|
(optionalString cfg.not-when-fullscreen "--not-when-fullscreen")
|
|
|
|
(optionalString cfg.not-when-audio "--not-when-audio")
|
|
|
|
timers
|
|
|
|
])}
|
|
|
|
'';
|
|
|
|
in {
|
2022-08-24 21:56:45 +02:00
|
|
|
meta.maintainers = [ maintainers.dschrempf hm.maintainers.bertof ];
|
2021-06-07 22:13:43 +02:00
|
|
|
|
|
|
|
options.services.xidlehook = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "xidlehook systemd service";
|
2021-06-07 22:13:43 +02:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.xidlehook;
|
|
|
|
defaultText = "pkgs.xidlehook";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "The package to use for xidlehook.";
|
2021-06-07 22:13:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
environment = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = { };
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-06-07 22:13:43 +02:00
|
|
|
{
|
|
|
|
"primary-display" = "$(xrandr | awk '/ primary/{print $1}')";
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-06-07 22:13:43 +02:00
|
|
|
Extra environment variables to be exported in the script.
|
2023-07-01 01:30:13 +02:00
|
|
|
These options are passed unescaped as `export name=value`.
|
2021-06-07 22:13:43 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
detect-sleep = mkEnableOption
|
|
|
|
"detecting when the system wakes up from a suspended state and resetting the idle timer";
|
2022-08-24 21:56:45 +02:00
|
|
|
|
2021-06-07 22:13:43 +02:00
|
|
|
not-when-fullscreen = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Disable locking when a fullscreen application is in use.";
|
2021-06-07 22:13:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
not-when-audio = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Disable locking when audio is playing.";
|
2021-06-07 22:13:43 +02:00
|
|
|
};
|
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
once = mkEnableOption "running the program once and exiting";
|
2021-06-07 22:13:43 +02:00
|
|
|
|
|
|
|
timers = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
delay = mkOption {
|
|
|
|
type = types.ints.unsigned;
|
|
|
|
example = 60;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Time before executing the command.";
|
2021-06-07 22:13:43 +02:00
|
|
|
};
|
|
|
|
command = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-06-07 22:13:43 +02:00
|
|
|
''${pkgs.libnotify}/bin/notify-send "Idle" "Sleeping in 1 minute"
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-06-07 22:13:43 +02:00
|
|
|
Command executed after the idle timeout is reached.
|
|
|
|
Path to executables are accepted.
|
|
|
|
The command is automatically escaped.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
canceller = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-06-07 22:13:43 +02:00
|
|
|
''${pkgs.libnotify}/bin/notify-send "Idle" "Resuming activity"
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-06-07 22:13:43 +02:00
|
|
|
Command executed when the user becomes active again.
|
|
|
|
This is only executed if the next timer has not been reached.
|
|
|
|
Path to executables are accepted.
|
|
|
|
The command is automatically escaped.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = [ ];
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-06-07 22:13:43 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
delay = 60;
|
|
|
|
command = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness .1";
|
|
|
|
canceller = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness 1";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
delay = 120;
|
|
|
|
command = "''${pkgs.writeShellScript "my-script" '''
|
|
|
|
# A complex script to run
|
|
|
|
'''}";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-06-07 22:13:43 +02:00
|
|
|
A set of commands to be executed after a specific idle timeout.
|
2023-07-01 01:30:13 +02:00
|
|
|
The commands specified in `command` and `canceller`
|
2021-06-07 22:13:43 +02:00
|
|
|
are passed escaped to the script.
|
|
|
|
To use or re-use environment variables that are script-dependent, specify them
|
2023-07-01 01:30:13 +02:00
|
|
|
in the `environment` section.
|
2021-06-07 22:13:43 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2021-07-07 23:24:27 +02:00
|
|
|
assertions = [
|
|
|
|
(lib.hm.assertions.assertPlatform "services.xidlehook" pkgs
|
|
|
|
lib.platforms.linux)
|
|
|
|
];
|
|
|
|
|
2021-06-07 22:13:43 +02:00
|
|
|
systemd.user.services.xidlehook = {
|
|
|
|
Unit = {
|
|
|
|
Description = "xidlehook service";
|
|
|
|
PartOf = [ "graphical-session.target" ];
|
|
|
|
After = [ "graphical-session.target" ];
|
|
|
|
ConditionEnvironment = [ "DISPLAY" ];
|
|
|
|
};
|
|
|
|
Service = {
|
|
|
|
Type = if cfg.once then "oneshot" else "simple";
|
|
|
|
ExecStart = "${script}";
|
|
|
|
};
|
|
|
|
Install.WantedBy = [ "graphical-session.target" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|