fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
4.7 KiB158 linesraw
1# Wrapper around xidlehook.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8  cfg = config.services.xidlehook;
9
10  notEmpty = list: filter (x: x != "" && x != null) (flatten list);
11
12  timers = let
13    toTimer = timer:
14      "--timer ${toString timer.delay} ${
15        escapeShellArgs [ timer.command timer.canceller ]
16      }";
17  in map toTimer (filter (timer: timer.command != null) cfg.timers);
18
19  script = pkgs.writeShellScript "xidlehook" ''
20    ${concatStringsSep "\n"
21    (mapAttrsToList (name: value: "export ${name}=${value}")
22      cfg.environment or { })}
23    ${concatStringsSep " " (notEmpty [
24      "${cfg.package}/bin/xidlehook"
25      (optionalString cfg.once "--once")
26      (optionalString cfg.detect-sleep "--detect-sleep")
27      (optionalString cfg.not-when-fullscreen "--not-when-fullscreen")
28      (optionalString cfg.not-when-audio "--not-when-audio")
29      timers
30    ])}
31  '';
32in {
33  meta.maintainers = [ maintainers.dschrempf hm.maintainers.bertof ];
34
35  options.services.xidlehook = {
36    enable = mkEnableOption "xidlehook systemd service";
37
38    package = mkOption {
39      type = types.package;
40      default = pkgs.xidlehook;
41      defaultText = "pkgs.xidlehook";
42      description = "The package to use for xidlehook.";
43    };
44
45    environment = mkOption {
46      type = types.attrsOf types.str;
47      default = { };
48      example = literalExpression ''
49        {
50          "primary-display" = "$(xrandr | awk '/ primary/{print $1}')";
51        }
52      '';
53      description = ''
54        Extra environment variables to be exported in the script.
55        These options are passed unescaped as `export name=value`.
56      '';
57    };
58
59    detect-sleep = mkEnableOption
60      "detecting when the system wakes up from a suspended state and resetting the idle timer";
61
62    not-when-fullscreen = mkOption {
63      type = types.bool;
64      default = false;
65      example = true;
66      description = "Disable locking when a fullscreen application is in use.";
67    };
68
69    not-when-audio = mkOption {
70      type = types.bool;
71      default = false;
72      example = true;
73      description = "Disable locking when audio is playing.";
74    };
75
76    once = mkEnableOption "running the program once and exiting";
77
78    timers = mkOption {
79      type = types.listOf (types.submodule {
80        options = {
81          delay = mkOption {
82            type = types.ints.unsigned;
83            example = 60;
84            description = "Time before executing the command.";
85          };
86          command = mkOption {
87            type = types.nullOr types.str;
88            example = literalExpression ''
89              ''${pkgs.libnotify}/bin/notify-send "Idle" "Sleeping in 1 minute"
90            '';
91            description = ''
92              Command executed after the idle timeout is reached.
93              Path to executables are accepted.
94              The command is automatically escaped.
95            '';
96          };
97          canceller = mkOption {
98            type = types.str;
99            default = "";
100            example = literalExpression ''
101              ''${pkgs.libnotify}/bin/notify-send "Idle" "Resuming activity"
102            '';
103            description = ''
104              Command executed when the user becomes active again.
105              This is only executed if the next timer has not been reached.
106              Path to executables are accepted.
107              The command is automatically escaped.
108            '';
109          };
110        };
111      });
112      default = [ ];
113      example = literalExpression ''
114        [
115          {
116            delay = 60;
117            command = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness .1";
118            canceller = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness 1";
119          }
120          {
121            delay = 120;
122            command = "''${pkgs.writeShellScript "my-script" '''
123              # A complex script to run
124            '''}";
125          }
126        ]
127      '';
128      description = ''
129        A set of commands to be executed after a specific idle timeout.
130        The commands specified in `command` and `canceller`
131        are passed escaped to the script.
132        To use or re-use environment variables that are script-dependent, specify them
133        in the `environment` section.
134      '';
135    };
136  };
137
138  config = mkIf cfg.enable {
139    assertions = [
140      (lib.hm.assertions.assertPlatform "services.xidlehook" pkgs
141        lib.platforms.linux)
142    ];
143
144    systemd.user.services.xidlehook = {
145      Unit = {
146        Description = "xidlehook service";
147        PartOf = [ "graphical-session.target" ];
148        After = [ "graphical-session.target" ];
149        ConditionEnvironment = [ "DISPLAY" ];
150      };
151      Service = {
152        Type = if cfg.once then "oneshot" else "simple";
153        ExecStart = "${script}";
154      };
155      Install.WantedBy = [ "graphical-session.target" ];
156    };
157  };
158}