From dfa7ef2b732838a73337c5aefa3d08ad2d148519 Mon Sep 17 00:00:00 2001 From: NAHO <90870942+trueNAHO@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:43:09 +0200 Subject: [PATCH] redshift-gammastep: support reading latitude and longitude from files Support reading latitude and longitude from files, which is useful for reading the values from decrypted runtime files. The new strNotFloat type catches deprecated str types used for the latitude and longitude options. The systemd ExecStart command points to a wrapper script for subshell invocations. --- .../redshift-gammastep/lib/options.nix | 58 +++++++++++++++---- ...astep-basic-configuration-expected.service | 2 +- ...tep-basic-configuration-file-expected.conf | 2 - ...shift-basic-configuration-expected.service | 2 +- ...ift-basic-configuration-file-expected.conf | 4 -- .../redshift-basic-configuration.nix | 5 +- 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/modules/services/redshift-gammastep/lib/options.nix b/modules/services/redshift-gammastep/lib/options.nix index 81347e643..ae9893829 100644 --- a/modules/services/redshift-gammastep/lib/options.nix +++ b/modules/services/redshift-gammastep/lib/options.nix @@ -28,7 +28,17 @@ in { (mkRenamed [ "brightness" "night" ] "brightness-night") ]; - options = { + options = let + strNotFloat = (pattern: + lib.mkOptionType { + inherit (lib.types.str) merge; + + check = x: lib.types.str.check x && builtins.match pattern x == null; + description = "string not matching the pattern ${pattern}"; + descriptionClass = "noun"; + name = "strNotMatching ${lib.strings.escapeNixString pattern}"; + }) "^-?[0-9]+\\.[0-9]+$"; + in { enable = mkEnableOption programName; dawnTime = mkOption { @@ -52,22 +62,30 @@ in { }; latitude = mkOption { - type = with types; nullOr (either str float); + type = with types; nullOr (either float strNotFloat); default = null; description = '' Your current latitude, between `-90.0` and `90.0`. Must be provided along with longitude. + + Specifying a string retrieves the latitude using the `"$(< + "''${config.services.${moduleName}.latitude}")"` command at runtime, + which is useful for reading the value from a decrypted runtime file. ''; }; longitude = mkOption { - type = with types; nullOr (either str float); + type = with types; nullOr (either float strNotFloat); default = null; description = '' Your current longitude, between `-180.0` and `180.0`. Must be provided along with latitude. + + Specifying a string retrieves the longitude using the `"$(< + "''${config.services.${moduleName}.longitude}")"` command at runtime, + which is useful for reading the value from a decrypted runtime file. ''; }; @@ -152,7 +170,7 @@ in { ? ${mainSection}.dusk-time) || (cfg.settings.${mainSection}.location-provider) == "geoclue2" || ((cfg.settings.${mainSection}.location-provider) == "manual" - && (cfg.settings ? manual.lat || cfg.settings ? manual.lon)); + && (cfg.latitude != null || cfg.longitude != null)); 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 @@ -171,10 +189,6 @@ in { 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 = @@ -200,10 +214,30 @@ in { ExecStart = let command = if cfg.tray then appletExecutable else mainExecutable; configFullPath = config.xdg.configHome + "/${xdgConfigFilePath}"; - in "${cfg.package}/bin/${command} " + cli.toGNUCommandLineShell { } { - v = cfg.enableVerboseLogging; - c = configFullPath; - }; + in pkgs.writeShellScript moduleName '' + ${cfg.package}/bin/${command} ${ + cli.toGNUCommandLineShell { } { + v = cfg.enableVerboseLogging; + c = configFullPath; + } + } ${ + optionalString (cfg.provider == "manual" + && (cfg.latitude != null || cfg.longitude != null)) '' + -l "${ + optionalString (cfg.latitude != null) + (if builtins.isFloat cfg.latitude then + toString cfg.latitude + else + ''$(< "${cfg.latitude}")'') + }:${ + optionalString (cfg.latitude != null) + (if builtins.isFloat cfg.longitude then + toString cfg.longitude + else + ''$(< "${cfg.longitude}")'') + }"'' + } + ''; RestartSec = 3; Restart = "on-failure"; }; 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 35eaf519f..2aff84969 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 -c /home/hm-user/.config/gammastep/config.ini +ExecStart=@gammastep@ 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 index a33230a68..3b9fe0740 100644 --- a/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf +++ b/tests/modules/services/redshift-gammastep/gammastep-basic-configuration-file-expected.conf @@ -7,7 +7,5 @@ location-provider=manual temp-day=5500 temp-night=3700 -[manual] - [randr] screen=0 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 5dce5dc19..d43856f9a 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 -c /home/hm-user/.config/redshift/redshift.conf +ExecStart=@redshift@ 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 index f2aab6a53..11a7ec3a1 100644 --- a/tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf +++ b/tests/modules/services/redshift-gammastep/redshift-basic-configuration-file-expected.conf @@ -1,7 +1,3 @@ -[manual] -lat=0.000000 -lon=0.0 - [randr] screen=0 diff --git a/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix b/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix index 691d01e25..55e8f81c8 100644 --- a/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix +++ b/tests/modules/services/redshift-gammastep/redshift-basic-configuration.nix @@ -6,7 +6,7 @@ enable = true; provider = "manual"; latitude = 0.0; - longitude = "0.0"; + longitude = "$XDG_RUNTIME_DIR/longitude"; settings = { redshift = { adjustment-method = "randr"; @@ -19,6 +19,9 @@ test.stubs.redshift = { }; nmt.script = '' + export XDG_RUNTIME_DIR="$(mktemp --directory)" + echo 0.0 > "$XDG_RUNTIME_DIR/longitude" + assertFileContent \ home-files/.config/redshift/redshift.conf \ ${./redshift-basic-configuration-file-expected.conf}