@@ -4,6 +4,7 @@ with lib;
44
55let
66 cfg = config.services.grafana;
7+ opt = options.services.grafana;
78
89 envOptions = {
910 PATHS_DATA = cfg.dataDir;
@@ -41,6 +42,12 @@ let
4142 AUTH_ANONYMOUS_ORG_ROLE = cfg.auth.anonymous.org_role;
4243
4344 ANALYTICS_REPORTING_ENABLED = boolToString cfg.analytics.reporting.enable;
45+
46+ SMTP_ENABLE = boolToString cfg.smtp.enable;
47+ SMTP_HOST = cfg.smtp.host;
48+ SMTP_USER = cfg.smtp.user;
49+ SMTP_PASSWORD = cfg.smtp.password;
50+ SMTP_FROM_ADDRESS = cfg.smtp.fromAddress;
4451 } // cfg.extraOptions;
4552
4653in {
@@ -134,11 +141,23 @@ in {
134141 };
135142
136143 password = mkOption {
137− description = "Database password.";
144+ description = ''
145+ Database password.
146+ This option is mutual exclusive with the passwordFile option.
147+ '';
138148 default = "";
139149 type = types.str;
140150 };
141151
152+ passwordFile = mkOption {
153+ description = ''
154+ File that containts the database password.
155+ This option is mutual exclusive with the password option.
156+ '';
157+ default = null;
158+ type = types.nullOr types.path;
159+ };
160+
142161 path = mkOption {
143162 description = "Database path.";
144163 default = "${cfg.dataDir}/data/grafana.db";
@@ -163,16 +182,69 @@ in {
163182 };
164183
165184 adminPassword = mkOption {
166− description = "Default admin password.";
185+ description = ''
186+ Default admin password.
187+ This option is mutual exclusive with the adminPasswordFile option.
188+ '';
167189 default = "admin";
168190 type = types.str;
169191 };
170192
193+ adminPasswordFile = mkOption {
194+ description = ''
195+ Default admin password.
196+ This option is mutual exclusive with the <literal>adminPassword</literal> option.
197+ '';
198+ default = null;
199+ type = types.nullOr types.path;
200+ };
201+
171202 secretKey = mkOption {
172203 description = "Secret key used for signing.";
173204 default = "SW2YcwTIb9zpOOhoPsMm";
174205 type = types.str;
175206 };
207+
208+ secretKeyFile = mkOption {
209+ description = "Secret key used for signing.";
210+ default = null;
211+ type = types.nullOr types.path;
212+ };
213+ };
214+
215+ smtp = {
216+ enable = mkEnableOption "smtp";
217+ host = mkOption {
218+ description = "Host to connect to";
219+ default = "localhost:25";
220+ type = types.str;
221+ };
222+ user = mkOption {
223+ description = "User used for authentication";
224+ default = "";
225+ type = types.str;
226+ };
227+ password = mkOption {
228+ description = ''
229+ Password used for authentication.
230+ This option is mutual exclusive with the passwordFile option.
231+ '';
232+ default = "";
233+ type = types.str;
234+ };
235+ passwordFile = mkOption {
236+ description = ''
237+ Password used for authentication.
238+ This option is mutual exclusive with the password option.
239+ '';
240+ default = null;
241+ type = types.nullOr types.path;
242+ };
243+ fromAddress = mkOption {
244+ description = "Email address used for sending";
245+ default = "admin@grafana.localhost";
246+ type = types.str;
247+ };
176248 };
177249
178250 users = {
@@ -241,12 +313,31 @@ in {
241313
242314 config = mkIf cfg.enable {
243315 warnings = optional (
244− cfg.database.password != options.services.grafana.database.password.default ||
245− cfg.security.adminPassword != options.services.grafana.security.adminPassword.default
316+ cfg.database.password != opt.database.password.default ||
317+ cfg.security.adminPassword != opt.security.adminPassword.default
246318 ) "Grafana passwords will be stored as plaintext in the Nix store!";
247319
248320 environment.systemPackages = [ cfg.package ];
249321
322+ assertions = [
323+ {
324+ assertion = cfg.database.password != opt.database.password.default -> cfg.database.passwordFile == null;
325+ message = "Cannot set both password and passwordFile";
326+ }
327+ {
328+ assertion = cfg.security.adminPassword != opt.security.adminPassword.default -> cfg.security.adminPasswordFile == null;
329+ message = "Cannot set both adminPassword and adminPasswordFile";
330+ }
331+ {
332+ assertion = cfg.security.secretKeyFile != opt.security.secretKeyFile.default -> cfg.security.secretKeyFile == null;
333+ message = "Cannot set both secretKey and secretKeyFile";
334+ }
335+ {
336+ assertion = cfg.smtp.password != opt.smtp.password.default -> cfg.smtp.passwordFile == null;
337+ message = "Cannot set both password and secretKeyFile";
338+ }
339+ ];
340+
250341 systemd.services.grafana = {
251342 description = "Grafana Service Daemon";
252343 wantedBy = ["multi-user.target"];
@@ -254,8 +345,22 @@ in {
254345 environment = {
255346 QT_QPA_PLATFORM = "offscreen";
256347 } // mapAttrs' (n: v: nameValuePair "GF_${n}" (toString v)) envOptions;
348+ script = ''
349+ ${optionalString (cfg.database.passwordFile != null) ''
350+ export GF_DATABASE_PASSWORD="$(cat ${escapeShellArg cfg.database.passwordFile})"
351+ ''}
352+ ${optionalString (cfg.security.adminPasswordFile != null) ''
353+ export GF_SECURITY_ADMIN_PASSWORD="$(cat ${escapeShellArg cfg.security.adminPasswordFile})"
354+ ''}
355+ ${optionalString (cfg.security.secretKeyFile != null) ''
356+ export GF_SECURITY_SECRET_KEY="$(cat ${escapeShellArg cfg.security.secretKeyFile})"
357+ ''}
358+ ${optionalString (cfg.smtp.passwordFile != null) ''
359+ export GF_SMTP_PASSWORD="$(cat ${escapeShellArg cfg.smtp.passwordFile})"
360+ ''}
361+ exec ${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}
362+ '';
257363 serviceConfig = {
258− ExecStart = "${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}";
259364 WorkingDirectory = cfg.dataDir;
260365 User = "grafana";
261366 };