7
7
postgresql ,
8
8
libuv ,
9
9
writeShellApplication ,
10
+ makeWrapper ,
10
11
} :
11
12
12
13
let
14
+ enableOverlayOnPackage = writeShellApplication {
15
+ name = "enable_overlay_on_package" ;
16
+ runtimeInputs = [ pkgs . coreutils ] ;
17
+ text = ''
18
+ # This script enable overlayfs on a specific nix store path
19
+ set -euo pipefail
20
+
21
+ if [ $# -ne 1 ]; then
22
+ echo "Usage: $0 <path>"
23
+ exit 1
24
+ fi
25
+
26
+ PACKAGE_PATH="$1"
27
+ PACKAGE_NAME=$(basename "$1"|cut -c 34-)
28
+
29
+ # Nixos compatibility: use systemd mount unit
30
+ #shellcheck disable=SC1091
31
+ source /etc/os-release || true
32
+ if [[ "$ID" == "nixos" ]]; then
33
+ # This script is used in NixOS test only for the moment
34
+ SYSTEMD_DIR="/run/systemd/system"
35
+ else
36
+ SYSTEMD_DIR="/etc/systemd/system"
37
+ fi
38
+
39
+ # Create required directories for overlay
40
+ echo "$PACKAGE_NAME"
41
+ mkdir -p "/var/lib/overlay/$PACKAGE_NAME/"{upper,work}
42
+
43
+ PACKAGE_MOUNT_PATH=$(systemd-escape -p --suffix=mount "$PACKAGE_PATH")
44
+
45
+ cat > "$SYSTEMD_DIR/$PACKAGE_MOUNT_PATH" <<EOF
46
+ [Unit]
47
+ Description=Overlay mount for PostgreSQL extension $PACKAGE_NAME
48
+
49
+ [Mount]
50
+ What=overlay
51
+ Type=overlay
52
+ Options=lowerdir=$PACKAGE_PATH,upperdir=/var/lib/overlay/$PACKAGE_NAME/upper,workdir=/var/lib/overlay/$PACKAGE_NAME/work
53
+
54
+ [Install]
55
+ WantedBy=multi-user.target
56
+ EOF
57
+ systemctl daemon-reload
58
+ systemctl start "$PACKAGE_MOUNT_PATH"
59
+ '' ;
60
+ } ;
13
61
switchPgNetVersion = writeShellApplication {
14
62
name = "switch_pg_net_version" ;
15
63
runtimeInputs = [ pkgs . coreutils ] ;
16
64
text = ''
17
65
# Create version switcher script
18
- set -e
66
+ set -euo pipefail
67
+
68
+ # Check if the required environment variables are set
69
+ if [ -z "'' ${EXT_WRAPPER:-}" ]; then
70
+ echo "Error: EXT_WRAPPER environment variable is not set."
71
+ exit 1
72
+ fi
19
73
20
74
if [ $# -ne 1 ]; then
21
75
echo "Usage: $0 <version>"
28
82
exit 1
29
83
fi
30
84
31
- VERSION=$1
32
-
33
- # Set defaults, allow environment variable overrides
34
- : "'' ${NIX_PROFILE:="/var/lib/postgresql/.nix-profile"}"
35
- : "'' ${LIB_DIR:=""}"
36
- : "'' ${EXTENSION_DIR:=""}"
37
-
38
- # If LIB_DIR not explicitly set, auto-detect it
39
- if [ -z "$LIB_DIR" ]; then
40
- # Follow the complete chain of symlinks to find the multi-version directory
41
- CURRENT_LINK="$NIX_PROFILE/lib/pg_net-$VERSION${ postgresql . dlSuffix } "
42
- echo "Starting with link: $CURRENT_LINK"
43
-
44
- # Follow first two symlinks to get to the multi-version directory
45
- for _ in 1 2; do
46
- if [ -L "$CURRENT_LINK" ]; then
47
- NEXT_LINK=$(readlink "$CURRENT_LINK")
48
- echo "Following link: $NEXT_LINK"
49
- if echo "$NEXT_LINK" | grep -q '^/'; then
50
- CURRENT_LINK="$NEXT_LINK"
51
- else
52
- CURRENT_LINK="$(dirname "$CURRENT_LINK")/$NEXT_LINK"
53
- fi
54
- echo "Current link is now: $CURRENT_LINK"
55
- fi
56
- done
57
-
58
- # The multi-version directory should be the parent of the current link
59
- MULTI_VERSION_DIR=$(dirname "$CURRENT_LINK")
60
- echo "Found multi-version directory: $MULTI_VERSION_DIR"
61
- LIB_DIR="$MULTI_VERSION_DIR"
62
- else
63
- echo "Using provided LIB_DIR: $LIB_DIR"
64
- fi
65
-
66
- # If EXTENSION_DIR not explicitly set, use default
67
- if [ -z "$EXTENSION_DIR" ]; then
68
- EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
69
- fi
70
- echo "Using EXTENSION_DIR: $EXTENSION_DIR"
85
+ VERSION="$1"
86
+ echo "$VERSION"
71
87
72
- echo "Looking for file: $LIB_DIR/pg_net-$VERSION ${ postgresql . dlSuffix } "
73
- ls -la "$LIB_DIR" || true
88
+ # Enable overlay on the wrapper package to be able to switch version
89
+ ${ lib . getExe enableOverlayOnPackage } "$EXT_WRAPPER"
74
90
75
91
# Check if version exists
76
- if [ ! -f "$LIB_DIR/pg_net-$VERSION${ postgresql . dlSuffix } " ]; then
77
- echo "Error: Version $VERSION not found in $LIB_DIR"
92
+ EXT_WRAPPER_LIB="$EXT_WRAPPER/lib"
93
+ PG_NET_LIB_TO_USE="$EXT_WRAPPER_LIB/pg_net-$VERSION${ postgresql . dlSuffix } "
94
+ if [ ! -f "$PG_NET_LIB_TO_USE" ]; then
95
+ echo "Error: Version $VERSION not found in $EXT_WRAPPER_LIB"
78
96
echo "Available versions:"
79
97
#shellcheck disable=SC2012
80
- ls "$LIB_DIR "/pg_net-*${ postgresql . dlSuffix } 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${ postgresql . dlSuffix } $//' || echo " No versions found"
98
+ ls "$EXT_WRAPPER_LIB "/pg_net-*${ postgresql . dlSuffix } 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${ postgresql . dlSuffix } $//' || echo " No versions found"
81
99
exit 1
82
100
fi
83
101
84
102
# Update library symlink
85
- ln -sfnv "pg_net-$VERSION ${ postgresql . dlSuffix } " "$LIB_DIR /pg_net${ postgresql . dlSuffix } "
103
+ ln -sfnv "$PG_NET_LIB_TO_USE " "$EXT_WRAPPER_LIB /pg_net${ postgresql . dlSuffix } "
86
104
87
105
# Update control file
88
- echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_net.control"
89
- cat "$EXTENSION_DIR/pg_net--$VERSION.control" >> "$EXTENSION_DIR/pg_net.control"
106
+ EXT_WRAPPER_SHARE="$EXT_WRAPPER/share/postgresql/extension"
107
+ echo "default_version = '$VERSION'" > "$EXT_WRAPPER_SHARE/pg_net.control"
108
+ cat "$EXT_WRAPPER_SHARE/pg_net--$VERSION.control" >> "$EXT_WRAPPER_SHARE/pg_net.control"
90
109
91
110
echo "Successfully switched pg_net to version $VERSION"
92
- EOF
93
111
'' ;
94
112
} ;
95
113
pname = "pg_net" ;
175
193
in
176
194
pkgs . buildEnv {
177
195
name = pname ;
178
- paths = packages ++ [ switchPgNetVersion ] ;
196
+ paths = packages ;
197
+ nativeBuildInputs = [ makeWrapper ] ;
179
198
postBuild = ''
180
199
{
181
200
echo "default_version = '${ latestVersion } '"
@@ -190,6 +209,9 @@ pkgs.buildEnv {
190
209
toString ( numberOfVersions + 1 )
191
210
} "
192
211
)
212
+
213
+ makeWrapper ${ lib . getExe switchPgNetVersion } $out/bin/switch_pg_net_version \
214
+ --prefix EXT_WRAPPER : "$out"
193
215
'' ;
194
216
195
217
passthru = {
0 commit comments