Skip to content

Commit 1d893bb

Browse files
committed
Move switch_pg_net_version script into separate package
1 parent 745dd57 commit 1d893bb

File tree

1 file changed

+85
-82
lines changed

1 file changed

+85
-82
lines changed

nix/ext/pg_net.nix

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,91 @@
66
curl,
77
postgresql,
88
libuv,
9+
writeShellApplication,
910
}:
1011

1112
let
13+
switchPgNetVersion = writeShellApplication {
14+
name = "switch_pg_net_version";
15+
runtimeInputs = [ pkgs.coreutils ];
16+
text = ''
17+
# Create version switcher script
18+
set -e
19+
20+
if [ $# -ne 1 ]; then
21+
echo "Usage: $0 <version>"
22+
echo "Example: $0 0.10.0"
23+
echo ""
24+
echo "Optional environment variables:"
25+
echo " NIX_PROFILE - Path to nix profile (default: /var/lib/postgresql/.nix-profile)"
26+
echo " LIB_DIR - Override library directory"
27+
echo " EXTENSION_DIR - Override extension directory"
28+
exit 1
29+
fi
30+
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 i 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"
71+
72+
echo "Looking for file: $LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}"
73+
ls -la "$LIB_DIR" || true
74+
75+
# 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"
78+
echo "Available versions:"
79+
ls "$LIB_DIR"/pg_net-*${postgresql.dlSuffix} 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${postgresql.dlSuffix}$//' || echo " No versions found"
80+
exit 1
81+
fi
82+
83+
# Update library symlink
84+
ln -sfnv "pg_net-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_net${postgresql.dlSuffix}"
85+
86+
# Update control file
87+
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_net.control"
88+
cat "$EXTENSION_DIR/pg_net--$VERSION.control" >> "$EXTENSION_DIR/pg_net.control"
89+
90+
echo "Successfully switched pg_net to version $VERSION"
91+
EOF
92+
'';
93+
};
1294
pname = "pg_net";
1395
build =
1496
version: hash:
@@ -18,7 +100,8 @@ let
18100
buildInputs = [
19101
curl
20102
postgresql
21-
] ++ lib.optional (version == "0.6") libuv;
103+
]
104+
++ lib.optional (version == "0.6") libuv;
22105

23106
src = fetchFromGitHub {
24107
owner = "supabase";
@@ -92,94 +175,14 @@ let
92175
in
93176
pkgs.buildEnv {
94177
name = pname;
95-
paths = packages;
178+
paths = packages ++ [ switchPgNetVersion ];
96179
postBuild = ''
97180
{
98181
echo "default_version = '${latestVersion}'"
99182
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
100183
} > $out/share/postgresql/extension/${pname}.control
101184
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
102185
103-
# Create version switcher script
104-
mkdir -p $out/bin
105-
cat > $out/bin/switch_pg_net_version <<'EOF'
106-
#!/bin/sh
107-
set -e
108-
109-
if [ $# -ne 1 ]; then
110-
echo "Usage: $0 <version>"
111-
echo "Example: $0 0.10.0"
112-
echo ""
113-
echo "Optional environment variables:"
114-
echo " NIX_PROFILE - Path to nix profile (default: /var/lib/postgresql/.nix-profile)"
115-
echo " LIB_DIR - Override library directory"
116-
echo " EXTENSION_DIR - Override extension directory"
117-
exit 1
118-
fi
119-
120-
VERSION=$1
121-
122-
# Set defaults, allow environment variable overrides
123-
: ''${NIX_PROFILE:="/var/lib/postgresql/.nix-profile"}
124-
: ''${LIB_DIR:=""}
125-
: ''${EXTENSION_DIR:=""}
126-
127-
# If LIB_DIR not explicitly set, auto-detect it
128-
if [ -z "$LIB_DIR" ]; then
129-
# Follow the complete chain of symlinks to find the multi-version directory
130-
CURRENT_LINK="$NIX_PROFILE/lib/pg_net-$VERSION${postgresql.dlSuffix}"
131-
echo "Starting with link: $CURRENT_LINK"
132-
133-
# Follow first two symlinks to get to the multi-version directory
134-
for i in 1 2; do
135-
if [ -L "$CURRENT_LINK" ]; then
136-
NEXT_LINK=$(readlink "$CURRENT_LINK")
137-
echo "Following link: $NEXT_LINK"
138-
if echo "$NEXT_LINK" | grep -q '^/'; then
139-
CURRENT_LINK="$NEXT_LINK"
140-
else
141-
CURRENT_LINK="$(dirname "$CURRENT_LINK")/$NEXT_LINK"
142-
fi
143-
echo "Current link is now: $CURRENT_LINK"
144-
fi
145-
done
146-
147-
# The multi-version directory should be the parent of the current link
148-
MULTI_VERSION_DIR=$(dirname "$CURRENT_LINK")
149-
echo "Found multi-version directory: $MULTI_VERSION_DIR"
150-
LIB_DIR="$MULTI_VERSION_DIR"
151-
else
152-
echo "Using provided LIB_DIR: $LIB_DIR"
153-
fi
154-
155-
# If EXTENSION_DIR not explicitly set, use default
156-
if [ -z "$EXTENSION_DIR" ]; then
157-
EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
158-
fi
159-
echo "Using EXTENSION_DIR: $EXTENSION_DIR"
160-
161-
echo "Looking for file: $LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}"
162-
ls -la "$LIB_DIR" || true
163-
164-
# Check if version exists
165-
if [ ! -f "$LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}" ]; then
166-
echo "Error: Version $VERSION not found in $LIB_DIR"
167-
echo "Available versions:"
168-
ls "$LIB_DIR"/pg_net-*${postgresql.dlSuffix} 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${postgresql.dlSuffix}$//' || echo " No versions found"
169-
exit 1
170-
fi
171-
172-
# Update library symlink
173-
ln -sfnv "pg_net-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_net${postgresql.dlSuffix}"
174-
175-
# Update control file
176-
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_net.control"
177-
cat "$EXTENSION_DIR/pg_net--$VERSION.control" >> "$EXTENSION_DIR/pg_net.control"
178-
179-
echo "Successfully switched pg_net to version $VERSION"
180-
EOF
181-
182-
chmod +x $out/bin/switch_pg_net_version
183186
184187
# checks
185188
(set -x

0 commit comments

Comments
 (0)