2023-11-05 04:30:17 +01:00
|
|
|
{ config, lib, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.shb.postgresql;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.shb.postgresql = {
|
2023-11-14 09:16:29 +01:00
|
|
|
debug = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2023-11-30 21:06:41 +01:00
|
|
|
description = ''
|
2023-11-14 09:16:29 +01:00
|
|
|
Enable debugging options.
|
|
|
|
|
|
|
|
Currently enables shared_preload_libraries = "auto_explain, pg_stat_statements"
|
|
|
|
|
|
|
|
See https://www.postgresql.org/docs/current/pgstatstatements.html'';
|
|
|
|
default = false;
|
|
|
|
};
|
2023-11-23 10:03:33 +01:00
|
|
|
enableTCPIP = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2023-11-05 04:30:17 +01:00
|
|
|
description = "Enable TCP/IP connection on given port.";
|
2023-11-23 10:03:33 +01:00
|
|
|
default = false;
|
2023-11-05 04:30:17 +01:00
|
|
|
};
|
|
|
|
|
2023-11-23 10:03:33 +01:00
|
|
|
ensures = lib.mkOption {
|
2023-12-04 09:33:16 +01:00
|
|
|
description = "List of username, database and/or passwords that should be created.";
|
2023-11-05 04:30:17 +01:00
|
|
|
type = lib.types.listOf (lib.types.submodule {
|
|
|
|
options = {
|
|
|
|
username = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Postgres user name.";
|
|
|
|
};
|
|
|
|
|
|
|
|
database = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Postgres database.";
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = lib.mkOption {
|
2023-11-05 04:59:55 +01:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2023-11-23 10:03:33 +01:00
|
|
|
description = "Optional password file for the postgres user. If not given, only peer auth is accepted for this user, otherwise password auth is allowed.";
|
2023-11-05 04:59:55 +01:00
|
|
|
default = null;
|
|
|
|
example = "/run/secrets/postgresql/password";
|
2023-11-05 04:30:17 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
2023-11-24 11:16:49 +01:00
|
|
|
commonConfig = {
|
|
|
|
services.postgresql.settings = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-23 10:03:33 +01:00
|
|
|
tcpConfig = {
|
2023-11-05 04:30:17 +01:00
|
|
|
services.postgresql.enableTCPIP = true;
|
|
|
|
services.postgresql.authentication = lib.mkOverride 10 ''
|
|
|
|
#type database DBuser origin-address auth-method
|
2023-11-07 04:43:00 +01:00
|
|
|
local all all peer
|
2023-11-05 04:30:17 +01:00
|
|
|
# ipv4
|
2023-11-23 10:03:33 +01:00
|
|
|
host all all 127.0.0.1/32 password
|
2023-11-05 04:30:17 +01:00
|
|
|
# ipv6
|
2023-11-23 10:03:33 +01:00
|
|
|
host all all ::1/128 password
|
2023-11-05 04:30:17 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-11-23 10:03:33 +01:00
|
|
|
dbConfig = ensureCfgs: {
|
|
|
|
services.postgresql.enable = lib.mkDefault ((builtins.length ensureCfgs) > 0);
|
|
|
|
services.postgresql.ensureDatabases = map ({ database, ... }: database) ensureCfgs;
|
2023-11-05 13:48:39 +01:00
|
|
|
services.postgresql.ensureUsers = map ({ username, database, ... }: {
|
2023-11-05 04:30:17 +01:00
|
|
|
name = username;
|
2023-12-04 08:33:55 +01:00
|
|
|
ensureDBOwnership = true;
|
|
|
|
ensureClauses.login = true;
|
2023-11-23 10:03:33 +01:00
|
|
|
}) ensureCfgs;
|
2023-11-05 04:30:17 +01:00
|
|
|
};
|
|
|
|
|
2023-11-23 10:03:33 +01:00
|
|
|
pwdConfig = ensureCfgs: {
|
2023-11-05 04:30:17 +01:00
|
|
|
systemd.services.postgresql.postStart =
|
|
|
|
let
|
2023-11-06 00:42:14 +01:00
|
|
|
prefix = ''
|
|
|
|
$PSQL -tA <<'EOF'
|
|
|
|
DO $$
|
|
|
|
DECLARE password TEXT;
|
|
|
|
BEGIN
|
|
|
|
'';
|
|
|
|
suffix = ''
|
|
|
|
END $$;
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
exec = { username, passwordFile, ... }: ''
|
|
|
|
password := trim(both from replace(pg_read_file('${passwordFile}'), E'\n', '''));
|
|
|
|
EXECUTE format('ALTER ROLE ${username} WITH PASSWORD '''%s''';', password);
|
|
|
|
'';
|
2023-11-23 10:03:33 +01:00
|
|
|
cfgsWithPasswords = builtins.filter (cfg: cfg.passwordFile != null) ensureCfgs;
|
2023-11-05 04:30:17 +01:00
|
|
|
in
|
2023-11-06 00:42:14 +01:00
|
|
|
if (builtins.length cfgsWithPasswords) == 0 then "" else
|
|
|
|
prefix + (lib.concatStrings (map exec cfgsWithPasswords)) + suffix;
|
2023-11-05 04:30:17 +01:00
|
|
|
};
|
2023-11-14 09:16:29 +01:00
|
|
|
|
|
|
|
debugConfig = enableDebug: lib.mkIf enableDebug {
|
|
|
|
services.postgresql.settings.shared_preload_libraries = "auto_explain, pg_stat_statements";
|
|
|
|
};
|
2023-11-05 04:30:17 +01:00
|
|
|
in
|
|
|
|
lib.mkMerge (
|
|
|
|
[
|
2023-11-24 11:16:49 +01:00
|
|
|
commonConfig
|
2023-11-23 10:03:33 +01:00
|
|
|
(dbConfig cfg.ensures)
|
|
|
|
(pwdConfig cfg.ensures)
|
|
|
|
(lib.mkIf cfg.enableTCPIP tcpConfig)
|
2023-11-14 09:16:29 +01:00
|
|
|
(debugConfig cfg.debug)
|
2023-11-05 04:30:17 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|