From d8dd2a09b0a9c2c12d733f5d1eb3fa39bbe215b8 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 27 Jan 2021 21:25:47 -0300 Subject: [PATCH] redshift/gammastep: use ini file Not every option is exposed by redshift/gammastep parameters, for example gamma options are only exposed in configuration file. So this PR refactors this module to generate a configuration file and pass it to the redshift/gammastep using -c parameter. This is a breaking change since there is no support for some of the older options like `extraOptions`, but unless you use `extraOptions` it should work without changes. --- doc/release-notes/rl-2105.adoc | 29 ++++ .../services/redshift-gammastep/gammastep.nix | 5 +- .../redshift-gammastep/lib/options.nix | 147 +++++++++++------- .../services/redshift-gammastep/redshift.nix | 4 +- ...astep-basic-configuration-expected.service | 2 +- ...tep-basic-configuration-file-expected.conf | 13 ++ .../gammastep-basic-configuration.nix | 14 +- ...shift-basic-configuration-expected.service | 2 +- ...ift-basic-configuration-file-expected.conf | 13 ++ .../redshift-basic-configuration.nix | 12 +- 10 files changed, 177 insertions(+), 64 deletions(-) create mode 100644 tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf create mode 100644 tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf diff --git a/doc/release-notes/rl-2105.adoc b/doc/release-notes/rl-2105.adoc index 0de98fb2b..4fe8953ae 100644 --- a/doc/release-notes/rl-2105.adoc +++ b/doc/release-notes/rl-2105.adoc @@ -72,6 +72,35 @@ programs.rofi.extraConfig = { }; ---- +* The `services.redshift.extraOptions` and `services.gammastep.extraOptions` +options were removed in favor of <> and +`services.gammastep.settings`, that are now an attribute set rather +than a string. They also support new features not available before, for +example: ++ +[source,nix] +---- +services.redshift = { + dawnTime = "6:00-7:45"; + duskTime = "18:35-20:15"; + settings = { + redshift = { + gamma = 0.8; + adjustment-method = "randr"; + }; + + randr = { + screen = 0; + }; + }; +}; +---- ++ +It is recommended to check either +https://github.com/jonls/redshift/blob/master/redshift.conf.sample[redshift.conf.sample] or +https://gitlab.com/chinstrap/gammastep/-/blob/master/gammastep.conf.sample[gammastep.conf.sample] +for the available additional options in each program. + [[sec-release-21.05-state-version-changes]] === State Version Changes diff --git a/modules/services/redshift-gammastep/gammastep.nix b/modules/services/redshift-gammastep/gammastep.nix index 6296a7e40..d83836926 100644 --- a/modules/services/redshift-gammastep/gammastep.nix +++ b/modules/services/redshift-gammastep/gammastep.nix @@ -4,14 +4,17 @@ with lib; let commonOptions = import ./lib/options.nix { - inherit config lib; + inherit config lib pkgs; moduleName = "gammastep"; programName = "Gammastep"; + # https://gitlab.com/chinstrap/gammastep/-/commit/1608ed61154cc652b087e85c4ce6125643e76e2f + mainSection = "general"; defaultPackage = pkgs.gammastep; examplePackage = "pkgs.gammastep"; mainExecutable = "gammastep"; appletExecutable = "gammastep-indicator"; + xdgConfigFilePath = "gammastep/config.ini"; serviceDocumentation = "https://gitlab.com/chinstrap/gammastep/"; }; diff --git a/modules/services/redshift-gammastep/lib/options.nix b/modules/services/redshift-gammastep/lib/options.nix index 69f14177b..11ce9d7ce 100644 --- a/modules/services/redshift-gammastep/lib/options.nix +++ b/modules/services/redshift-gammastep/lib/options.nix @@ -1,32 +1,60 @@ -# Adapted from Nixpkgs. - -{ config, lib, moduleName, programName, defaultPackage, examplePackage -, mainExecutable, appletExecutable, serviceDocumentation }: +{ config, lib, pkgs, moduleName, mainSection, programName, defaultPackage +, examplePackage, mainExecutable, appletExecutable, xdgConfigFilePath +, serviceDocumentation }: with lib; let cfg = config.services.${moduleName}; + settingsFormat = pkgs.formats.ini { }; in { meta = { maintainers = with maintainers; [ rycee petabyteboy thiagokokada ]; }; + imports = let + mkRenamed = old: new: + mkRenamedOptionModule ([ "services" moduleName ] ++ old) [ + "services" + moduleName + "settings" + mainSection + new + ]; + in [ + (mkRemovedOptionModule [ "services" moduleName "extraOptions" ] + "All ${programName} configuration is now available through services.${moduleName}.settings instead.") + (mkRenamed [ "brightness" "day" ] "brightness-day") + (mkRenamed [ "brightness" "night" ] "brightness-night") + ]; + options = { - enable = mkOption { - type = types.bool; - default = false; - example = true; + enable = mkEnableOption programName; + + dawnTime = mkOption { + type = types.nullOr types.str; + default = null; + example = "6:00-7:45"; description = '' - Enable ${programName} to change your screen's colour temperature - depending on the time of day. + Set the time interval of dawn manually. + The times must be specified as HH:MM in 24-hour format. + ''; + }; + + duskTime = mkOption { + type = types.nullOr types.str; + default = null; + example = "18:35-20:15"; + description = '' + Set the time interval of dusk manually. + The times must be specified as HH:MM in 24-hour format. ''; }; latitude = mkOption { - type = types.nullOr types.str; + type = with types; nullOr (either str float); default = null; description = '' Your current latitude, between -90.0 and @@ -36,7 +64,7 @@ in { }; longitude = mkOption { - type = types.nullOr types.str; + type = with types; nullOr (either str float); default = null; description = '' Your current longitude, between -180.0 and @@ -75,26 +103,6 @@ in { }; }; - 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 = defaultPackage; @@ -113,26 +121,63 @@ in { ''; }; - extraOptions = mkOption { - type = types.listOf types.str; - default = [ ]; - example = [ "-v" "-m randr" ]; + settings = mkOption { + type = types.submodule { freeformType = settingsFormat.type; }; + default = { }; + example = literalExample '' + { + ${mainSection} = { + adjustment-method = "randr"; + }; + randr = { + screen = 0; + }; + }; + ''; description = '' - Additional command-line arguments to pass to - redshift. + The configuration to pass to ${programName}. + Available options for ${programName} described in + + ${moduleName} + 1 + . ''; }; }; config = { assertions = [{ - assertion = cfg.provider == "manual" -> cfg.latitude != null - && cfg.longitude != null; - message = "Must provide services.${moduleName}.latitude and" - + " services.${moduleName}.latitude when" - + " services.${moduleName}.provider is set to \"manual\"."; + assertion = (cfg.settings ? ${mainSection}.dawn-time || cfg.settings + ? ${mainSection}.dusk-time) + || (cfg.settings.${mainSection}.location-provider) == "geoclue2" + || ((cfg.settings.${mainSection}.location-provider) == "manual" + && (cfg.settings ? manual.lat || cfg.settings ? manual.lon)); + message = '' + In order for ${programName} to know the time of action, you need to set one of + - services.${moduleName}.provider = "geoclue2" for automatically inferring your location + (you also need to enable Geoclue2 service separately) + - services.${moduleName}.longitude and .latitude for specifying your location manually + - services.${moduleName}.dawnTime and .duskTime for specifying the times manually + ''; }]; + services.${moduleName}.settings = { + ${mainSection} = { + temp-day = cfg.temperature.day; + temp-night = cfg.temperature.night; + location-provider = cfg.provider; + dawn-time = mkIf (cfg.dawnTime != null) cfg.dawnTime; + dusk-time = mkIf (cfg.duskTime != null) cfg.duskTime; + }; + manual = mkIf (cfg.provider == "manual") { + lat = mkIf (cfg.latitude != null) (toString cfg.latitude); + lon = mkIf (cfg.longitude != null) (toString cfg.longitude); + }; + }; + + xdg.configFile.${xdgConfigFilePath}.source = + settingsFormat.generate xdgConfigFilePath cfg.settings; + systemd.user.services.${moduleName} = { Unit = { Description = "${programName} colour temperature adjuster"; @@ -145,21 +190,9 @@ in { 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 appletExecutable else mainExecutable; - in "${cfg.package}/bin/${command} ${concatStringsSep " " args}"; + configFullPath = config.xdg.configHome + "/${xdgConfigFilePath}"; + in "${cfg.package}/bin/${command} -v -c ${configFullPath}"; RestartSec = 3; Restart = "on-failure"; }; diff --git a/modules/services/redshift-gammastep/redshift.nix b/modules/services/redshift-gammastep/redshift.nix index 068dbd67b..d65227ca1 100644 --- a/modules/services/redshift-gammastep/redshift.nix +++ b/modules/services/redshift-gammastep/redshift.nix @@ -4,14 +4,16 @@ with lib; let commonOptions = import ./lib/options.nix { - inherit config lib; + inherit config lib pkgs; moduleName = "redshift"; programName = "Redshift"; + mainSection = "redshift"; defaultPackage = pkgs.redshift; examplePackage = "pkgs.redshift"; mainExecutable = "redshift"; appletExecutable = "redshift-gtk"; + xdgConfigFilePath = "redshift/redshift.conf"; serviceDocumentation = "http://jonls.dk/redshift/"; }; diff --git a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-expected.service b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-expected.service index 25b95b55a..8d0e7a5fb 100644 --- a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-expected.service +++ b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-expected.service @@ -2,7 +2,7 @@ WantedBy=graphical-session.target [Service] -ExecStart=@gammastep@/bin/gammastep -l 0.0:0.0 -t 5500:3700 -b 1:1 +ExecStart=@gammastep@/bin/gammastep -v -c /home/hm-user/.config/gammastep/config.ini Restart=on-failure RestartSec=3 diff --git a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf new file mode 100644 index 000000000..a33230a68 --- /dev/null +++ b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf @@ -0,0 +1,13 @@ +[general] +adjustment-method=randr +dawn-time=6:00-7:45 +dusk-time=18:35-20:15 +gamma=0.800000 +location-provider=manual +temp-day=5500 +temp-night=3700 + +[manual] + +[randr] +screen=0 diff --git a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration.nix b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration.nix index c3baf8d98..9ccf38528 100644 --- a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration.nix +++ b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration.nix @@ -5,8 +5,15 @@ services.gammastep = { enable = true; provider = "manual"; - latitude = "0.0"; - longitude = "0.0"; + dawnTime = "6:00-7:45"; + duskTime = "18:35-20:15"; + settings = { + general = { + adjustment-method = "randr"; + gamma = 0.8; + }; + randr = { screen = 0; }; + }; }; nixpkgs.overlays = [ @@ -18,6 +25,9 @@ ]; nmt.script = '' + assertFileContent \ + home-files/.config/gammastep/config.ini \ + ${./gammastep-basic-configuration-file-expected.conf} assertFileContent \ home-files/.config/systemd/user/gammastep.service \ ${./gammastep-basic-configuration-expected.service} diff --git a/tests/modules/services/redshift-gammastep/redshift-basic-configuration-expected.service b/tests/modules/services/redshift-gammastep/redshift-basic-configuration-expected.service index 13ccf550e..07ffbf06d 100644 --- a/tests/modules/services/redshift-gammastep/redshift-basic-configuration-expected.service +++ b/tests/modules/services/redshift-gammastep/redshift-basic-configuration-expected.service @@ -2,7 +2,7 @@ WantedBy=graphical-session.target [Service] -ExecStart=@redshift@/bin/redshift -l 0.0:0.0 -t 5500:3700 -b 1:1 +ExecStart=@redshift@/bin/redshift -v -c /home/hm-user/.config/redshift/redshift.conf Restart=on-failure RestartSec=3 diff --git a/tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf b/tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf new file mode 100644 index 000000000..f2aab6a53 --- /dev/null +++ b/tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf @@ -0,0 +1,13 @@ +[manual] +lat=0.000000 +lon=0.0 + +[randr] +screen=0 + +[redshift] +adjustment-method=randr +gamma=0.800000 +location-provider=manual +temp-day=5500 +temp-night=3700 diff --git a/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix b/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix index ab8a159a4..5e2fe40e0 100644 --- a/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix +++ b/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix @@ -5,8 +5,15 @@ services.redshift = { enable = true; provider = "manual"; - latitude = "0.0"; + latitude = 0.0; longitude = "0.0"; + settings = { + redshift = { + adjustment-method = "randr"; + gamma = 0.8; + }; + randr = { screen = 0; }; + }; }; nixpkgs.overlays = [ @@ -18,6 +25,9 @@ ]; nmt.script = '' + assertFileContent \ + home-files/.config/redshift/redshift.conf \ + ${./redshift-basic-configuration-file-expected.conf} assertFileContent \ home-files/.config/systemd/user/redshift.service \ ${./redshift-basic-configuration-expected.service}