1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-30 06:59:45 +01:00

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.
This commit is contained in:
NAHO 2024-09-09 19:43:09 +02:00
parent 10541f19c5
commit dfa7ef2b73
No known key found for this signature in database
GPG key ID: 229CB671D09B95F5
6 changed files with 52 additions and 21 deletions

View file

@ -28,7 +28,17 @@ in {
(mkRenamed [ "brightness" "night" ] "brightness-night") (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; enable = mkEnableOption programName;
dawnTime = mkOption { dawnTime = mkOption {
@ -52,22 +62,30 @@ in {
}; };
latitude = mkOption { latitude = mkOption {
type = with types; nullOr (either str float); type = with types; nullOr (either float strNotFloat);
default = null; default = null;
description = '' description = ''
Your current latitude, between `-90.0` and Your current latitude, between `-90.0` and
`90.0`. Must be provided along with `90.0`. Must be provided along with
longitude. 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 { longitude = mkOption {
type = with types; nullOr (either str float); type = with types; nullOr (either float strNotFloat);
default = null; default = null;
description = '' description = ''
Your current longitude, between `-180.0` and Your current longitude, between `-180.0` and
`180.0`. Must be provided along with `180.0`. Must be provided along with
latitude. 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) ? ${mainSection}.dusk-time)
|| (cfg.settings.${mainSection}.location-provider) == "geoclue2" || (cfg.settings.${mainSection}.location-provider) == "geoclue2"
|| ((cfg.settings.${mainSection}.location-provider) == "manual" || ((cfg.settings.${mainSection}.location-provider) == "manual"
&& (cfg.settings ? manual.lat || cfg.settings ? manual.lon)); && (cfg.latitude != null || cfg.longitude != null));
message = '' message = ''
In order for ${programName} to know the time of action, you need to set one of 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 - services.${moduleName}.provider = "geoclue2" for automatically inferring your location
@ -171,10 +189,6 @@ in {
dawn-time = mkIf (cfg.dawnTime != null) cfg.dawnTime; dawn-time = mkIf (cfg.dawnTime != null) cfg.dawnTime;
dusk-time = mkIf (cfg.duskTime != null) cfg.duskTime; 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 = xdg.configFile.${xdgConfigFilePath}.source =
@ -200,10 +214,30 @@ in {
ExecStart = let ExecStart = let
command = if cfg.tray then appletExecutable else mainExecutable; command = if cfg.tray then appletExecutable else mainExecutable;
configFullPath = config.xdg.configHome + "/${xdgConfigFilePath}"; configFullPath = config.xdg.configHome + "/${xdgConfigFilePath}";
in "${cfg.package}/bin/${command} " + cli.toGNUCommandLineShell { } { in pkgs.writeShellScript moduleName ''
v = cfg.enableVerboseLogging; ${cfg.package}/bin/${command} ${
c = configFullPath; 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; RestartSec = 3;
Restart = "on-failure"; Restart = "on-failure";
}; };

View file

@ -2,7 +2,7 @@
WantedBy=graphical-session.target WantedBy=graphical-session.target
[Service] [Service]
ExecStart=@gammastep@/bin/gammastep -c /home/hm-user/.config/gammastep/config.ini ExecStart=@gammastep@
Restart=on-failure Restart=on-failure
RestartSec=3 RestartSec=3

View file

@ -7,7 +7,5 @@ location-provider=manual
temp-day=5500 temp-day=5500
temp-night=3700 temp-night=3700
[manual]
[randr] [randr]
screen=0 screen=0

View file

@ -2,7 +2,7 @@
WantedBy=graphical-session.target WantedBy=graphical-session.target
[Service] [Service]
ExecStart=@redshift@/bin/redshift -c /home/hm-user/.config/redshift/redshift.conf ExecStart=@redshift@
Restart=on-failure Restart=on-failure
RestartSec=3 RestartSec=3

View file

@ -1,7 +1,3 @@
[manual]
lat=0.000000
lon=0.0
[randr] [randr]
screen=0 screen=0

View file

@ -6,7 +6,7 @@
enable = true; enable = true;
provider = "manual"; provider = "manual";
latitude = 0.0; latitude = 0.0;
longitude = "0.0"; longitude = "$XDG_RUNTIME_DIR/longitude";
settings = { settings = {
redshift = { redshift = {
adjustment-method = "randr"; adjustment-method = "randr";
@ -19,6 +19,9 @@
test.stubs.redshift = { }; test.stubs.redshift = { };
nmt.script = '' nmt.script = ''
export XDG_RUNTIME_DIR="$(mktemp --directory)"
echo 0.0 > "$XDG_RUNTIME_DIR/longitude"
assertFileContent \ assertFileContent \
home-files/.config/redshift/redshift.conf \ home-files/.config/redshift/redshift.conf \
${./redshift-basic-configuration-file-expected.conf} ${./redshift-basic-configuration-file-expected.conf}