From 6b206d7c98fbbefa96d52989af4f1bd09fa0c559 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Fri, 22 Aug 2025 21:54:15 +0200 Subject: [PATCH] `modules/services/postgresql.nix`: add `startScript`, `earlyStartScript` options Signed-off-by: magic_rb --- modules/services/postgresql.nix | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/services/postgresql.nix b/modules/services/postgresql.nix index e090401..aa75d95 100644 --- a/modules/services/postgresql.nix +++ b/modules/services/postgresql.nix @@ -129,6 +129,23 @@ in ''; }; + startScript = lib.mkOption { + type = lib.types.lines; + description = '' + PostgresSQL script to run on every startup. + ''; + default = ""; + }; + + earlyStartScript = lib.mkOption { + type = lib.types.lines; + description = '' + PostgresSQL script to run on every startup, very early. + ''; + default = ""; + }; + + ensureExtensions = lib.mkOption { type = with lib.types; attrsOf (listOf str); default = { }; @@ -405,12 +422,14 @@ in chpst -u postgres:postgres ${cfg.package}/bin/postgres & postgresql=$! - PSQL="chpst -u postgres:postgres ${cfg.package}/bin/psql --port=${cfg.port}" + PSQL="chpst -u postgres:postgres ${cfg.package}/bin/psql --port=${cfg.port} -X" while ! $PSQL -d postgres -c "" 2> /dev/null ; do if ! kill -0 "$postgresql"; then exit 1; fi sleep 0.1 done + $PSQL -f "${pkgs.writeText "postgres-start-early.sql" cfg.earlyStartScript}" + ${lib.concatMapStrings ( { database, options }: @@ -465,6 +484,8 @@ in rm -f "${cfg.dataDir}/.first_startup" fi + $PSQL -f "${pkgs.writeText "postgres-start.sql" cfg.startScript}" + wait $postgresql ''; # END Copyright (c) 2003-2021 Eelco Dolstra and the Nixpkgs/NixOS contributors