From 6b15b038986e989839b4820aa2de13c3a2b4bbf9 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 23 Jan 2021 19:03:57 -0300 Subject: [PATCH] redshift/gammastep: unify common options Nowadays services.{redshift,gammastep} modules are really similar. They should, since Gammastep is a fork of Redshift with the main objective is to support Wayland. So instead of trying to maintain two separate modules, this commit unify the options in lib/options.nix file, making the implementation of the module itself ends up being really simple (just calling the common options with the necessary parameters to differentiate between them). --- .github/CODEOWNERS | 5 +- modules/modules.nix | 4 +- modules/services/gammastep.nix | 166 ------------------ .../services/redshift-gammastep/gammastep.nix | 22 +++ .../lib/options.nix} | 41 +++-- .../services/redshift-gammastep/redshift.nix | 22 +++ 6 files changed, 70 insertions(+), 190 deletions(-) delete mode 100644 modules/services/gammastep.nix create mode 100644 modules/services/redshift-gammastep/gammastep.nix rename modules/services/{redshift.nix => redshift-gammastep/lib/options.nix} (78%) create mode 100644 modules/services/redshift-gammastep/redshift.nix diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 3946afef5..a1b6c8718 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -167,8 +167,6 @@ /modules/services/fluidsynth.nix @Valodim -/modules/services/gammastep.nix @petabyteboy - /modules/services/gnome-keyring.nix @rycee /modules/services/gpg-agent.nix @rycee @@ -215,7 +213,8 @@ /modules/services/random-background.nix @rycee -/modules/services/redshift.nix @rycee +/modules/services/redshift-gammastep @rycee @petabyteboy @thiagokokada +/tests/modules/redshift-gammastep @thiagokokada /modules/services/status-notifier-watcher.nix @pltanton diff --git a/modules/modules.nix b/modules/modules.nix index 752aee7ce..0abec5be2 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -146,7 +146,7 @@ let (loadModule ./services/emacs.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/flameshot.nix { }) (loadModule ./services/fluidsynth.nix { condition = hostPlatform.isLinux; }) - (loadModule ./services/gammastep.nix { condition = hostPlatform.isLinux; }) + (loadModule ./services/redshift-gammastep/gammastep.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/getmail.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/gnome-keyring.nix { }) (loadModule ./services/gpg-agent.nix { }) @@ -178,7 +178,7 @@ let (loadModule ./services/polybar.nix { }) (loadModule ./services/pulseeffects.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/random-background.nix { }) - (loadModule ./services/redshift.nix { }) + (loadModule ./services/redshift-gammastep/redshift.nix { }) (loadModule ./services/rsibreak.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/screen-locker.nix { }) (loadModule ./services/stalonetray.nix { }) diff --git a/modules/services/gammastep.nix b/modules/services/gammastep.nix deleted file mode 100644 index 7740c462c..000000000 --- a/modules/services/gammastep.nix +++ /dev/null @@ -1,166 +0,0 @@ -# Adapted from Nixpkgs. - -{ config, lib, pkgs, ... }: - -with lib; - -let - - cfg = config.services.gammastep; - -in { - meta.maintainers = [ maintainers.petabyteboy ]; - - options.services.gammastep = { - enable = mkOption { - type = types.bool; - default = false; - example = true; - description = '' - Enable Gammastep to change your screen's colour temperature depending on - the time of day. - ''; - }; - - latitude = mkOption { - type = types.nullOr types.str; - default = null; - description = '' - Your current latitude, between -90.0 and - 90.0. Must be provided along with - longitude. - ''; - }; - - longitude = mkOption { - type = types.nullOr types.str; - default = null; - description = '' - Your current longitude, between -180.0 and - 180.0. Must be provided along with - latitude. - ''; - }; - - provider = mkOption { - type = types.enum [ "manual" "geoclue2" ]; - default = "manual"; - description = '' - The location provider to use for determining your location. If set to - manual you must also provide latitude/longitude. - If set to geoclue2, you must also enable the global - geoclue2 service. - ''; - }; - - temperature = { - day = mkOption { - type = types.int; - default = 5500; - description = '' - Colour temperature to use during the day, between - 1000 and 25000 K. - ''; - }; - - night = mkOption { - type = types.int; - default = 3700; - description = '' - Colour temperature to use at night, between - 1000 and 25000 K. - ''; - }; - }; - - brightness = { - day = mkOption { - type = types.str; - default = "1"; - description = '' - Screen brightness to apply during the day, - between 0.1 and 1.0. - ''; - }; - - night = mkOption { - type = types.str; - default = "1"; - description = '' - Screen brightness to apply during the night, - between 0.1 and 1.0. - ''; - }; - }; - - package = mkOption { - type = types.package; - default = pkgs.gammastep; - defaultText = literalExample "pkgs.gammastep"; - description = '' - gammastep derivation to use. - ''; - }; - - tray = mkOption { - type = types.bool; - default = false; - example = true; - description = '' - Start the gammastep-indicator tray applet. - ''; - }; - - extraOptions = mkOption { - type = types.listOf types.str; - default = [ ]; - example = [ "-v" "-m randr" ]; - description = '' - Additional command-line arguments to pass to - gammastep. - ''; - }; - }; - - config = mkIf cfg.enable { - assertions = [{ - assertion = cfg.provider == "manual" -> cfg.latitude != null - && cfg.longitude != null; - message = "Must provide services.gammastep.latitude and" - + " services.gammastep.latitude when" - + " services.gammastep.provider is set to \"manual\"."; - }]; - - systemd.user.services.gammastep = { - Unit = { - Description = "Gammastep colour temperature adjuster"; - After = [ "graphical-session-pre.target" ]; - PartOf = [ "graphical-session.target" ]; - }; - - Install = { WantedBy = [ "graphical-session.target" ]; }; - - Service = { - ExecStart = let - providerString = if cfg.provider == "manual" then - "${cfg.latitude}:${cfg.longitude}" - else - cfg.provider; - - args = [ - "-l ${providerString}" - "-t ${toString cfg.temperature.day}:${ - toString cfg.temperature.night - }" - "-b ${toString cfg.brightness.day}:${toString cfg.brightness.night}" - ] ++ cfg.extraOptions; - - command = if cfg.tray then "gammastep-indicator" else "gammastep"; - in "${cfg.package}/bin/${command} ${concatStringsSep " " args}"; - RestartSec = 3; - Restart = "always"; - }; - }; - }; - -} diff --git a/modules/services/redshift-gammastep/gammastep.nix b/modules/services/redshift-gammastep/gammastep.nix new file mode 100644 index 000000000..6296a7e40 --- /dev/null +++ b/modules/services/redshift-gammastep/gammastep.nix @@ -0,0 +1,22 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + commonOptions = import ./lib/options.nix { + inherit config lib; + + moduleName = "gammastep"; + programName = "Gammastep"; + defaultPackage = pkgs.gammastep; + examplePackage = "pkgs.gammastep"; + mainExecutable = "gammastep"; + appletExecutable = "gammastep-indicator"; + serviceDocumentation = "https://gitlab.com/chinstrap/gammastep/"; + }; + +in { + meta = commonOptions.meta; + options.services.gammastep = commonOptions.options; + config = mkIf config.services.gammastep.enable commonOptions.config; +} diff --git a/modules/services/redshift.nix b/modules/services/redshift-gammastep/lib/options.nix similarity index 78% rename from modules/services/redshift.nix rename to modules/services/redshift-gammastep/lib/options.nix index f87fbc273..69f14177b 100644 --- a/modules/services/redshift.nix +++ b/modules/services/redshift-gammastep/lib/options.nix @@ -1,24 +1,27 @@ # Adapted from Nixpkgs. -{ config, lib, pkgs, ... }: +{ config, lib, moduleName, programName, defaultPackage, examplePackage +, mainExecutable, appletExecutable, serviceDocumentation }: with lib; let - cfg = config.services.redshift; + cfg = config.services.${moduleName}; in { - meta.maintainers = [ maintainers.rycee ]; + meta = { + maintainers = with maintainers; [ rycee petabyteboy thiagokokada ]; + }; - options.services.redshift = { + options = { enable = mkOption { type = types.bool; default = false; example = true; description = '' - Enable Redshift to change your screen's colour temperature depending on - the time of day. + Enable ${programName} to change your screen's colour temperature + depending on the time of day. ''; }; @@ -81,6 +84,7 @@ in { between 0.1 and 1.0. ''; }; + night = mkOption { type = types.str; default = "1"; @@ -93,10 +97,10 @@ in { package = mkOption { type = types.package; - default = pkgs.redshift; - defaultText = literalExample "pkgs.redshift"; + default = defaultPackage; + defaultText = literalExample examplePackage; description = '' - redshift derivation to use. + ${programName} derivation to use. ''; }; @@ -105,7 +109,7 @@ in { default = false; example = true; description = '' - Start the redshift-gtk tray applet. + Start the ${appletExecutable} tray applet. ''; }; @@ -120,19 +124,19 @@ in { }; }; - config = mkIf cfg.enable { + config = { assertions = [{ assertion = cfg.provider == "manual" -> cfg.latitude != null && cfg.longitude != null; - message = "Must provide services.redshift.latitude and" - + " services.redshift.latitude when" - + " services.redshift.provider is set to \"manual\"."; + message = "Must provide services.${moduleName}.latitude and" + + " services.${moduleName}.latitude when" + + " services.${moduleName}.provider is set to \"manual\"."; }]; - systemd.user.services.redshift = { + systemd.user.services.${moduleName} = { Unit = { - Description = "Redshift colour temperature adjuster"; - Documentation = "http://jonls.dk/redshift/"; + Description = "${programName} colour temperature adjuster"; + Documentation = serviceDocumentation; After = [ "graphical-session-pre.target" ]; PartOf = [ "graphical-session.target" ]; }; @@ -154,12 +158,11 @@ in { "-b ${toString cfg.brightness.day}:${toString cfg.brightness.night}" ] ++ cfg.extraOptions; - command = if cfg.tray then "redshift-gtk" else "redshift"; + command = if cfg.tray then appletExecutable else mainExecutable; in "${cfg.package}/bin/${command} ${concatStringsSep " " args}"; RestartSec = 3; Restart = "on-failure"; }; }; }; - } diff --git a/modules/services/redshift-gammastep/redshift.nix b/modules/services/redshift-gammastep/redshift.nix new file mode 100644 index 000000000..068dbd67b --- /dev/null +++ b/modules/services/redshift-gammastep/redshift.nix @@ -0,0 +1,22 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + commonOptions = import ./lib/options.nix { + inherit config lib; + + moduleName = "redshift"; + programName = "Redshift"; + defaultPackage = pkgs.redshift; + examplePackage = "pkgs.redshift"; + mainExecutable = "redshift"; + appletExecutable = "redshift-gtk"; + serviceDocumentation = "http://jonls.dk/redshift/"; + }; + +in { + meta = commonOptions.meta; + options.services.redshift = commonOptions.options; + config = mkIf config.services.redshift.enable commonOptions.config; +}