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