Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 1d91f14

Browse files
committed
nix: refactor tools to use --subst-var-by
Summary: Easier to understand and makes the "what" these scripts are doing much more clear. Signed-off-by: Austin Seipp <[email protected]> Change-Id: I5d1c56564e742a473a2379d9af120efc
1 parent d3e0c39 commit 1d91f14

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

flake.nix

+15-14
Original file line numberDiff line numberDiff line change
@@ -217,36 +217,37 @@
217217
# Start a version of the server.
218218
start-server = pkgs.runCommand "start-postgres-server" {} ''
219219
mkdir -p $out/bin
220-
substitute ${./tools/run-server.sh} $out/bin/start-postgres-server \
221-
--replace 'PSQL14=' 'PSQL14=${basePackages.psql_14.bin} #' \
222-
--replace 'PSQL15=' 'PSQL15=${basePackages.psql_15.bin} #'
220+
substitute ${./tools/run-server.sh.in} $out/bin/start-postgres-server \
221+
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
222+
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
223223
chmod +x $out/bin/start-postgres-server
224224
'';
225225

226226
# Start a version of the client.
227227
start-client = pkgs.runCommand "start-postgres-client" {} ''
228228
mkdir -p $out/bin
229-
substitute ${./tools/run-client.sh} $out/bin/start-postgres-client \
230-
--replace 'PSQL14=' 'PSQL14=${basePackages.psql_14.bin} #'
229+
substitute ${./tools/run-client.sh.in} $out/bin/start-postgres-client \
230+
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
231+
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
231232
chmod +x $out/bin/start-postgres-client
232233
'';
233234

234235
# Migrate between two data directories.
235236
migrate-tool =
236237
let
237-
configFile = ./tests/postgresql.conf;
238+
configFile = ./tests/postgresql.conf.in;
238239
getkeyScript = ./tests/util/pgsodium_getkey.sh;
239240
primingScript = ./tests/prime.sql;
240241
migrationData = ./tests/migrations/data.sql;
241242
in pkgs.runCommand "migrate-postgres" {} ''
242243
mkdir -p $out/bin
243-
substitute ${./tools/migrate-tool.sh} $out/bin/migrate-postgres \
244-
--replace 'PSQL14=' 'PSQL14=${basePackages.psql_14.bin} #' \
245-
--replace 'PSQL15=' 'PSQL15=${basePackages.psql_15.bin} #' \
246-
--replace 'PSQL_CONF_FILE=' 'PSQL_CONF_FILE=${configFile} #' \
247-
--replace 'PGSODIUM_GETKEY_SCRIPT=' 'PGSODIUM_GETKEY_SCRIPT=${getkeyScript} #' \
248-
--replace 'PRIMING_SCRIPT=' 'PRIMING_SCRIPT=${primingScript} #' \
249-
--replace 'MIGRATION_DATA=' 'MIGRATION_DATA=${migrationData} #'
244+
substitute ${./tools/migrate-tool.sh.in} $out/bin/migrate-postgres \
245+
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
246+
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
247+
--subst-var-by 'PSQL_CONF_FILE' '${configFile}' \
248+
--subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
249+
--subst-var-by 'PRIMING_SCRIPT' '${primingScript}' \
250+
--subst-var-by 'MIGRATION_DATA' '${migrationData}'
250251
251252
chmod +x $out/bin/migrate-postgres
252253
'';
@@ -264,7 +265,7 @@
264265
mkdir -p $PGDATA
265266
initdb --locale=C
266267
267-
substitute ${./tests/postgresql.conf} $PGDATA/postgresql.conf \
268+
substitute ${./tests/postgresql.conf.in} $PGDATA/postgresql.conf \
268269
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${./tests/util/pgsodium_getkey.sh}"
269270
270271
postgres -k /tmp >logfile 2>&1 &
File renamed without changes.

tools/migrate-tool.sh renamed to tools/migrate-tool.sh.in

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ if [[ $1 == /nix/store* ]]; then
1010
fi
1111
OLDVER="$1"
1212
elif [ "$1" == "14" ]; then
13-
PSQL14=$(nix build --quiet --no-link --print-out-paths .#"psql_14/bin")
13+
PSQL14=@PSQL14_BINDIR@
1414
OLDVER="$PSQL14"
1515
elif [ "$1" == "15" ]; then
16-
PSQL15=$(nix build --quiet --no-link --print-out-paths .#"psql_15/bin")
16+
PSQL15=@PSQL15_BINDIR@
1717
OLDVER="$PSQL15"
1818
else
1919
echo "Please provide a valid Postgres version (14 or 15), or a /nix/store path"
@@ -28,10 +28,10 @@ if [[ $2 == /nix/store* ]]; then
2828
fi
2929
NEWVER="$2"
3030
elif [ "$2" == "14" ]; then
31-
PSQL14=$(nix build --quiet --no-link --print-out-paths .#"psql_14/bin")
31+
PSQL14=@PSQL14_BINDIR@
3232
NEWVER="$PSQL14"
3333
elif [ "$2" == "15" ]; then
34-
PSQL15=$(nix build --quiet --no-link --print-out-paths .#"psql_15/bin")
34+
PSQL15=@PSQL15_BINDIR@
3535
NEWVER="$PSQL15"
3636
else
3737
echo "Please provide a valid Postgres version (14 or 15), or a /nix/store path"
@@ -62,8 +62,8 @@ $OLDVER/bin/initdb -D "$DATDIR" --locale=C
6262
$NEWVER/bin/initdb -D "$NEWDAT" --locale=C
6363

6464
# NOTE (aseipp): we need to patch postgresql.conf to have the right pgsodium_getkey script
65-
PSQL_CONF_FILE=$PWD/tests/postgresql.conf
66-
PGSODIUM_GETKEY_SCRIPT=$PWD/tests/util/pgsodium_getkey.sh
65+
PSQL_CONF_FILE=@PSQL_CONF_FILE@
66+
PGSODIUM_GETKEY_SCRIPT=@PGSODIUM_GETKEY@
6767
echo "NOTE: patching postgresql.conf files"
6868
for x in "$DATDIR" "$NEWDAT"; do
6969
sed \
@@ -74,8 +74,8 @@ done
7474
echo "NOTE: Starting first server (v${1}) to load data into the system"
7575
$OLDVER/bin/pg_ctl start -D "$DATDIR"
7676

77-
PRIMING_SCRIPT=$PWD/tests/prime.sql
78-
MIGRATION_DATA=$PWD/tests/migrations/data.sql
77+
PRIMING_SCRIPT=@PRIMING_SCRIPT@
78+
MIGRATION_DATA=@MIGRATION_DATA@
7979

8080
$OLDVER/bin/psql -h localhost -d postgres -Xf "$PRIMING_SCRIPT"
8181
$OLDVER/bin/psql -h localhost -d postgres -Xf "$MIGRATION_DATA"

tools/run-client.sh renamed to tools/run-client.sh.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# first argument should be '14' or '15' for the version
77
if [ "$1" == "14" ]; then
88
echo "Starting server for PSQL 14"
9-
PSQL14=$(nix build --quiet --no-link --print-out-paths .#"psql_14/bin")
9+
PSQL14=@PSQL14_BINDIR@
1010
BINDIR="$PSQL14"
1111
elif [ "$1" == "15" ]; then
1212
echo "Starting server for PSQL 15"
13-
PSQL15=$(nix build --quiet --no-link --print-out-paths .#"psql_15/bin")
13+
PSQL15=@PSQL15_BINDIR@
1414
BINDIR="$PSQL15"
1515
else
1616
echo "Please provide a valid Postgres version (14 or 15)"

tools/run-server.sh renamed to tools/run-server.sh.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# first argument should be '14' or '15' for the version
77
if [ "$1" == "14" ]; then
88
echo "Starting server for PSQL 14"
9-
PSQL14=$(nix build --quiet --no-link --print-out-paths .#"psql_14/bin")
9+
PSQL14=@PSQL14_BINDIR@
1010
BINDIR="$PSQL14"
1111
elif [ "$1" == "15" ]; then
1212
echo "Starting server for PSQL 15"
13-
PSQL15=$(nix build --quiet --no-link --print-out-paths .#"psql_15/bin")
13+
PSQL15=@PSQL15_BINDIR@
1414
BINDIR="$PSQL15"
1515
else
1616
echo "Please provide a valid Postgres version (14 or 15)"

0 commit comments

Comments
 (0)