feat(postgres): add superuser option (#29)

This commit is contained in:
Adrian 2023-07-21 23:12:04 +01:00 committed by GitHub
parent 4da1956dd8
commit 1209368ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,6 +106,15 @@ in
'';
};
superuser = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Name of superuser.
null defaults to $USER
'';
};
createDatabase = lib.mkOption {
type = types.bool;
default = true;
@ -332,13 +341,17 @@ in
configFile = pkgs.writeText "postgresql.conf" (lib.concatStringsSep "\n"
(lib.mapAttrsToList (n: v: "${n} = ${toStr v}") (config.defaultSettings // config.settings)));
initdbArgs =
config.initdbArgs
++ (lib.optionals (config.superuser != null) ["-U" config.superuser]);
setupScript = pkgs.writeShellScriptBin "setup-postgres" ''
set -euo pipefail
export PATH=${postgresPkg}/bin:${pkgs.coreutils}/bin
if [[ ! -d "$PGDATA" ]]; then
set -x
initdb ${lib.concatStringsSep " " config.initdbArgs}
initdb ${lib.concatStringsSep " " initdbArgs}
set +x
${runInitialScript.before}
@ -364,13 +377,18 @@ in
startScript = pkgs.writeShellApplication {
name = "start-postgres";
text = ''
set -x
export PATH="${postgresPkg}"/bin:$PATH
set -x
PGDATA=$(readlink -f "${config.dataDir}")
export PGDATA
postgres -k "$PGDATA"
'';
};
pg_isreadyArgs = [
"-h $(readlink -f ${config.dataDir})"
"-p ${toString config.port}"
"-d template1"
] ++ (lib.optional (config.superuser != null) "-U ${config.superuser}");
in
{
command = startScript;
@ -378,7 +396,7 @@ in
# SIGINT (= 2) for faster shutdown: https://www.postgresql.org/docs/current/server-shutdown.html
shutdown.signal = 2;
readiness_probe = {
exec.command = "${postgresPkg}/bin/pg_isready -h $(readlink -f ${config.dataDir}) -p ${toString config.port} -d template1";
exec.command = "${postgresPkg}/bin/pg_isready ${lib.concatStringsSep " " pg_isreadyArgs}";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;