fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
4.7 KiB160 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.screen-locker;
8
9in {
10  meta.maintainers = [ hm.maintainers.jrobsonchase hm.maintainers.rszamszur ];
11
12  imports = let
13    origOpt = name: [ "services" "screen-locker" name ];
14    xautolockOpt = name: [ "services" "screen-locker" "xautolock" name ];
15    xssLockOpt = name: [ "services" "screen-locker" "xss-lock" name ];
16  in [
17    (mkRenamedOptionModule (origOpt "xssLockExtraOptions")
18      (xssLockOpt "extraOptions"))
19    (mkRenamedOptionModule (origOpt "xautolockExtraOptions")
20      (xautolockOpt "extraOptions"))
21    (mkRenamedOptionModule (origOpt "enableDetectSleep")
22      (xautolockOpt "detectSleep"))
23  ];
24
25  options.services.screen-locker = {
26    enable = mkEnableOption "screen locker for X session";
27
28    lockCmd = mkOption {
29      type = types.str;
30      description = "Locker command to run.";
31      example = "\${pkgs.i3lock}/bin/i3lock -n -c 000000";
32    };
33
34    inactiveInterval = mkOption {
35      type = types.int;
36      default = 10;
37      description = ''
38        Inactive time interval in minutes after which session will be locked.
39        The minimum is 1 minute, and the maximum is 1 hour.
40        If {option}`xautolock.enable` is true, it will use this setting.
41        See <https://linux.die.net/man/1/xautolock>.
42        Otherwise, this will be used with {command}`xset` to configure
43        the X server's screensaver timeout.
44      '';
45    };
46
47    xautolock = {
48      enable = mkOption {
49        type = types.bool;
50        default = true;
51        description = "Use xautolock for time-based locking.";
52      };
53
54      package = mkOption {
55        type = types.package;
56        default = pkgs.xautolock;
57        description = ''
58          Package providing the {command}`xautolock` binary.
59        '';
60      };
61
62      detectSleep = mkOption {
63        type = types.bool;
64        default = true;
65        description = ''
66          Whether to reset xautolock timers when awaking from sleep.
67          No effect if {option}`xautolock.enable` is false.
68        '';
69      };
70
71      extraOptions = mkOption {
72        type = types.listOf types.str;
73        default = [ ];
74        description = ''
75          Extra command-line arguments to pass to {command}`xautolock`.
76          No effect if {option}`xautolock.enable` is false.
77        '';
78      };
79    };
80
81    xss-lock = {
82      package = mkOption {
83        type = types.package;
84        default = pkgs.xss-lock;
85        description = ''
86          Package providing the {command}`xss-lock` binary.
87        '';
88      };
89
90      extraOptions = mkOption {
91        type = types.listOf types.str;
92        default = [ ];
93        description = ''
94          Extra command-line arguments to pass to {command}`xss-lock`.
95        '';
96      };
97
98      screensaverCycle = mkOption {
99        type = types.int;
100        default = 600;
101        description = ''
102          The X server's screensaver cycle value expressed as seconds.
103          This will be used with {command}`xset` to configure
104          the cycle along with timeout.
105        '';
106      };
107    };
108  };
109
110  config = mkIf cfg.enable (mkMerge [
111    {
112      assertions = [
113        (lib.hm.assertions.assertPlatform "services.screen-locker" pkgs
114          lib.platforms.linux)
115      ];
116
117      systemd.user.services.xss-lock = {
118        Unit = {
119          Description = "xss-lock, session locker service";
120          After = [ "graphical-session-pre.target" ];
121          PartOf = [ "graphical-session.target" ];
122        };
123
124        Install = { WantedBy = [ "graphical-session.target" ]; };
125
126        Service = {
127          ExecStart = concatStringsSep " "
128            ([ "${cfg.xss-lock.package}/bin/xss-lock" "-s \${XDG_SESSION_ID}" ]
129              ++ cfg.xss-lock.extraOptions ++ [ "-- ${cfg.lockCmd}" ]);
130        };
131      };
132    }
133    (mkIf (!cfg.xautolock.enable) {
134      systemd.user.services.xss-lock.Service.ExecStartPre =
135        "${pkgs.xorg.xset}/bin/xset s ${toString (cfg.inactiveInterval * 60)} ${
136          toString cfg.xss-lock.screensaverCycle
137        }";
138    })
139    (mkIf cfg.xautolock.enable {
140      systemd.user.services.xautolock-session = {
141        Unit = {
142          Description = "xautolock, session locker service";
143          After = [ "graphical-session-pre.target" ];
144          PartOf = [ "graphical-session.target" ];
145        };
146
147        Install = { WantedBy = [ "graphical-session.target" ]; };
148
149        Service = {
150          ExecStart = concatStringsSep " " ([
151            "${cfg.xautolock.package}/bin/xautolock"
152            "-time ${toString cfg.inactiveInterval}"
153            "-locker '${pkgs.systemd}/bin/loginctl lock-session \${XDG_SESSION_ID}'"
154          ] ++ optional cfg.xautolock.detectSleep "-detectsleep"
155            ++ cfg.xautolock.extraOptions);
156        };
157      };
158    })
159  ]);
160}