Merge pull request #47070 from Mic92/grafana-improvements
Grafana: secrets outside of the nix store + smtp
This commit is contained in:
commit
c4a7ebb46b
@ -4,6 +4,7 @@ with lib;
|
|||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.services.grafana;
|
cfg = config.services.grafana;
|
||||||
|
opt = options.services.grafana;
|
||||||
|
|
||||||
envOptions = {
|
envOptions = {
|
||||||
PATHS_DATA = cfg.dataDir;
|
PATHS_DATA = cfg.dataDir;
|
||||||
@ -41,6 +42,12 @@ let
|
|||||||
AUTH_ANONYMOUS_ORG_ROLE = cfg.auth.anonymous.org_role;
|
AUTH_ANONYMOUS_ORG_ROLE = cfg.auth.anonymous.org_role;
|
||||||
|
|
||||||
ANALYTICS_REPORTING_ENABLED = boolToString cfg.analytics.reporting.enable;
|
ANALYTICS_REPORTING_ENABLED = boolToString cfg.analytics.reporting.enable;
|
||||||
|
|
||||||
|
SMTP_ENABLE = boolToString cfg.smtp.enable;
|
||||||
|
SMTP_HOST = cfg.smtp.host;
|
||||||
|
SMTP_USER = cfg.smtp.user;
|
||||||
|
SMTP_PASSWORD = cfg.smtp.password;
|
||||||
|
SMTP_FROM_ADDRESS = cfg.smtp.fromAddress;
|
||||||
} // cfg.extraOptions;
|
} // cfg.extraOptions;
|
||||||
|
|
||||||
in {
|
in {
|
||||||
@ -134,11 +141,23 @@ in {
|
|||||||
};
|
};
|
||||||
|
|
||||||
password = mkOption {
|
password = mkOption {
|
||||||
description = "Database password.";
|
description = ''
|
||||||
|
Database password.
|
||||||
|
This option is mutual exclusive with the passwordFile option.
|
||||||
|
'';
|
||||||
default = "";
|
default = "";
|
||||||
type = types.str;
|
type = types.str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
passwordFile = mkOption {
|
||||||
|
description = ''
|
||||||
|
File that containts the database password.
|
||||||
|
This option is mutual exclusive with the password option.
|
||||||
|
'';
|
||||||
|
default = null;
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
};
|
||||||
|
|
||||||
path = mkOption {
|
path = mkOption {
|
||||||
description = "Database path.";
|
description = "Database path.";
|
||||||
default = "${cfg.dataDir}/data/grafana.db";
|
default = "${cfg.dataDir}/data/grafana.db";
|
||||||
@ -163,16 +182,69 @@ in {
|
|||||||
};
|
};
|
||||||
|
|
||||||
adminPassword = mkOption {
|
adminPassword = mkOption {
|
||||||
description = "Default admin password.";
|
description = ''
|
||||||
|
Default admin password.
|
||||||
|
This option is mutual exclusive with the adminPasswordFile option.
|
||||||
|
'';
|
||||||
default = "admin";
|
default = "admin";
|
||||||
type = types.str;
|
type = types.str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
adminPasswordFile = mkOption {
|
||||||
|
description = ''
|
||||||
|
Default admin password.
|
||||||
|
This option is mutual exclusive with the <literal>adminPassword</literal> option.
|
||||||
|
'';
|
||||||
|
default = null;
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
};
|
||||||
|
|
||||||
secretKey = mkOption {
|
secretKey = mkOption {
|
||||||
description = "Secret key used for signing.";
|
description = "Secret key used for signing.";
|
||||||
default = "SW2YcwTIb9zpOOhoPsMm";
|
default = "SW2YcwTIb9zpOOhoPsMm";
|
||||||
type = types.str;
|
type = types.str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
secretKeyFile = mkOption {
|
||||||
|
description = "Secret key used for signing.";
|
||||||
|
default = null;
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
smtp = {
|
||||||
|
enable = mkEnableOption "smtp";
|
||||||
|
host = mkOption {
|
||||||
|
description = "Host to connect to";
|
||||||
|
default = "localhost:25";
|
||||||
|
type = types.str;
|
||||||
|
};
|
||||||
|
user = mkOption {
|
||||||
|
description = "User used for authentication";
|
||||||
|
default = "";
|
||||||
|
type = types.str;
|
||||||
|
};
|
||||||
|
password = mkOption {
|
||||||
|
description = ''
|
||||||
|
Password used for authentication.
|
||||||
|
This option is mutual exclusive with the passwordFile option.
|
||||||
|
'';
|
||||||
|
default = "";
|
||||||
|
type = types.str;
|
||||||
|
};
|
||||||
|
passwordFile = mkOption {
|
||||||
|
description = ''
|
||||||
|
Password used for authentication.
|
||||||
|
This option is mutual exclusive with the password option.
|
||||||
|
'';
|
||||||
|
default = null;
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
};
|
||||||
|
fromAddress = mkOption {
|
||||||
|
description = "Email address used for sending";
|
||||||
|
default = "admin@grafana.localhost";
|
||||||
|
type = types.str;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
users = {
|
users = {
|
||||||
@ -241,12 +313,31 @@ in {
|
|||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
warnings = optional (
|
warnings = optional (
|
||||||
cfg.database.password != options.services.grafana.database.password.default ||
|
cfg.database.password != opt.database.password.default ||
|
||||||
cfg.security.adminPassword != options.services.grafana.security.adminPassword.default
|
cfg.security.adminPassword != opt.security.adminPassword.default
|
||||||
) "Grafana passwords will be stored as plaintext in the Nix store!";
|
) "Grafana passwords will be stored as plaintext in the Nix store!";
|
||||||
|
|
||||||
environment.systemPackages = [ cfg.package ];
|
environment.systemPackages = [ cfg.package ];
|
||||||
|
|
||||||
|
assertions = [
|
||||||
|
{
|
||||||
|
assertion = cfg.database.password != opt.database.password.default -> cfg.database.passwordFile == null;
|
||||||
|
message = "Cannot set both password and passwordFile";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = cfg.security.adminPassword != opt.security.adminPassword.default -> cfg.security.adminPasswordFile == null;
|
||||||
|
message = "Cannot set both adminPassword and adminPasswordFile";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = cfg.security.secretKeyFile != opt.security.secretKeyFile.default -> cfg.security.secretKeyFile == null;
|
||||||
|
message = "Cannot set both secretKey and secretKeyFile";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = cfg.smtp.password != opt.smtp.password.default -> cfg.smtp.passwordFile == null;
|
||||||
|
message = "Cannot set both password and secretKeyFile";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
systemd.services.grafana = {
|
systemd.services.grafana = {
|
||||||
description = "Grafana Service Daemon";
|
description = "Grafana Service Daemon";
|
||||||
wantedBy = ["multi-user.target"];
|
wantedBy = ["multi-user.target"];
|
||||||
@ -254,8 +345,22 @@ in {
|
|||||||
environment = {
|
environment = {
|
||||||
QT_QPA_PLATFORM = "offscreen";
|
QT_QPA_PLATFORM = "offscreen";
|
||||||
} // mapAttrs' (n: v: nameValuePair "GF_${n}" (toString v)) envOptions;
|
} // mapAttrs' (n: v: nameValuePair "GF_${n}" (toString v)) envOptions;
|
||||||
|
script = ''
|
||||||
|
${optionalString (cfg.database.passwordFile != null) ''
|
||||||
|
export GF_DATABASE_PASSWORD="$(cat ${escapeShellArg cfg.database.passwordFile})"
|
||||||
|
''}
|
||||||
|
${optionalString (cfg.security.adminPasswordFile != null) ''
|
||||||
|
export GF_SECURITY_ADMIN_PASSWORD="$(cat ${escapeShellArg cfg.security.adminPasswordFile})"
|
||||||
|
''}
|
||||||
|
${optionalString (cfg.security.secretKeyFile != null) ''
|
||||||
|
export GF_SECURITY_SECRET_KEY="$(cat ${escapeShellArg cfg.security.secretKeyFile})"
|
||||||
|
''}
|
||||||
|
${optionalString (cfg.smtp.passwordFile != null) ''
|
||||||
|
export GF_SMTP_PASSWORD="$(cat ${escapeShellArg cfg.smtp.passwordFile})"
|
||||||
|
''}
|
||||||
|
exec ${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}
|
||||||
|
'';
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = "${cfg.package.bin}/bin/grafana-server -homepath ${cfg.dataDir}";
|
|
||||||
WorkingDirectory = cfg.dataDir;
|
WorkingDirectory = cfg.dataDir;
|
||||||
User = "grafana";
|
User = "grafana";
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user