hyprshade: add module

This commit is contained in:
svl 2024-06-03 12:38:59 +02:00
parent 9b53a10f4c
commit fe92de159e
7 changed files with 245 additions and 0 deletions

View file

@ -554,4 +554,10 @@
github = "zorrobert";
githubId = 118135271;
};
svl = {
matrix = "@gyn:matrix.org";
github = "4lxs";
githubId = 66408983;
name = "svl";
};
}

View file

@ -308,6 +308,7 @@ let
./services/hound.nix
./services/hypridle.nix
./services/hyprpaper.nix
./services/hyprshade.nix
./services/imapnotify.nix
./services/kanshi.nix
./services/kbfs.nix

View file

@ -0,0 +1,173 @@
{ config, lib, pkgs, ... }:
with lib;
let
configFormat = pkgs.formats.toml { };
shadeType = with types;
submodule {
options = {
name = mkOption {
type = str;
example = "color-filter";
description = ''
name of the shade
'';
};
default = lib.mkEnableOption ''
whether to use this shade when no other shade is scheduled
'';
startTime = mkOption {
type = nullOr str;
default = null;
example = "19:00:00";
description = ''
time to start the shade in 24-hour "hh:mm:ss" format
'';
};
endTime = mkOption {
type = nullOr str;
default = null;
example = "06:00:00";
description = ''
time to end the shade in 24-hour "hh:mm:ss" format.
optional if you have more than one shade with startTime
'';
};
config = mkOption {
inherit (configFormat) type;
default = { };
example = {
type = "red-green";
strength = 1.0;
};
description = ''
configuration passed to the shade
'';
};
};
};
cfg = config.services.hyprshade;
in {
meta.maintainers = [ maintainers.svl ];
options.services.hyprshade = {
enable = mkEnableOption "hyprshade, Hyprland shade configuration tool ";
package = mkPackageOption pkgs "hyprshade" { };
additionalShades = mkOption {
type = types.attrsOf types.str;
default = [ ];
description = ''
additional shades that you can then use with hyprshade
'';
};
schedule = mkOption {
type = types.listOf shadeType;
default = [ ];
example = [
{
name = "vibrance";
default = true;
}
{
name = "blue-light-filter";
startTime = "19:00:00";
endTime = "06:00:00";
}
{
name = "color-filter";
config = {
type = "red-green";
strength = 0.5;
};
}
];
description = "";
};
extraConfig = mkOption {
inherit (configFormat) type;
default = { };
description = ''
extra configuration to be added to the hyprshade.toml file
'';
};
systemd.enable = mkOption {
type = types.bool;
default = true;
example = false;
description = ''
whether to enable the hyprshade systemd service that will apply the
shade based on the provided schedule.
if you don't provide the schedule, the service may not work
'';
};
};
config = mkIf cfg.enable {
xdg.configFile = {
"hypr/hyprshade.toml".source = let
mkShadeConf = conf:
filterAttrs (_: val: val != null) {
inherit (conf) name config;
start_time = conf.startTime or null;
end_time = conf.endTime or null;
default = conf.default or false;
};
config = { shades = builtins.map mkShadeConf cfg.schedule; };
in pkgs.callPackage ({ runCommand, remarshal }:
runCommand "hyprshade.toml" {
nativeBuildInputs = [ remarshal ];
value = builtins.toJSON (config // cfg.extraConfig);
passAsFile = [ "value" ];
preferLocalBuild = true;
} ''
json2toml "$valuePath" "$out"
# remove quotes around time values e.g. "19:00:00" -> 19:00:00
sed -i 's/"\(\([[:digit:]]\{2\}:\?\)\{3\}\)"/\1/' "$out"
'') { };
} // mapAttrs'
(name: shade: nameValuePair "hypr/shaders/${name}" { text = shade; })
cfg.additionalShades;
systemd.user = mkIf cfg.systemd.enable {
services.hyprshade = {
Install.WantedBy = [ "graphical-session.target" ];
Unit = {
ConditionEnvironment = "HYPRLAND_INSTANCE_SIGNATURE";
Description = "Apply screen filter";
After = [ "graphical-session-pre.target" ];
PartOf = [ "graphical-session.target" ];
};
Service = {
Type = "oneshot";
ExecStart = "${getExe cfg.package} auto";
};
};
timers.hyprshade = {
Install.WantedBy = [ "timers.target" ];
Unit = { Description = "Apply screen filter on schedule"; };
Timer.OnCalendar = builtins.map (time: "*-*-* ${time}") (builtins.foldl'
(acc: sched:
acc ++ (lists.optional (sched.startTime != null) sched.startTime)
++ (lists.optional (sched.endTime != null) sched.endTime)) [ ]
cfg.schedule);
};
};
};
}

View file

@ -245,6 +245,7 @@ in import nmtSrc {
./modules/services/home-manager-auto-upgrade
./modules/services/hypridle
./modules/services/hyprpaper
./modules/services/hyprshade
./modules/services/imapnotify
./modules/services/kanshi
./modules/services/lieer

View file

@ -0,0 +1,36 @@
{ pkgs, ... }: {
services.hyprshade = {
enable = true;
package = pkgs.hyprshade;
schedule = [
{
name = "vibrance";
default = true;
}
{
name = "blue-light-filter";
startTime = "06:00:00";
endTime = "19:00:00";
}
{
name = "color-filter";
config = {
type = "red-green";
strength = 0.5;
};
}
];
systemd.enable = true;
};
test.stubs.hyprshade = { };
nmt.script = ''
config=home-files/.config/hypr/hyprshade.toml
clientServiceFile=home-files/.config/systemd/user/hyprshade.service
assertFileExists $config
assertFileExists $clientServiceFile
assertFileContent $config ${./hyprshade.toml}
'';
}

View file

@ -0,0 +1 @@
{ hyprshade-basic-configuration = ./basic-configuration.nix; }

View file

@ -0,0 +1,27 @@
[[shades]]
default = true
end_time = 06:00:00
name = "vibrance"
start_time = 19:00:00
[shades.config]
fuck = "me"
[[shades]]
default = false
end_time = 19:00:00
name = "blue-light-filter"
start_time = 06:00:00
[shades.config]
fuck = "me"
[[shades]]
default = false
end_time = 06:00:00
name = "color-filter"
start_time = 19:00:00
[shades.config]
strength = 0.5
type = "red-green"