@@ -134,11 +134,23 @@ in {
134134 };
135135
136136 password = mkOption {
137− description = "Database password.";
137+ description = ''
138+ Database password.
139+ This option is mutual exclusive with the passwordFile option.
140+ '';
138141 default = "";
139142 type = types.str;
140143 };
141144
145+ passwordFile = mkOption {
146+ description = ''
147+ File that containts the database password.
148+ This option is mutual exclusive with the password option.
149+ '';
150+ default = null;
151+ type = types.nullOr types.path;
152+ };
153+
142154 path = mkOption {
143155 description = "Database path.";
144156 default = "${cfg.dataDir}/data/grafana.db";
@@ -163,16 +175,34 @@ in {
163175 };
164176
165177 adminPassword = mkOption {
166− description = "Default admin password.";
178+ description = ''
179+ Default admin password.
180+ This option is mutual exclusive with the adminPasswordFile option.
181+ '';
167182 default = "admin";
168183 type = types.str;
169184 };
170185
186+ adminPasswordFile = mkOption {
187+ description = ''
188+ Default admin password.
189+ This option is mutual exclusive with the <literal>adminPassword</literal> option.
190+ '';
191+ default = null;
192+ type = types.nullOr types.path;
193+ };
194+
171195 secretKey = mkOption {
172196 description = "Secret key used for signing.";
173197 default = "SW2YcwTIb9zpOOhoPsMm";
174198 type = types.str;
175199 };
200+
201+ secretKeyFile = mkOption {
202+ description = "Secret key used for signing.";
203+ default = null;
204+ type = types.nullOr types.path;
205+ };
176206 };
177207
178208 users = {
@@ -247,6 +277,21 @@ in {
247277
248278 environment.systemPackages = [ cfg.package ];
249279
280+ assertions = [
281+ {
282+ assertion = cfg.database.password != opt.database.password.default -> cfg.database.passwordFile == null;
283+ message = "Cannot set both password and passwordFile";
284+ }
285+ {
286+ assertion = cfg.security.adminPassword != opt.security.adminPassword.default -> cfg.security.adminPasswordFile == null;
287+ message = "Cannot set both adminPassword and adminPasswordFile";
288+ }
289+ {
290+ assertion = cfg.security.secretKeyFile != opt.security.secretKeyFile.default -> cfg.security.secretKeyFile == null;
291+ message = "Cannot set both secretKey and secretKeyFile";
292+ }
293+ ];
294+
250295 systemd.services.grafana = {
251296 description = "Grafana Service Daemon";
252297 wantedBy = ["multi-user.target"];
@@ -254,8 +299,18 @@ in {
254299 environment = {
255300 QT_QPA_PLATFORM = "offscreen";
256301 } // mapAttrs' (n: v: nameValuePair "GF_${n}" (toString v)) envOptions;
302+ script = ''
303+ ${optionalString (cfg.database.passwordFile != null) ''
304+ export GF_DATABASE_PASSWORD="$(cat ${escapeShellArg cfg.database.passwordFile})"
305+ ''}
306+ ${optionalString (cfg.security.adminPasswordFile != null) ''
307+ export GF_SECURITY_ADMIN_PASSWORD="$(cat ${escapeShellArg cfg.security.adminPasswordFile})"
308+ ''}
309+ ${optionalString (cfg.security.secretKeyFile != null) ''
310+ export GF_SECURITY_SECRET_KEY="$(cat ${escapeShellArg cfg.security.secretKeyFile})"
311+ ''}
312+ exec ${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}
257313 serviceConfig = {
258− ExecStart = "${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}";
259314 WorkingDirectory = cfg.dataDir;
260315 User = "grafana";
261316 };