home-manager/modules/services/xsuspender.nix
Emily 36a53d9f26 treewide: convert all option docs to Markdown
This process was automated by [my fork of `nix-doc-munge`]. All
conversions were automatically checked to produce the same DocBook
result when converted back, modulo minor typographical/formatting
differences on the acceptable-to-desirable spectrum.

To reproduce this commit, run:

  $ NIX_PATH=nixpkgs=flake:nixpkgs/e7e69199f0372364a6106a1e735f68604f4c5a25 \
    nix shell nixpkgs#coreutils \
    -c find . -name '*.nix' \
    -exec nix run -- github:emilazy/nix-doc-munge/98dadf1f77351c2ba5dcb709a2a171d655f15099 \
    {} +
  $ ./format

[my fork of `nix-doc-munge`]: https://github.com/emilazy/nix-doc-munge/tree/home-manager
2023-07-17 18:40:56 +01:00

203 lines
5.7 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.xsuspender;
iniFormat = pkgs.formats.ini { };
xsuspenderOptions = types.submodule {
options = {
matchWmClassContains = mkOption {
description = lib.mdDoc "Match windows that wm class contains string.";
type = types.nullOr types.str;
default = null;
};
matchWmClassGroupContains = mkOption {
description =
lib.mdDoc "Match windows where wm class group contains string.";
type = types.nullOr types.str;
default = null;
};
matchWmNameContains = mkOption {
description = lib.mdDoc "Match windows where wm name contains string.";
type = types.nullOr types.str;
default = null;
};
suspendDelay = mkOption {
description = lib.mdDoc "Initial suspend delay in seconds.";
type = types.int;
default = 5;
};
resumeEvery = mkOption {
description = lib.mdDoc "Resume interval in seconds.";
type = types.int;
default = 50;
};
resumeFor = mkOption {
description = lib.mdDoc "Resume duration in seconds.";
type = types.int;
default = 5;
};
execSuspend = mkOption {
description = lib.mdDoc ''
Before suspending, execute this shell script. If it fails,
abort suspension.
'';
type = types.nullOr types.str;
default = null;
example = ''echo "suspending window $XID of process $PID"'';
};
execResume = mkOption {
description = lib.mdDoc ''
Before resuming, execute this shell script. Resume the
process regardless script failure.
'';
type = types.nullOr types.str;
default = null;
example = "echo resuming ...";
};
sendSignals = mkOption {
description = lib.mdDoc ''
Whether to send SIGSTOP / SIGCONT signals or not.
If false just the exec scripts are run.
'';
type = types.bool;
default = true;
};
suspendSubtreePattern = mkOption {
description =
lib.mdDoc "Also suspend descendant processes that match this regex.";
type = types.nullOr types.str;
default = null;
};
onlyOnBattery = mkOption {
description =
lib.mdDoc "Whether to enable process suspend only on battery.";
type = types.bool;
default = false;
};
autoSuspendOnBattery = mkOption {
description = lib.mdDoc ''
Whether to auto-apply rules when switching to battery
power even if the window(s) didn't just lose focus.
'';
type = types.bool;
default = true;
};
downclockOnBattery = mkOption {
description = lib.mdDoc ''
Limit CPU consumption for this factor when on battery power.
Value 1 means 50% decrease, 2 means 66%, 3 means 75% etc.
'';
type = types.int;
default = 0;
};
};
};
in {
meta.maintainers = [ maintainers.offline ];
options = {
services.xsuspender = {
enable = mkEnableOption (lib.mdDoc "XSuspender");
defaults = mkOption {
description = lib.mdDoc "XSuspender defaults.";
type = xsuspenderOptions;
default = { };
};
rules = mkOption {
description = lib.mdDoc "Attribute set of XSuspender rules.";
type = types.attrsOf xsuspenderOptions;
default = { };
example = {
Chromium = {
suspendDelay = 10;
matchWmClassContains = "chromium-browser";
suspendSubtreePattern = "chromium";
};
};
};
debug = mkOption {
description = lib.mdDoc "Whether to enable debug output.";
type = types.bool;
default = false;
};
iniContent = mkOption {
type = iniFormat.type;
internal = true;
};
};
};
config = mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "services.xsuspender" pkgs
lib.platforms.linux)
];
services.xsuspender.iniContent = let
mkSection = values:
filterAttrs (_: v: v != null) {
match_wm_class_contains = values.matchWmClassContains;
match_wm_class_group_contains = values.matchWmClassGroupContains;
match_wm_name_contains = values.matchWmNameContains;
suspend_delay = values.suspendDelay;
resume_every = values.resumeEvery;
resume_for = values.resumeFor;
exec_suspend = values.execSuspend;
exec_resume = values.execResume;
send_signals = values.sendSignals;
suspend_subtree_pattern = values.suspendSubtreePattern;
only_on_battery = values.onlyOnBattery;
auto_suspend_on_battery = values.autoSuspendOnBattery;
downclock_on_battery = values.downclockOnBattery;
};
in {
Default = mkSection cfg.defaults;
} // mapAttrs (_: mkSection) cfg.rules;
# To make the xsuspender tool available.
home.packages = [ pkgs.xsuspender ];
xdg.configFile."xsuspender.conf".source =
iniFormat.generate "xsuspender.conf" cfg.iniContent;
systemd.user.services.xsuspender = {
Unit = {
Description = "XSuspender";
After = [ "graphical-session-pre.target" ];
PartOf = [ "graphical-session.target" ];
X-Restart-Triggers =
[ "${config.xdg.configFile."xsuspender.conf".source}" ];
};
Service = {
ExecStart = "${pkgs.xsuspender}/bin/xsuspender";
Environment = mkIf cfg.debug [ "G_MESSAGES_DEBUG=all" ];
};
Install = { WantedBy = [ "graphical-session.target" ]; };
};
};
}