1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8with lib;
9
10let
11 cfg = config.services.xserver.xautolock;
12in
13{
14 options = {
15 services.xserver.xautolock = {
16 enable = mkEnableOption "xautolock";
17 enableNotifier = mkEnableOption "xautolock.notify" // {
18 description = ''
19 Whether to enable the notifier feature of xautolock.
20 This publishes a notification before the autolock.
21 '';
22 };
23
24 time = mkOption {
25 default = 15;
26 type = types.int;
27
28 description = ''
29 Idle time (in minutes) to wait until xautolock locks the computer.
30 '';
31 };
32
33 locker = mkOption {
34 default = "${pkgs.xlockmore}/bin/xlock"; # default according to `man xautolock`
35 defaultText = literalExpression ''"''${pkgs.xlockmore}/bin/xlock"'';
36 example = literalExpression ''"''${pkgs.i3lock}/bin/i3lock -i /path/to/img"'';
37 type = types.str;
38
39 description = ''
40 The script to use when automatically locking the computer.
41 '';
42 };
43
44 nowlocker = mkOption {
45 default = null;
46 example = literalExpression ''"''${pkgs.i3lock}/bin/i3lock -i /path/to/img"'';
47 type = types.nullOr types.str;
48
49 description = ''
50 The script to use when manually locking the computer with {command}`xautolock -locknow`.
51 '';
52 };
53
54 notify = mkOption {
55 default = 10;
56 type = types.int;
57
58 description = ''
59 Time (in seconds) before the actual lock when the notification about the pending lock should be published.
60 '';
61 };
62
63 notifier = mkOption {
64 default = null;
65 example = literalExpression ''"''${pkgs.libnotify}/bin/notify-send 'Locking in 10 seconds'"'';
66 type = types.nullOr types.str;
67
68 description = ''
69 Notification script to be used to warn about the pending autolock.
70 '';
71 };
72
73 killer = mkOption {
74 default = null; # default according to `man xautolock` is none
75 example = "/run/current-system/systemd/bin/systemctl suspend";
76 type = types.nullOr types.str;
77
78 description = ''
79 The script to use when nothing has happened for as long as {option}`killtime`
80 '';
81 };
82
83 killtime = mkOption {
84 default = 20; # default according to `man xautolock`
85 type = types.int;
86
87 description = ''
88 Minutes xautolock waits until it executes the script specified in {option}`killer`
89 (Has to be at least 10 minutes)
90 '';
91 };
92
93 extraOptions = mkOption {
94 type = types.listOf types.str;
95 default = [ ];
96 example = [ "-detectsleep" ];
97 description = ''
98 Additional command-line arguments to pass to
99 {command}`xautolock`.
100 '';
101 };
102 };
103 };
104
105 config = mkIf cfg.enable {
106 environment.systemPackages = with pkgs; [ xautolock ];
107 systemd.user.services.xautolock = {
108 description = "xautolock service";
109 wantedBy = [ "graphical-session.target" ];
110 partOf = [ "graphical-session.target" ];
111 serviceConfig = with lib; {
112 ExecStart = strings.concatStringsSep " " (
113 [
114 "${pkgs.xautolock}/bin/xautolock"
115 "-noclose"
116 "-time ${toString cfg.time}"
117 "-locker '${cfg.locker}'"
118 ]
119 ++ optionals cfg.enableNotifier [
120 "-notify ${toString cfg.notify}"
121 "-notifier '${cfg.notifier}'"
122 ]
123 ++ optionals (cfg.nowlocker != null) [
124 "-nowlocker '${cfg.nowlocker}'"
125 ]
126 ++ optionals (cfg.killer != null) [
127 "-killer '${cfg.killer}'"
128 "-killtime ${toString cfg.killtime}"
129 ]
130 ++ cfg.extraOptions
131 );
132 Restart = "always";
133 };
134 };
135 assertions = [
136 {
137 assertion = cfg.enableNotifier -> cfg.notifier != null;
138 message = "When enabling the notifier for xautolock, you also need to specify the notify script";
139 }
140 {
141 assertion = cfg.killer != null -> cfg.killtime >= 10;
142 message = "killtime has to be at least 10 minutes according to `man xautolock`";
143 }
144 ]
145 ++ (lib.forEach [ "locker" "notifier" "nowlocker" "killer" ] (option: {
146 assertion = cfg.${option} != null -> builtins.substring 0 1 cfg.${option} == "/";
147 message = "Please specify a canonical path for `services.xserver.xautolock.${option}`";
148 }));
149 };
150}