1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.redshift;
8
9in {
10
11 options.services.redshift = {
12 enable = mkOption {
13 type = types.bool;
14 default = false;
15 example = true;
16 description = ''
17 Enable Redshift to change your screen's colour temperature depending on
18 the time of day.
19 '';
20 };
21
22 latitude = mkOption {
23 type = types.str;
24 description = ''
25 Your current latitude, between
26 <literal>-90.0</literal> and <literal>90.0</literal>.
27 '';
28 };
29
30 longitude = mkOption {
31 type = types.str;
32 description = ''
33 Your current longitude, between
34 between <literal>-180.0</literal> and <literal>180.0</literal>.
35 '';
36 };
37
38 temperature = {
39 day = mkOption {
40 type = types.int;
41 default = 5500;
42 description = ''
43 Colour temperature to use during the day, between
44 <literal>1000</literal> and <literal>25000</literal> K.
45 '';
46 };
47 night = mkOption {
48 type = types.int;
49 default = 3700;
50 description = ''
51 Colour temperature to use at night, between
52 <literal>1000</literal> and <literal>25000</literal> K.
53 '';
54 };
55 };
56
57 brightness = {
58 day = mkOption {
59 type = types.str;
60 default = "1";
61 description = ''
62 Screen brightness to apply during the day,
63 between <literal>0.1</literal> and <literal>1.0</literal>.
64 '';
65 };
66 night = mkOption {
67 type = types.str;
68 default = "1";
69 description = ''
70 Screen brightness to apply during the night,
71 between <literal>0.1</literal> and <literal>1.0</literal>.
72 '';
73 };
74 };
75
76 package = mkOption {
77 type = types.package;
78 default = pkgs.redshift;
79 defaultText = "pkgs.redshift";
80 description = ''
81 redshift derivation to use.
82 '';
83 };
84
85 extraOptions = mkOption {
86 type = types.listOf types.str;
87 default = [];
88 example = [ "-v" "-m randr" ];
89 description = ''
90 Additional command-line arguments to pass to
91 <command>redshift</command>.
92 '';
93 };
94 };
95
96 config = mkIf cfg.enable {
97 systemd.user.services.redshift = {
98 description = "Redshift colour temperature adjuster";
99 wantedBy = [ "default.target" ];
100 serviceConfig = {
101 ExecStart = ''
102 ${cfg.package}/bin/redshift \
103 -l ${cfg.latitude}:${cfg.longitude} \
104 -t ${toString cfg.temperature.day}:${toString cfg.temperature.night} \
105 -b ${toString cfg.brightness.day}:${toString cfg.brightness.night} \
106 ${lib.strings.concatStringsSep " " cfg.extraOptions}
107 '';
108 RestartSec = 3;
109 Restart = "always";
110 };
111 environment = {
112 DISPLAY = ":${toString (
113 let display = config.services.xserver.display;
114 in if display != null then display else 0
115 )}";
116 };
117 };
118 };
119
120}