From fdaaf543bad047639ef0b356ea2e6caec2f1215c Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Sun, 5 May 2024 04:13:18 -0500 Subject: [PATCH] hypridle: add module --- modules/misc/news.nix | 11 +++ modules/modules.nix | 1 + modules/services/hypridle.nix | 95 +++++++++++++++++++ tests/default.nix | 1 + .../services/hypridle/basic-configuration.nix | 38 ++++++++ tests/modules/services/hypridle/default.nix | 1 + tests/modules/services/hypridle/hypridle.conf | 16 ++++ 7 files changed, 163 insertions(+) create mode 100644 modules/services/hypridle.nix create mode 100644 tests/modules/services/hypridle/basic-configuration.nix create mode 100644 tests/modules/services/hypridle/default.nix create mode 100644 tests/modules/services/hypridle/hypridle.conf diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 92d3e9f5..ef3b98f5 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1584,6 +1584,17 @@ in { https://conky.cc/ for more. ''; } + + { + time = "2024-05-05T07:22:01+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'services.hypridle'. + + Hypridle is a program that monitors user activity and runs commands + when idle or active. See https://github.com/hyprwm/hypridle for more. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 7c81102a..b59812b7 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -302,6 +302,7 @@ let ./services/gromit-mpx.nix ./services/home-manager-auto-upgrade.nix ./services/hound.nix + ./services/hypridle.nix ./services/imapnotify.nix ./services/kanshi.nix ./services/kbfs.nix diff --git a/modules/services/hypridle.nix b/modules/services/hypridle.nix new file mode 100644 index 00000000..0e28c543 --- /dev/null +++ b/modules/services/hypridle.nix @@ -0,0 +1,95 @@ +{ config, lib, pkgs, ... }: +with lib; +let + + cfg = config.services.hypridle; +in { + meta.maintainers = [ maintainers.khaneliman maintainers.fufexan ]; + + options.services.hypridle = { + enable = mkEnableOption "Hypridle, Hyprland's idle daemon"; + + package = mkPackageOption pkgs "hypridle" { }; + + settings = lib.mkOption { + type = with lib.types; + let + valueType = nullOr (oneOf [ + bool + int + float + str + path + (attrsOf valueType) + (listOf valueType) + ]) // { + description = "Hypridle configuration value"; + }; + in valueType; + default = { }; + description = '' + Hypridle configuration written in Nix. Entries with the same key + should be written as lists. Variables' and colors' names should be + quoted. See for more examples. + ''; + example = lib.literalExpression '' + { + general = { + after_sleep_cmd = "hyprctl dispatch dpms on"; + ignore_dbus_inhibit = false; + lock_cmd = "hyprlock"; + }; + + listener = [ + { + timeout = 900; + on-timeout = "hyprlock"; + } + { + timeout = 1200; + on-timeout = "hyprctl dispatch dpms off"; + on-resume = "hyprctl dispatch dpms on"; + } + ]; + } + ''; + }; + + importantPrefixes = lib.mkOption { + type = with lib.types; listOf str; + default = [ "$" ]; + example = [ "$" ]; + description = '' + List of prefix of attributes to source at the top of the config. + ''; + }; + }; + + config = mkIf cfg.enable { + xdg.configFile."hypr/hypridle.conf" = mkIf (cfg.settings != { }) { + text = lib.hm.generators.toHyprconf { + attrs = cfg.settings; + inherit (cfg) importantPrefixes; + }; + }; + + systemd.user.services.hypridle = { + Install = { WantedBy = [ "graphical-session.target" ]; }; + + Unit = { + ConditionEnvironment = "WAYLAND_DISPLAY"; + Description = "hypridle"; + After = [ "graphical-session-pre.target" ]; + PartOf = [ "graphical-session.target" ]; + X-Restart-Triggers = + [ "${config.xdg.configFile."hypr/hypridle.conf".source}" ]; + }; + + Service = { + ExecStart = "${getExe cfg.package}"; + Restart = "always"; + RestartSec = "10"; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 7b714581..e879c13c 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -239,6 +239,7 @@ in import nmtSrc { ./modules/services/gpg-agent ./modules/services/gromit-mpx ./modules/services/home-manager-auto-upgrade + ./modules/services/hypridle ./modules/services/imapnotify ./modules/services/kanshi ./modules/services/lieer diff --git a/tests/modules/services/hypridle/basic-configuration.nix b/tests/modules/services/hypridle/basic-configuration.nix new file mode 100644 index 00000000..461f70a5 --- /dev/null +++ b/tests/modules/services/hypridle/basic-configuration.nix @@ -0,0 +1,38 @@ +{ pkgs, ... }: + +{ + services.hypridle = { + enable = true; + package = pkgs.hypridle; + + settings = { + general = { + after_sleep_cmd = "hyprctl dispatch dpms on"; + ignore_dbus_inhibit = false; + lock_cmd = "hyprlock"; + }; + + listener = [ + { + timeout = 900; + on-timeout = "hyprlock"; + } + { + timeout = 1200; + on-timeout = "hyprctl dispatch dpms off"; + on-resume = "hyprctl dispatch dpms on"; + } + ]; + }; + }; + + test.stubs.hypridle = { }; + + nmt.script = '' + config=home-files/.config/hypr/hypridle.conf + clientServiceFile=home-files/.config/systemd/user/hypridle.service + assertFileExists $config + assertFileExists $clientServiceFile + assertFileContent $config ${./hypridle.conf} + ''; +} diff --git a/tests/modules/services/hypridle/default.nix b/tests/modules/services/hypridle/default.nix new file mode 100644 index 00000000..c959983c --- /dev/null +++ b/tests/modules/services/hypridle/default.nix @@ -0,0 +1 @@ +{ hypridle-basic-configuration = ./basic-configuration.nix; } diff --git a/tests/modules/services/hypridle/hypridle.conf b/tests/modules/services/hypridle/hypridle.conf new file mode 100644 index 00000000..ee296337 --- /dev/null +++ b/tests/modules/services/hypridle/hypridle.conf @@ -0,0 +1,16 @@ +general { + after_sleep_cmd=hyprctl dispatch dpms on + ignore_dbus_inhibit=false + lock_cmd=hyprlock +} + +listener { + on-timeout=hyprlock + timeout=900 +} + +listener { + on-resume=hyprctl dispatch dpms on + on-timeout=hyprctl dispatch dpms off + timeout=1200 +}