fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
6.2 KiB212 linesraw
1{ config, lib, pkgs, moduleName, mainSection, programName, defaultPackage
2, examplePackage, mainExecutable, appletExecutable, xdgConfigFilePath
3, serviceDocumentation }:
4
5with lib;
6
7let
8
9  cfg = config.services.${moduleName};
10  settingsFormat = pkgs.formats.ini { };
11
12in {
13  meta.maintainers = with maintainers; [ rycee thiagokokada ];
14
15  imports = let
16    mkRenamed = old: new:
17      mkRenamedOptionModule ([ "services" moduleName ] ++ old) [
18        "services"
19        moduleName
20        "settings"
21        mainSection
22        new
23      ];
24  in [
25    (mkRemovedOptionModule [ "services" moduleName "extraOptions" ]
26      "All ${programName} configuration is now available through services.${moduleName}.settings instead.")
27    (mkRenamed [ "brightness" "day" ] "brightness-day")
28    (mkRenamed [ "brightness" "night" ] "brightness-night")
29  ];
30
31  options = {
32    enable = mkEnableOption programName;
33
34    dawnTime = mkOption {
35      type = types.nullOr types.str;
36      default = null;
37      example = "6:00-7:45";
38      description = ''
39        Set the time interval of dawn manually.
40        The times must be specified as HH:MM in 24-hour format.
41      '';
42    };
43
44    duskTime = mkOption {
45      type = types.nullOr types.str;
46      default = null;
47      example = "18:35-20:15";
48      description = ''
49        Set the time interval of dusk manually.
50        The times must be specified as HH:MM in 24-hour format.
51      '';
52    };
53
54    latitude = mkOption {
55      type = with types; nullOr (either str float);
56      default = null;
57      description = ''
58        Your current latitude, between `-90.0` and
59        `90.0`. Must be provided along with
60        longitude.
61      '';
62    };
63
64    longitude = mkOption {
65      type = with types; nullOr (either str float);
66      default = null;
67      description = ''
68        Your current longitude, between `-180.0` and
69        `180.0`. Must be provided along with
70        latitude.
71      '';
72    };
73
74    provider = mkOption {
75      type = types.enum [ "manual" "geoclue2" ];
76      default = "manual";
77      description = ''
78        The location provider to use for determining your location. If set to
79        `manual` you must also provide latitude/longitude.
80        If set to `geoclue2`, you must also enable the global
81        geoclue2 service.
82      '';
83    };
84
85    temperature = {
86      day = mkOption {
87        type = types.int;
88        default = 5500;
89        description = ''
90          Colour temperature to use during the day, between
91          `1000` and `25000` K.
92        '';
93      };
94      night = mkOption {
95        type = types.int;
96        default = 3700;
97        description = ''
98          Colour temperature to use at night, between
99          `1000` and `25000` K.
100        '';
101      };
102    };
103
104    package = mkOption {
105      type = types.package;
106      default = defaultPackage;
107      defaultText = literalExpression examplePackage;
108      description = ''
109        ${programName} derivation to use.
110      '';
111    };
112
113    enableVerboseLogging = mkEnableOption "verbose service logging";
114
115    tray = mkOption {
116      type = types.bool;
117      default = false;
118      example = true;
119      description = ''
120        Start the ${appletExecutable} tray applet.
121      '';
122    };
123
124    settings = mkOption {
125      type = types.submodule { freeformType = settingsFormat.type; };
126      default = { };
127      example = literalExpression ''
128        {
129          ${mainSection} = {
130            adjustment-method = "randr";
131          };
132          randr = {
133            screen = 0;
134          };
135        };
136      '';
137      description = ''
138        The configuration to pass to ${programName}.
139        Available options for ${programName} described in
140        {manpage}`${moduleName}(1)`.
141      '';
142    };
143  };
144
145  config = {
146    assertions = [
147      (hm.assertions.assertPlatform "services.${moduleName}" pkgs
148        platforms.linux)
149
150      {
151        assertion = (cfg.settings ? ${mainSection}.dawn-time || cfg.settings
152          ? ${mainSection}.dusk-time)
153          || (cfg.settings.${mainSection}.location-provider) == "geoclue2"
154          || ((cfg.settings.${mainSection}.location-provider) == "manual"
155            && (cfg.settings ? manual.lat || cfg.settings ? manual.lon));
156        message = ''
157          In order for ${programName} to know the time of action, you need to set one of
158            - services.${moduleName}.provider = "geoclue2" for automatically inferring your location
159              (you also need to enable Geoclue2 service separately)
160            - services.${moduleName}.longitude and .latitude for specifying your location manually
161            - services.${moduleName}.dawnTime and .duskTime for specifying the times manually
162        '';
163      }
164    ];
165
166    services.${moduleName}.settings = {
167      ${mainSection} = {
168        temp-day = cfg.temperature.day;
169        temp-night = cfg.temperature.night;
170        location-provider = cfg.provider;
171        dawn-time = mkIf (cfg.dawnTime != null) cfg.dawnTime;
172        dusk-time = mkIf (cfg.duskTime != null) cfg.duskTime;
173      };
174      manual = mkIf (cfg.provider == "manual") {
175        lat = mkIf (cfg.latitude != null) (toString cfg.latitude);
176        lon = mkIf (cfg.longitude != null) (toString cfg.longitude);
177      };
178    };
179
180    xdg.configFile.${xdgConfigFilePath}.source =
181      settingsFormat.generate xdgConfigFilePath cfg.settings;
182
183    home.packages = [ cfg.package ];
184
185    systemd.user.services.${moduleName} = {
186      Unit = let
187        geoclueAgentService =
188          lib.optional (cfg.provider == "geoclue2") "geoclue-agent.service";
189      in {
190        Description = "${programName} colour temperature adjuster";
191        Documentation = serviceDocumentation;
192        After = [ "graphical-session-pre.target" ] ++ geoclueAgentService;
193        Wants = geoclueAgentService;
194        PartOf = [ "graphical-session.target" ];
195      };
196
197      Install = { WantedBy = [ "graphical-session.target" ]; };
198
199      Service = {
200        ExecStart = let
201          command = if cfg.tray then appletExecutable else mainExecutable;
202          configFullPath = config.xdg.configHome + "/${xdgConfigFilePath}";
203        in "${cfg.package}/bin/${command} " + cli.toGNUCommandLineShell { } {
204          v = cfg.enableVerboseLogging;
205          c = configFullPath;
206        };
207        RestartSec = 3;
208        Restart = "on-failure";
209      };
210    };
211  };
212}