Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: add socketDir option #77

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions nix/postgres/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}";
};
};
Expand All @@ -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 ''
Expand Down Expand Up @@ -292,12 +298,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}");
Expand Down
7 changes: 3 additions & 4 deletions nix/postgres/setup-script.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{ config, pkgs, lib }:
let
setupInitialSchema = dbName: schema: ''
${lib.optionalString (schema != null) ''
echo "Applying database schema on ${dbName}"
if [ -f "${schema}" ]
then
Expand All @@ -20,7 +19,6 @@ let
echo "ERROR: Could not determine how to apply schema with ${schema}"
exit 1
fi
''}
'';
setupInitialDatabases =
if config.initialDatabases != [ ] then
Expand All @@ -37,7 +35,8 @@ let
if [ 1 -ne "$dbAlreadyExists" ]; then
echo "Creating database: ${database.name}"
echo 'create database "${database.name}";' | psql -d postgres
${lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas}
${lib.optionalString (database.schemas != null)
(lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas)}
fi
'')
config.initialDatabases)
Expand Down Expand Up @@ -101,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=$(mktemp -d "$(readlink -f "${config.socketDir}")/pg-init-XXXXXX")
export PGHOST

function remove_tmp_pg_init_sock_dir() {
Expand Down