summary refs log tree commit diff
path: root/nixos/modules/services/x11/xautolock.nix
blob: 947d8f4edfb59765372c18078eeb9502d6b5cc12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.xserver.xautolock;
in
  {
    options = {
      services.xserver.xautolock = {
        enable = mkEnableOption "xautolock";
        enableNotifier = mkEnableOption "xautolock.notify" // {
          description = ''
            Whether to enable the notifier feature of xautolock.
            This publishes a notification before the autolock.
          '';
        };

        time = mkOption {
          default = 15;
          type = types.int;

          description = ''
            Idle time (in minutes) to wait until xautolock locks the computer.
          '';
        };

        locker = mkOption {
          default = "${pkgs.xlockmore}/bin/xlock"; # default according to `man xautolock`
          defaultText = literalExpression ''"''${pkgs.xlockmore}/bin/xlock"'';
          example = literalExpression ''"''${pkgs.i3lock}/bin/i3lock -i /path/to/img"'';
          type = types.str;

          description = ''
            The script to use when automatically locking the computer.
          '';
        };

        nowlocker = mkOption {
          default = null;
          example = literalExpression ''"''${pkgs.i3lock}/bin/i3lock -i /path/to/img"'';
          type = types.nullOr types.str;

          description = ''
            The script to use when manually locking the computer with <command>xautolock -locknow</command>.
          '';
        };

        notify = mkOption {
          default = 10;
          type = types.int;

          description = ''
            Time (in seconds) before the actual lock when the notification about the pending lock should be published.
          '';
        };

        notifier = mkOption {
          default = null;
          example = literalExpression ''"''${pkgs.libnotify}/bin/notify-send 'Locking in 10 seconds'"'';
          type = types.nullOr types.str;

          description = ''
            Notification script to be used to warn about the pending autolock.
          '';
        };

        killer = mkOption {
          default = null; # default according to `man xautolock` is none
          example = "/run/current-system/systemd/bin/systemctl suspend";
          type = types.nullOr types.str;

          description = ''
            The script to use when nothing has happend for as long as <option>killtime</option>
          '';
        };

        killtime = mkOption {
          default = 20; # default according to `man xautolock`
          type = types.int;

          description = ''
            Minutes xautolock waits until it executes the script specified in <option>killer</option>
            (Has to be at least 10 minutes)
          '';
        };

        extraOptions = mkOption {
          type = types.listOf types.str;
          default = [ ];
          example = [ "-detectsleep" ];
          description = ''
            Additional command-line arguments to pass to
            <command>xautolock</command>.
          '';
        };
      };
    };

    config = mkIf cfg.enable {
      environment.systemPackages = with pkgs; [ xautolock ];
      systemd.user.services.xautolock = {
        description = "xautolock service";
        wantedBy = [ "graphical-session.target" ];
        partOf = [ "graphical-session.target" ];
        serviceConfig = with lib; {
          ExecStart = strings.concatStringsSep " " ([
            "${pkgs.xautolock}/bin/xautolock"
            "-noclose"
            "-time ${toString cfg.time}"
            "-locker '${cfg.locker}'"
          ] ++ optionals cfg.enableNotifier [
            "-notify ${toString cfg.notify}"
            "-notifier '${cfg.notifier}'"
          ] ++ optionals (cfg.nowlocker != null) [
            "-nowlocker '${cfg.nowlocker}'"
          ] ++ optionals (cfg.killer != null) [
            "-killer '${cfg.killer}'"
            "-killtime ${toString cfg.killtime}"
          ] ++ cfg.extraOptions);
          Restart = "always";
        };
      };
      assertions = [
        {
          assertion = cfg.enableNotifier -> cfg.notifier != null;
          message = "When enabling the notifier for xautolock, you also need to specify the notify script";
        }
        {
          assertion = cfg.killer != null -> cfg.killtime >= 10;
          message = "killtime has to be at least 10 minutes according to `man xautolock`";
        }
      ] ++ (lib.forEach [ "locker" "notifier" "nowlocker" "killer" ]
        (option:
        {
          assertion = cfg.${option} != null -> builtins.substring 0 1 cfg.${option} == "/";
          message = "Please specify a canonical path for `services.xserver.xautolock.${option}`";
        })
      );
    };
  }