fork/home-manager
git clone https://git.julia.blue/fork/home-manager
6d95d98b
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . xsuspender ; 8 9iniFormat = pkgs . formats . ini { }; 10 11xsuspenderOptions = types . submodule { 12options = { 13matchWmClassContains = mkOption { 14description = "Match windows that wm class contains string." ; 15type = types . nullOr types . str ; 16default = null ; 17}; 18 19matchWmClassGroupContains = mkOption { 20description = "Match windows where wm class group contains string." ; 21type = types . nullOr types . str ; 22default = null ; 23}; 24 25matchWmNameContains = mkOption { 26description = "Match windows where wm name contains string." ; 27type = types . nullOr types . str ; 28default = null ; 29}; 30 31suspendDelay = mkOption { 32description = "Initial suspend delay in seconds." ; 33type = types . int ; 34default = 5 ; 35}; 36 37resumeEvery = mkOption { 38description = "Resume interval in seconds." ; 39type = types . int ; 40default = 50 ; 41}; 42 43resumeFor = mkOption { 44description = "Resume duration in seconds." ; 45type = types . int ; 46default = 5 ; 47}; 48 49execSuspend = mkOption { 50description = '' 51Before suspending, execute this shell script. If it fails, 52abort suspension. 53'' ; 54type = types . nullOr types . str ; 55default = null ; 56example = ''echo "suspending window $XID of process $PID"'' ; 57}; 58 59execResume = mkOption { 60description = '' 61Before resuming, execute this shell script. Resume the 62process regardless script failure. 63'' ; 64type = types . nullOr types . str ; 65default = null ; 66example = "echo resuming ..." ; 67}; 68 69sendSignals = mkOption { 70description = '' 71Whether to send SIGSTOP / SIGCONT signals or not. 72If false just the exec scripts are run. 73'' ; 74type = types . bool ; 75default = true ; 76}; 77 78suspendSubtreePattern = mkOption { 79description = 80"Also suspend descendant processes that match this regex." ; 81type = types . nullOr types . str ; 82default = null ; 83}; 84 85onlyOnBattery = mkOption { 86description = "Whether to enable process suspend only on battery." ; 87type = types . bool ; 88default = false ; 89}; 90 91autoSuspendOnBattery = mkOption { 92description = '' 93Whether to auto-apply rules when switching to battery 94power even if the window(s) didn't just lose focus. 95'' ; 96type = types . bool ; 97default = true ; 98}; 99 100downclockOnBattery = mkOption { 101description = '' 102Limit CPU consumption for this factor when on battery power. 103Value 1 means 50% decrease, 2 means 66%, 3 means 75% etc. 104'' ; 105type = types . int ; 106default = 0 ; 107}; 108}; 109}; 110 111in { 112meta . maintainers = [ maintainers . offline ]; 113 114options = { 115services . xsuspender = { 116enable = mkEnableOption "XSuspender" ; 117 118defaults = mkOption { 119description = "XSuspender defaults." ; 120type = xsuspenderOptions ; 121default = { }; 122}; 123 124rules = mkOption { 125description = "Attribute set of XSuspender rules." ; 126type = types . attrsOf xsuspenderOptions ; 127default = { }; 128example = { 129Chromium = { 130suspendDelay = 10 ; 131matchWmClassContains = "chromium-browser" ; 132suspendSubtreePattern = "chromium" ; 133}; 134}; 135}; 136 137debug = mkOption { 138description = "Whether to enable debug output." ; 139type = types . bool ; 140default = false ; 141}; 142 143iniContent = mkOption { 144type = iniFormat . type ; 145internal = true ; 146}; 147}; 148}; 149 150config = mkIf cfg . enable { 151assertions = [ 152( lib . hm . assertions . assertPlatform "services.xsuspender" pkgs 153lib . platforms . linux ) 154]; 155 156services . xsuspender . iniContent = let 157mkSection = values : 158filterAttrs ( _ :v :v != null ) { 159match_wm_class_contains = values . matchWmClassContains ; 160match_wm_class_group_contains = values . matchWmClassGroupContains ; 161match_wm_name_contains = values . matchWmNameContains ; 162suspend_delay = values . suspendDelay ; 163resume_every = values . resumeEvery ; 164resume_for = values . resumeFor ; 165exec_suspend = values . execSuspend ; 166exec_resume = values . execResume ; 167send_signals = values . sendSignals ; 168suspend_subtree_pattern = values . suspendSubtreePattern ; 169only_on_battery = values . onlyOnBattery ; 170auto_suspend_on_battery = values . autoSuspendOnBattery ; 171downclock_on_battery = values . downclockOnBattery ; 172}; 173in { 174Default = mkSection cfg . defaults ; 175} // mapAttrs ( _ :mkSection ) cfg . rules ; 176 177# To make the xsuspender tool available. 178home . packages = [ pkgs . xsuspender ]; 179 180xdg . configFile . "xsuspender.conf" . source = 181iniFormat . generate "xsuspender.conf" cfg . iniContent ; 182 183systemd . user . services . xsuspender = { 184Unit = { 185Description = "XSuspender" ; 186After = [ "graphical-session-pre.target" ]; 187PartOf = [ "graphical-session.target" ]; 188X-Restart-Triggers = 189[ " ${ config . xdg . configFile . "xsuspender.conf" . source } " ]; 190}; 191 192Service = { 193ExecStart = " ${ pkgs . xsuspender } /bin/xsuspender" ; 194Environment = mkIf cfg . debug [ "G_MESSAGES_DEBUG=all" ]; 195}; 196 197Install = { WantedBy = [ "graphical-session.target" ]; }; 198}; 199}; 200}