Skip to content

Commit c2e2801

Browse files
committed
chore: consolidate nix code that handles building of postgres versions
1 parent 049010a commit c2e2801

File tree

4 files changed

+62
-179
lines changed

4 files changed

+62
-179
lines changed

flake.nix

Lines changed: 25 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@
2424
pgsqlSuperuser = "supabase_admin";
2525
nix2img = nix2container.packages.${system}.nix2container;
2626

27-
# The 'oriole_pkgs' variable holds all the upstream packages in nixpkgs, which
28-
# we can use to build our own images; it is the common name to refer to
29-
# a copy of nixpkgs which contains all its packages.
30-
# it also serves as a base for importing the orioldb/postgres overlay to
31-
#build the orioledb postgres patched version of postgresql16
32-
oriole_pkgs = import nixpkgs {
33-
config = { allowUnfree = true; };
34-
inherit system;
35-
overlays = [
36-
# NOTE (aseipp): add any needed overlays here. in theory we could
37-
# pull them from the overlays/ directory automatically, but we don't
38-
# want to have an arbitrary order, since it might matter. being
39-
# explicit is better.
40-
(import ./nix/overlays/cargo-pgrx-0-11-3.nix)
41-
(import ./nix/overlays/psql_16-oriole.nix)
42-
43-
];
44-
};
45-
#This variable works the same as 'oriole_pkgs' but builds using the upstream
46-
#nixpkgs builds of postgresql 15 and 16 + the overlays listed below
4727
pkgs = import nixpkgs {
4828
config = {
4929
allowUnfree = true;
@@ -59,11 +39,7 @@
5939
# explicit is better.
6040
(final: prev: {
6141
postgresql = final.callPackage ./nix/postgresql/default.nix {
62-
inherit (final) lib;
63-
inherit (final) stdenv;
64-
inherit (final) fetchurl;
65-
inherit (final) makeWrapper;
66-
inherit (final) callPackage;
42+
inherit (final) lib stdenv fetchurl makeWrapper callPackage buildEnv newScope;
6743
};
6844
})
6945
(import ./nix/overlays/cargo-pgrx-0-11-3.nix)
@@ -141,17 +117,15 @@
141117
x != ./nix/ext/pgvector.nix &&
142118
x != ./nix/ext/plv8.nix &&
143119
x != ./nix/ext/postgis.nix &&
144-
x != ./nix/ext/pgrouting.nix &&
145-
x != ./nix/ext/pg_net.nix
120+
x != ./nix/ext/pgrouting.nix
146121
) ourExtensions;
147122

148-
orioledbExtension = orioleFilteredExtensions ++ [ ./nix/ext/orioledb.nix ];
123+
orioledbExtensions = orioleFilteredExtensions ++ [ ./nix/ext/orioledb.nix ];
149124

150-
#this var is a convenience setting to import the orioledb patched version of postgresql
151-
postgresql_orioledb_16 = oriole_pkgs.postgresql_orioledb;
152-
#postgis_override = pkgs.postgis_override;
153125
getPostgresqlPackage = version:
154-
pkgs.postgresql."postgresql_${version}";
126+
if version == "orioledb_16"
127+
then pkgs.postgresql."postgresql_orioledb-16"
128+
else pkgs.postgresql."postgresql_${version}";
155129
# Create a 'receipt' file for a given postgresql package. This is a way
156130
# of adding a bit of metadata to the package, which can be used by other
157131
# tools to inspect what the contents of the install are: the PSQL
@@ -180,30 +154,26 @@
180154
};
181155
extensions = ourExts;
182156

183-
# NOTE (aseipp): this field can be used to do cache busting (e.g.
157+
# NOTE this field can be used to do cache busting (e.g.
184158
# force a rebuild of the psql packages) but also to helpfully inform
185159
# tools what version of the schema is being used, for forwards and
186160
# backwards compatibility
187161
receipt-version = "1";
188162
};
189163
};
190164

191-
makeOurOrioleDbPostgresPkgs = version: patchedPostgres:
192-
let postgresql = patchedPostgres;
193-
in map (path: pkgs.callPackage path { inherit postgresql; }) orioledbExtension;
165+
# makeOurOrioleDbPostgresPkgs = version: patchedPostgres:
166+
# let postgresql = patchedPostgres;
167+
# in map (path: pkgs.callPackage path { inherit postgresql; }) orioledbExtension;
194168

195169
makeOurPostgresPkgs = version:
196-
let postgresql = getPostgresqlPackage version;
197-
in map (path: pkgs.callPackage path { inherit postgresql; }) ourExtensions;
170+
let
171+
postgresql = getPostgresqlPackage version;
172+
extensionsToUse = if version == "orioledb-16"
173+
then orioledbExtensions
174+
else ourExtensions;
175+
in map (path: pkgs.callPackage path { inherit postgresql; }) extensionsToUse;
198176

199-
# Create an attrset that contains all the extensions included in a server for the orioledb version of postgresql + extension.
200-
makeOurOrioleDbPostgresPkgsSet = version: patchedPostgres:
201-
(builtins.listToAttrs (map
202-
(drv:
203-
{ name = drv.pname; value = drv; }
204-
)
205-
(makeOurOrioleDbPostgresPkgs version patchedPostgres)))
206-
// { recurseForDerivations = true; };
207177

208178
# Create an attrset that contains all the extensions included in a server.
209179
makeOurPostgresPkgsSet = version:
@@ -243,117 +213,6 @@
243213
paths = [ pgbin (makeReceipt pgbin upstreamExts ourExts) ];
244214
};
245215

246-
# makeOrioleDbPostgresBin = version: patchedPostgres:
247-
# let
248-
# postgresql = patchedPostgres;
249-
# upstreamExts = map
250-
# (ext: {
251-
# name = postgresql.pkgs."${ext}".pname;
252-
# version = postgresql.pkgs."${ext}".version;
253-
# })
254-
# orioledbPsqlExtensions;
255-
# ourExts = map (ext: { name = ext.pname; version = ext.version; }) (makeOurOrioleDbPostgresPkgs version postgresql);
256-
257-
# pgbin = postgresql.withPackages (ps:
258-
# (map (ext: ps."${ext}") orioledbPsqlExtensions) ++ (makeOurOrioleDbPostgresPkgs version postgresql)
259-
# );
260-
# in
261-
# pkgs.symlinkJoin {
262-
# inherit (pgbin) name version;
263-
# paths = [ pgbin (makeReceipt pgbin upstreamExts ourExts) ];
264-
# };
265-
makeOrioleDbPostgresBin = version: patchedPostgres:
266-
let
267-
postgresql = patchedPostgres;
268-
upstreamExts = map
269-
(ext: {
270-
name = patchedPostgres.pkgs."${ext}".pname;
271-
version = patchedPostgres.pkgs."${ext}".version;
272-
})
273-
orioledbPsqlExtensions;
274-
ourExts = map (ext: { name = ext.pname; version = ext.version; }) (makeOurOrioleDbPostgresPkgs version patchedPostgres);
275-
276-
scope = {
277-
inherit (patchedPostgres) jitSupport;
278-
inherit (oriole_pkgs.llvmPackages) llvm;
279-
postgresql = patchedPostgres;
280-
inherit (postgresql) stdenv';
281-
};
282-
newSelf = self // scope;
283-
newSuper = { callPackage = oriole_pkgs.newScope (scope // postgresql.pkgs); };
284-
285-
selectedPackages =
286-
(map (ext: postgresql.pkgs."${ext}") orioledbPsqlExtensions) ++
287-
(makeOurOrioleDbPostgresPkgs version postgresql);
288-
289-
pgbin = oriole_pkgs.buildEnv {
290-
name = "postgresql-and-plugins-${postgresql.version}";
291-
paths = [ postgresql ] ++ selectedPackages;
292-
buildInputs = [ oriole_pkgs.makeWrapper ];
293-
passthru = postgresql.passthru // {
294-
inherit postgresql;
295-
args = selectedPackages;
296-
};
297-
298-
postBuild = ''
299-
# Ensure all necessary directories exist
300-
mkdir -p $out/bin
301-
mkdir -p $out/lib/postgresql
302-
mkdir -p $out/share/postgresql/extension
303-
304-
# Create proper links for all binaries with environment
305-
for bin in ${postgresql}/bin/*; do
306-
binary_name=$(basename $bin)
307-
rm -f "$out/bin/$binary_name"
308-
makeWrapper $bin $out/bin/$binary_name \
309-
--set POSTGRES_MAJOR_VERSION "${postgresql.psqlSchema}" \
310-
--prefix PATH : $out/bin \
311-
--prefix LD_LIBRARY_PATH : "$out/lib:$out/lib/postgresql" \
312-
--prefix PKG_CONFIG_PATH : "$out/lib/pkgconfig" \
313-
--set PGDATA "/var/lib/postgresql/${postgresql.psqlSchema}" \
314-
--set PGHOST "/run/postgresql"
315-
done
316-
317-
# Ensure extension files are properly linked
318-
for pkg in ${postgresql} ${toString selectedPackages}; do
319-
# Link shared objects
320-
if [ -d "$pkg/lib" ]; then
321-
find "$pkg/lib" -type f -name '*.so' -exec ln -sf {} "$out/lib/postgresql/" \;
322-
fi
323-
324-
# Link extension control and SQL files
325-
if [ -d "$pkg/share/postgresql/extension" ]; then
326-
find "$pkg/share/postgresql/extension" -type f \( -name '*.control' -o -name '*.sql' \) \
327-
-exec ln -sf {} "$out/share/postgresql/extension/" \;
328-
fi
329-
done
330-
331-
# Create pg_config if it doesn't exist
332-
if [ ! -e "$out/bin/pg_config" ]; then
333-
makeWrapper ${postgresql}/bin/pg_config $out/bin/pg_config \
334-
--set prefix "$out" \
335-
--set includedir "$out/include" \
336-
--set pkgincludedir "$out/include/postgresql" \
337-
--set includedir-server "$out/include/postgresql/server" \
338-
--set libdir "$out/lib" \
339-
--set pkglibdir "$out/lib/postgresql" \
340-
--set localedir "$out/share/locale" \
341-
--set mandir "$out/share/man" \
342-
--set sharedir "$out/share/postgresql" \
343-
--set sysconfdir "/etc/postgresql" \
344-
--set pgxs "$out/lib/postgresql/pgxs/src/makefiles/pgxs.mk" \
345-
--set configure "--enable-nls --with-openssl --with-libxml --with-libxslt"
346-
fi
347-
'';
348-
} // {
349-
version = postgresql.version;
350-
};
351-
in
352-
pkgs.symlinkJoin {
353-
inherit (pgbin) name version;
354-
paths = [ pgbin (makeReceipt pgbin upstreamExts ourExts) ];
355-
};
356-
357216
# Create an attribute set, containing all the relevant packages for a
358217
# PostgreSQL install, wrapped up with a bow on top. There are three
359218
# packages:
@@ -368,11 +227,6 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
368227
exts = makeOurPostgresPkgsSet version;
369228
recurseForDerivations = true;
370229
};
371-
makeOrioleDbPostgres = version: patchedPostgres: rec {
372-
bin = makeOrioleDbPostgresBin version patchedPostgres;
373-
exts = makeOurOrioleDbPostgresPkgsSet version patchedPostgres;
374-
recurseForDerivations = true;
375-
};
376230

377231
# The base set of packages that we export from this Nix Flake, that can
378232
# be used with 'nix build'. Don't use the names listed below; check the
@@ -390,7 +244,7 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
390244
postgresVersions = {
391245
psql_15 = makePostgres "15";
392246
psql_16 = makePostgres "16";
393-
psql_oriole-16 = makeOrioleDbPostgres "16" postgresql_orioledb_16;
247+
psql_orioledb-16 = makePostgres "orioledb-16" ;
394248
};
395249

396250
# Find the active PostgreSQL version
@@ -406,20 +260,20 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
406260
};
407261
postgresql_15 = getPostgresqlPackage "15";
408262
postgresql_16 = getPostgresqlPackage "16";
409-
postgresql_oriole-16 = postgresql_orioledb_16;
263+
postgresql_orioledb-16 = getPostgresqlPackage "orioledb-16";
410264
in
411265
postgresVersions //{
412266
supabase-groonga = supabase-groonga;
413267
# PostgreSQL versions.
414268
psql_15 = postgresVersions.psql_15;
415269
psql_16 = postgresVersions.psql_16;
416-
psql_oriole-16 = postgresVersions.psql_oriole-16;
270+
psql_orioledb-16 = postgresVersions.psql_orioledb-16;
417271
sfcgal = sfcgal;
418272
pg_prove = pkgs.perlPackages.TAPParserSourceHandlerpgTAP;
419-
inherit postgresql_15 postgresql_16 postgresql_oriole-16;
273+
inherit postgresql_15 postgresql_16 postgresql_orioledb-16;
420274
postgresql_15_debug = if pkgs.stdenv.isLinux then postgresql_15.debug else null;
421275
postgresql_16_debug = if pkgs.stdenv.isLinux then postgresql_16.debug else null;
422-
postgresql_oriole-16_debug = if pkgs.stdenv.isLinux then postgresql_orioledb_16.debug else null;
276+
postgresql_orioledb-16_debug = if pkgs.stdenv.isLinux then postgresql_orioledb-16.debug else null;
423277
postgresql_15_src = pkgs.stdenv.mkDerivation {
424278
pname = "postgresql-15-src";
425279
version = postgresql_15.version;
@@ -464,9 +318,9 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
464318
platforms = platforms.all;
465319
};
466320
};
467-
postgresql_oriole-16_src = pkgs.stdenv.mkDerivation {
321+
postgresql_orioledb-16_src = pkgs.stdenv.mkDerivation {
468322
pname = "postgresql-16-src";
469-
version = postgresql_oriole-16.version;
323+
version = postgresql_orioledb-16.version;
470324

471325
src = postgresql_16.src;
472326

@@ -545,7 +399,7 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
545399
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
546400
--subst-var-by 'PSQL_CONF_FILE' $out/etc/postgresql/postgresql.conf \
547401
--subst-var-by 'PSQL16_BINDIR' '${basePackages.psql_16.bin}' \
548-
--subst-var-by 'PSQLORIOLEDB16_BINDIR' '${basePackages.psql_oriole-16.bin}' \
402+
--subst-var-by 'PSQLORIOLEDB16_BINDIR' '${basePackages.psql_orioledb-16.bin}' \
549403
--subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
550404
--subst-var-by 'READREPL_CONF_FILE' "$out/etc/postgresql-custom/read-replica.conf" \
551405
--subst-var-by 'LOGGING_CONF_FILE' "$out/etc/postgresql-custom/logging.conf" \
@@ -575,7 +429,7 @@ makeOrioleDbPostgresBin = version: patchedPostgres:
575429
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
576430
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
577431
--subst-var-by 'PSQL16_BINDIR' '${basePackages.psql_16.bin}' \
578-
--subst-var-by 'PSQLORIOLEDB16_BINDIR' '${basePackages.psql_oriole-16.bin}' \
432+
--subst-var-by 'PSQLORIOLEDB16_BINDIR' '${basePackages.psql_orioledb-16.bin}' \
579433
--subst-var-by 'MIGRATIONS_DIR' '${migrationsDir}' \
580434
--subst-var-by 'POSTGRESQL_SCHEMA_SQL' '${postgresqlSchemaSql}' \
581435
--subst-var-by 'PGBOUNCER_AUTH_SCHEMA_SQL' '${pgbouncerAuthSchemaSql}' \

nix/postgresql/default.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
self:
22
let
3-
#adapted from the postgresql nixpkgs package
43
versions = {
54
postgresql_15 = ./15.nix;
65
postgresql_16 = ./16.nix;
6+
postgresql_orioledb-16 = ./orioledb-16.nix;
77
};
8-
98
mkAttributes = jitSupport:
109
self.lib.mapAttrs' (version: path:
1110
let
@@ -15,7 +14,6 @@ let
1514
inherit jitSupport self;
1615
})
1716
) versions;
18-
1917
in
2018
# variations without and with JIT
2119
(mkAttributes false) // (mkAttributes true)

nix/postgresql/generic.nix

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ let
77
, glibc, zlib, readline, openssl, icu, lz4, zstd, systemd, libossp_uuid
88
, pkg-config, libxml2, tzdata, libkrb5, substituteAll, darwin
99
, linux-pam
10+
#orioledb specific
11+
, perl, bison, flex, docbook_xsl, docbook_xml_dtd_45, docbook_xsl_ns, libxslt
1012

1113
# This is important to obtain a version of `libpq` that does not depend on systemd.
1214
, systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemd && !stdenv.hostPlatform.isStatic
@@ -49,10 +51,16 @@ let
4951
inherit version;
5052
pname = pname + lib.optionalString jitSupport "-jit";
5153

52-
src = fetchurl {
53-
url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
54-
inherit hash;
55-
};
54+
src = if (builtins.match "16_.*" version != null) then
55+
fetchurl {
56+
url = "https://github.com/orioledb/postgres/archive/refs/tags/patches${version}.tar.gz";
57+
inherit hash;
58+
}
59+
else
60+
fetchurl {
61+
url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
62+
inherit hash;
63+
};
5664

5765
hardeningEnable = lib.optionals (!stdenv'.cc.isClang) [ "pie" ];
5866

@@ -74,7 +82,10 @@ let
7482
++ lib.optionals pythonSupport [ python3 ]
7583
++ lib.optionals gssSupport [ libkrb5 ]
7684
++ lib.optionals stdenv'.isLinux [ linux-pam ]
77-
++ lib.optionals (!stdenv'.isDarwin) [ libossp_uuid ];
85+
++ lib.optionals (!stdenv'.isDarwin) [ libossp_uuid ]
86+
++ lib.optionals (builtins.match "16_.*" version != null) [
87+
perl bison flex docbook_xsl docbook_xml_dtd_45 docbook_xsl_ns libxslt
88+
];
7889

7990
nativeBuildInputs = [
8091
makeWrapper

nix/postgresql/orioledb-16.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# { self, fetchurl, ... }:
2+
3+
# let
4+
# generic = import ./generic.nix rec {
5+
# version = "16";
6+
# hash = "sha256-29uHUACwZKh8e4zJ9tWzEhLNjEuh6P31KbpxnMEhtuI=";
7+
# src = fetchurl {
8+
# url = "https://github.com/orioledb/postgres/archive/refs/tags/patches16_31.tar.gz";
9+
# sha256 = hash;
10+
# };
11+
# };
12+
# in
13+
# generic.overrideAttrs (oldAttrs: {
14+
# inherit generic;
15+
# })
16+
# orioledb-16.nix
17+
import ./generic.nix {
18+
version = "16_31";
19+
hash = "sha256-29uHUACwZKh8e4zJ9tWzEhLNjEuh6P31KbpxnMEhtuI=";
20+
}

0 commit comments

Comments
 (0)