Skip to content

Instantly share code, notes, and snippets.

@hyperfekt
Last active September 30, 2019 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyperfekt/ab6595330ec71fb0178d75488717a221 to your computer and use it in GitHub Desktop.
Save hyperfekt/ab6595330ec71fb0178d75488717a221 to your computer and use it in GitHub Desktop.
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.redshift;
lcfg = config.location;
target = if (cfg.config.redshift.adjustment-method or null) == "drm" then "basic.target" else "graphical-session.target";
generatedConfig = pkgs.writeText "redshift-generated.conf" (generators.toINI {} cfg.config);
allTimesSet = (cfg.config.redshift.dawn-time != null) && (cfg.config.redshift.dusk-time != null);
noTimesSet = (cfg.config.redshift.dawn-time == null) && (cfg.config.redshift.dusk-time == null);
in {
imports = [
(mkChangedOptionModule [ "services" "redshift" "latitude" ] [ "location" "latitude" ]
(config:
let value = getAttrFromPath [ "services" "redshift" "latitude" ] config;
in if value == null then
throw "services.redshift.latitude is set to null, you can remove this"
else builtins.fromJSON value))
(mkChangedOptionModule [ "services" "redshift" "longitude" ] [ "location" "longitude" ]
(config:
let value = getAttrFromPath [ "services" "redshift" "longitude" ] config;
in if value == null then
throw "services.redshift.longitude is set to null, you can remove this"
else builtins.fromJSON value))
(mkRenamedOptionModule [ "services" "redshift" "provider" ] [ "location" "provider" ])
] ++
(map (option: mkRenamedOptionModule ([ "services" "redshift" ] ++ option.old) [ "services" "redshift" "config" "redshift" option.new ]) [
{ old = [ "temperature" "day" ]; new = "temp-day"; }
{ old = [ "temperature" "night" ]; new = "temp-night"; }
{ old = [ "brightness" "day" ]; new = "brightness-day"; }
{ old = [ "brightness" "night" ]; new = "brightness-night"; }
]);
options.services.redshift = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Enable Redshift to change your screen's colour temperature depending on
the time of day.
'';
};
config = mkOption {
type = with types; attrsOf (attrsOf (nullOr (either str int)));
default = {};
description = ''
The configuration to pass to redshift.
See <command>man redshift</command> under the section
CONFIGURATION FILE for options.
'';
example = {
redshift = {
dawn-time = "05:00-06:00";
dusk-time = "22:00-23:00";
temp-night = 3000;
};
};
};
configFile = mkOption {
type = types.path;
example = "~/.config/redshift/redshift.conf";
description = ''
Configuration file for redshift. It is recommended to use the
<option>config</option> option instead.
</para>
<para>
Setting this option will override any config file auto-generated
through the <option>config</option> option.
'';
};
package = mkOption {
type = types.package;
default = pkgs.redshift;
defaultText = "pkgs.redshift";
description = ''
redshift derivation to use.
'';
};
};
config = mkIf cfg.enable {
services.redshift.configFile = mkDefault generatedConfig;
assertions = mkIf (cfg.configFile == generatedConfig) [ {
assertion = allTimesSet || noTimesSet;
message = "Time of dawn and time of dusk must be provided together.";
} ];
services.redshift.config.redshift.dawn-time = mkOptionDefault null;
services.redshift.config.redshift.dusk-time = mkOptionDefault null;
services.redshift.config.redshift.location-provider = if (noTimesSet && (lcfg ? provider)) then lcfg.provider else null;
/*services.redshift.config.manual = {
lat = mkIf (lcfg ? latitude) lcfg.latitude;
lon = mkIf (lcfg ? longitude) lcfg.longitude;
};*/
# needed so that .desktop files are installed, which geoclue cares about
environment.systemPackages = [ cfg.package ];
services.geoclue2.appConfig.redshift = {
isAllowed = true;
isSystem = true;
};
systemd.user.services.redshift = {
description = "Redshift colour temperature adjuster";
wantedBy = [ target ];
partOf = [ target ];
serviceConfig = {
ExecStart = ''
${cfg.package}/bin/redshift \
-c ${cfg.configFile}
'';
RestartSec = 3;
Restart = "always";
};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment