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