nixos/wakapi: add database options; gate db creation behind database.createLocally (#341176)

This commit is contained in:
Christina Sørensen 2024-10-18 18:04:46 +02:00 committed by GitHub
commit d218858bb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -66,6 +66,60 @@ in
The path to a file containing the password for the smtp mailer used by Wakapi.
'';
};
database = {
createLocally = mkEnableOption ''
automatic database configuration.
::: {.note}
Only PostgreSQL is supported for the time being.
:::
'';
dialect = mkOption {
type = types.nullOr (
types.enum [
"postgres"
"sqlite3"
"mysql"
"cockroach"
"mssql"
]
);
default = cfg.settings.db.dialect or null; # handle case where dialect is not set
defaultText = ''
Database dialect from settings if {option}`services.wakatime.settings.db.dialect`
is set, or `null` otherwise.
'';
description = ''
The database type to use for Wakapi.
'';
};
name = mkOption {
type = types.str;
default = cfg.settings.db.name or "wakapi";
defaultText = ''
Database name from settings if {option}`services.wakatime.settings.db.name`
is set, or "wakapi" otherwise.
'';
description = ''
The name of the database to use for Wakapi.
'';
};
user = mkOption {
type = types.str;
default = cfg.settings.db.user or "wakapi";
defaultText = ''
User from settings if {option}`services.wakatime.settings.db.user`
is set, or "wakapi" otherwise.
'';
description = ''
The name of the user to use for Wakapi.
'';
};
};
};
config = mkIf cfg.enable {
@ -73,10 +127,10 @@ in
description = "Wakapi (self-hosted WakaTime-compatible backend)";
wants = [
"network-online.target"
] ++ optional (cfg.settings.db.dialect == "postgres") "postgresql.service";
] ++ optional (cfg.database.dialect == "postgres") "postgresql.service";
after = [
"network-online.target"
] ++ optional (cfg.settings.db.dialect == "postgres") "postgresql.service";
] ++ optional (cfg.database.dialect == "postgres") "postgresql.service";
wantedBy = [ "multi-user.target" ];
script = ''
@ -135,6 +189,19 @@ in
assertion = cfg.smtpPassword != null -> cfg.smtpPasswordFile != null;
message = "Both `services.wakapi.smtpPassword` `services.wakapi.smtpPasswordFile` should not be set at the same time.";
}
{
assertion = cfg.db.createLocally -> cfg.db.dialect != null;
message = "`services.wakapi.database.createLocally` is true, but a database dialect is not set!";
}
];
warnings = [
(lib.optionalString (cfg.db.createLocall -> cfg.db.dialect != "postgres") ''
You have enabled automatic database configuration, but the database dialect is not set to "posgres".
The Wakapi module only supports for PostgreSQL. Please set `services.wakapi.database.createLocally`
to `false`, or switch to "postgres" as your database dialect.
'')
];
users = {
@ -146,10 +213,10 @@ in
groups.wakapi = { };
};
services.postgresql = mkIf (cfg.settings.db.dialect == "postgres") {
services.postgresql = mkIf (cfg.database.createLocally && cfg.database.dialect == "postgres") {
enable = true;
ensureDatabases = singleton cfg.settings.db.name;
ensureDatabases = singleton cfg.database.name;
ensureUsers = singleton {
name = cfg.settings.db.user;
ensureDBOwnership = true;
@ -161,5 +228,8 @@ in
};
};
meta.maintainers = with lib.maintainers; [ isabelroses ];
meta.maintainers = with lib.maintainers; [
isabelroses
NotAShelf
];
}