1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 11:36:05 +01:00
home-manager/modules/services/xidlehook.nix
Austin Horstman 7419250703
treewide: convert package options to use mkPackageOption (#7116)
This commit converts `package = mkOption` declarations throughout the
codebase to use the more modern and consistent `lib.mkPackageOption`
function.

Key changes:
- Simple package options: `mkOption { type = types.package; default = pkgs.foo; }`
  becomes `lib.mkPackageOption pkgs "foo" { }`
- Package set options: Uses correct package set as first argument with
  `pkgsText` parameter (e.g., `lib.mkPackageOption pkgs.vimPlugins "plugin" { pkgsText = "pkgs.vimPlugins"; }`)
- Removes redundant descriptions that just restate the package name
- Preserves examples and extra context where meaningful
- Handles submodule plugin options properly with `null` defaults

This modernizes the option declarations and makes them more consistent
with current nixpkgs patterns while maintaining full backward compatibility.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
2025-05-23 00:42:38 -05:00

174 lines
4.9 KiB
Nix

# Wrapper around xidlehook.
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
literalExpression
mkEnableOption
mkOption
optionalString
types
;
cfg = config.services.xidlehook;
notEmpty = list: lib.filter (x: x != "" && x != null) (lib.flatten list);
timers =
let
toTimer =
timer:
"--timer ${toString timer.delay} ${
lib.escapeShellArgs [
timer.command
timer.canceller
]
}";
in
map toTimer (lib.filter (timer: timer.command != null) cfg.timers);
script = pkgs.writeShellScript "xidlehook" ''
${lib.concatStringsSep "\n" (
lib.mapAttrsToList (name: value: "export ${name}=${value}") cfg.environment or { }
)}
${lib.concatStringsSep " " (notEmpty [
"${cfg.package}/bin/xidlehook"
(optionalString cfg.once "--once")
(optionalString cfg.detect-sleep "--detect-sleep")
(optionalString cfg.not-when-fullscreen "--not-when-fullscreen")
(optionalString cfg.not-when-audio "--not-when-audio")
timers
])}
'';
in
{
meta.maintainers = [
lib.maintainers.dschrempf
lib.hm.maintainers.bertof
];
options.services.xidlehook = {
enable = mkEnableOption "xidlehook systemd service";
package = lib.mkPackageOption pkgs "xidlehook" { };
environment = mkOption {
type = types.attrsOf types.str;
default = { };
example = literalExpression ''
{
"primary-display" = "$(xrandr | awk '/ primary/{print $1}')";
}
'';
description = ''
Extra environment variables to be exported in the script.
These options are passed unescaped as `export name=value`.
'';
};
detect-sleep = mkEnableOption "detecting when the system wakes up from a suspended state and resetting the idle timer";
not-when-fullscreen = mkOption {
type = types.bool;
default = false;
example = true;
description = "Disable locking when a fullscreen application is in use.";
};
not-when-audio = mkOption {
type = types.bool;
default = false;
example = true;
description = "Disable locking when audio is playing.";
};
once = mkEnableOption "running the program once and exiting";
timers = mkOption {
type = types.listOf (
types.submodule {
options = {
delay = mkOption {
type = types.ints.unsigned;
example = 60;
description = "Time before executing the command.";
};
command = mkOption {
type = types.nullOr types.str;
example = literalExpression ''
''${pkgs.libnotify}/bin/notify-send "Idle" "Sleeping in 1 minute"
'';
description = ''
Command executed after the idle timeout is reached.
Path to executables are accepted.
The command is automatically escaped.
'';
};
canceller = mkOption {
type = types.str;
default = "";
example = literalExpression ''
''${pkgs.libnotify}/bin/notify-send "Idle" "Resuming activity"
'';
description = ''
Command executed when the user becomes active again.
This is only executed if the next timer has not been reached.
Path to executables are accepted.
The command is automatically escaped.
'';
};
};
}
);
default = [ ];
example = literalExpression ''
[
{
delay = 60;
command = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness .1";
canceller = "xrandr --output \"$PRIMARY_DISPLAY\" --brightness 1";
}
{
delay = 120;
command = "''${pkgs.writeShellScript "my-script" '''
# A complex script to run
'''}";
}
]
'';
description = ''
A set of commands to be executed after a specific idle timeout.
The commands specified in `command` and `canceller`
are passed escaped to the script.
To use or re-use environment variables that are script-dependent, specify them
in the `environment` section.
'';
};
};
config = lib.mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "services.xidlehook" pkgs lib.platforms.linux)
];
systemd.user.services.xidlehook = {
Unit = {
Description = "xidlehook service";
PartOf = [ "graphical-session.target" ];
After = [ "graphical-session.target" ];
ConditionEnvironment = [ "DISPLAY" ];
};
Service = {
Type = if cfg.once then "oneshot" else "simple";
ExecStart = "${script}";
} // lib.optionalAttrs (!cfg.once) { Restart = "always"; };
Install.WantedBy = [ "graphical-session.target" ];
};
};
}