From cb0ae89c6c220b8a6098b587f4660b3ea94d5f32 Mon Sep 17 00:00:00 2001 From: John Lotoski Date: Tue, 16 Jan 2024 20:55:34 -0600 Subject: [PATCH] postgres: add a config.socketDir option * Defaults to config.dataDir * Allows customization of socketDir for users who do not wish it fixed to dataDir * Sockets are limited to around [100 chars](https://linux.die.net/man/7/unix): * In the case that the default or desired dataDir absolute path exceeds this char limit, postgres will fail to start if it is also used for socketDir * In this case, dataDir can remain as desired and socketDir can be set separately to a shorter absolute path --- nix/postgres/default.nix | 15 +++++++++++---- nix/postgres/setup-script.nix | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/nix/postgres/default.nix b/nix/postgres/default.nix index 30590720..4118c776 100644 --- a/nix/postgres/default.nix +++ b/nix/postgres/default.nix @@ -50,6 +50,12 @@ in description = "The DB data directory"; }; + socketDir = lib.mkOption { + type = lib.types.str; + default = config.dataDir; + description = "The DB socket directory"; + }; + hbaConf = let hbaConfSubmodule = lib.types.submodule { @@ -161,7 +167,7 @@ in default = { listen_addresses = config.listen_addresses; port = config.port; - unix_socket_directories = config.dataDir; + unix_socket_directories = config.socketDir; hba_file = "${config.hbaConfFile}"; }; }; @@ -181,7 +187,7 @@ in default = { listen_addresses = config.listen_addresses; port = config.port; - unix_socket_directories = lib.mkDefault config.dataDir; + unix_socket_directories = lib.mkDefault config.socketDir; hba_file = "${config.hbaConfFile}"; }; example = lib.literalExpression '' @@ -311,12 +317,13 @@ in text = '' set -euo pipefail PGDATA=$(readlink -f "${config.dataDir}") + PGSOCKETDIR=$(readlink -f "${config.socketDir}") export PGDATA - postgres -k "$PGDATA" + postgres -k "$PGSOCKETDIR" ''; }; pg_isreadyArgs = [ - "-h $(readlink -f ${config.dataDir})" + "-h $(readlink -f \"${config.socketDir}\")" "-p ${toString config.port}" "-d template1" ] ++ (lib.optional (config.superuser != null) "-U ${config.superuser}"); diff --git a/nix/postgres/setup-script.nix b/nix/postgres/setup-script.nix index 06f0e2c4..94928007 100644 --- a/nix/postgres/setup-script.nix +++ b/nix/postgres/setup-script.nix @@ -100,7 +100,7 @@ in echo echo "PostgreSQL is setting up the initial database." echo - PGHOST=$(mktemp -d "$(readlink -f ${config.dataDir})/pg-init-XXXXXX") + PGHOST=$(mkdir -p "${config.socketDir}" && mktemp -d "$(readlink -f "${config.socketDir}")/pg-init-XXXXXX") export PGHOST function remove_tmp_pg_init_sock_dir() {