forked from supabase/nix-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
374 lines (335 loc) · 15 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{
description = "Prototype tooling for deploying PostgreSQL";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
nixConfig = {
extra-substituters = [
"https://nix-postgres-artifacts.s3.amazonaws.com"
];
extra-trusted-public-keys = [
"nix-cache.supabase.com-1:ZfEc7Qb7SN+qOTJGMtCz54rnVQ1W2ZI2ROCSSD6YQYc="
];
};
outputs = { self, nixpkgs, flake-utils }: let
gitRev = "vcs=${self.shortRev or "dirty"}+${builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}";
ourSystems = with flake-utils.lib; [
system.x86_64-linux
system.aarch64-linux
]; in flake-utils.lib.eachSystem ourSystems (system:
let
pgsqlDefaultPort = "5435";
# The 'pkgs' variable holds all the upstream packages in nixpkgs, which
# we can use to build our own images; it is the common name to refer to
# a copy of nixpkgs which contains all its packages.
pkgs = import nixpkgs {
inherit system;
overlays = [
# NOTE (aseipp): add any needed overlays here. in theory we could
# pull them from the overlays/ directory automatically, but we don't
# want to have an arbitrary order, since it might matter. being
# explicit is better.
(import ./overlays/cargo-pgrx.nix)
(import ./overlays/gdal-small.nix)
];
};
# FIXME (aseipp): pg_prove is yet another perl program that needs
# LOCALE_ARCHIVE set in non-NixOS environments. upstream this. once that's done, we
# can remove this wrapper.
pg_prove = pkgs.runCommand "pg_prove" {
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
mkdir -p $out/bin
for x in pg_prove pg_tapgen; do
makeWrapper "${pkgs.perlPackages.TAPParserSourceHandlerpgTAP}/bin/$x" "$out/bin/$x" \
--set LOCALE_ARCHIVE "${pkgs.glibcLocales}/lib/locale/locale-archive"
done
'';
# Our list of PostgreSQL extensions which come from upstream Nixpkgs.
# These are maintained upstream and can easily be used here just by
# listing their name. Anytime the version of nixpkgs is upgraded, these
# may also bring in new versions of the extensions.
psqlExtensions = [
"postgis"
"pgrouting"
"pgtap"
"pg_cron"
"pgaudit"
"pgjwt"
"plpgsql_check"
"pg_safeupdate"
"timescaledb"
"wal2json"
/* pljava */
"plv8"
"rum"
"pgvector"
"pg_repack"
"pgroonga"
];
# Custom extensions that exist in our repository. These aren't upstream
# either because nobody has done the work, maintaining them here is
# easier and more expedient, or because they may not be suitable, or are
# too niche/one-off.
#
# Ideally, most of these should have copies upstream for third party
# use, but even if they did, keeping our own copies means that we can
# rollout new versions of these critical things easier without having to
# go through the upstream release engineering process.
ourExtensions = [
./ext/pgsql-http.nix
./ext/pg_plan_filter.nix
./ext/pg_net.nix
./ext/pg_hashids.nix
./ext/pgsodium.nix
./ext/pg_graphql.nix
./ext/pg_stat_monitor.nix
./ext/pg_jsonschema.nix
./ext/vault.nix
./ext/hypopg.nix
./ext/pg_tle.nix
./ext/wrappers/default.nix
./ext/supautils.nix
];
# Create a 'receipt' file for a given postgresql package. This is a way
# of adding a bit of metadata to the package, which can be used by other
# tools to inspect what the contents of the install are: the PSQL
# version, the installed extensions, et cetera.
#
# This takes three arguments:
# - pgbin: the postgresql package we are building on top of
# - upstreamExts: the list of extensions from upstream nixpkgs. This is
# not a list of packages, but an attrset containing extension names
# mapped to versions.
# - ourExts: the list of extensions from upstream nixpkgs. This is not
# a list of packages, but an attrset containing extension names
# mapped to versions.
#
# The output is a package containing the receipt.json file, which can be
# merged with the PostgreSQL installation using 'symlinkJoin'.
makeReceipt = pgbin: upstreamExts: ourExts: pkgs.writeTextFile {
name = "receipt";
destination = "/receipt.json";
text = builtins.toJSON {
revision = gitRev;
psql-version = pgbin.version;
nixpkgs = {
revision = nixpkgs.rev;
extensions = upstreamExts;
};
extensions = ourExts;
# NOTE (aseipp): this field can be used to do cache busting (e.g.
# force a rebuild of the psql packages) but also to helpfully inform
# tools what version of the schema is being used, for forwards and
# backwards compatibility
receipt-version = "1";
};
};
makeOurPostgresPkgs = version:
let postgresql = pkgs."postgresql_${version}";
in map (path: pkgs.callPackage path { inherit postgresql; }) ourExtensions;
# Create an attrset that contains all the extensions included in a server.
makeOurPostgresPkgsSet = version:
(builtins.listToAttrs (map (drv:
{ name = drv.pname; value = drv; }
) (makeOurPostgresPkgs version)))
// { recurseForDerivations = true; };
# Create a binary distribution of PostgreSQL, given a version.
#
# NOTE: The version here does NOT refer to the exact PostgreSQL version;
# it refers to the *major number only*, which is used to select the
# correct version of the package from nixpkgs. This is because we want
# to be able to do so in an open ended way. As an example, the version
# "14" passed in will use the nixpkgs package "postgresql_14" as the
# basis for building extensions, etc.
makePostgresBin = version:
let
postgresql = pkgs."postgresql_${version}";
upstreamExts = map (ext: {
name = postgresql.pkgs."${ext}".pname;
version = postgresql.pkgs."${ext}".version;
}) psqlExtensions;
ourExts = map (ext: { name = ext.pname; version = ext.version; }) (makeOurPostgresPkgs version);
pgbin = postgresql.withPackages (ps:
(map (ext: ps."${ext}") psqlExtensions) ++ (makeOurPostgresPkgs version)
);
in pkgs.symlinkJoin {
inherit (pgbin) name version;
paths = [ pgbin (makeReceipt pgbin upstreamExts ourExts) ];
};
# Make a Docker Image from a given PostgreSQL version and binary package.
makePostgresDocker = version: binPackage:
let
initScript = pkgs.runCommand "docker-init.sh" {} ''
mkdir -p $out/bin
substitute ${./docker/init.sh.in} $out/bin/init.sh \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}'
chmod +x $out/bin/init.sh
'';
postgresqlConfig = pkgs.runCommand "postgresql.conf" {} ''
mkdir -p $out/etc/
substitute ${./tests/postgresql.conf.in} $out/etc/postgresql.conf \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${./tests/util/pgsodium_getkey.sh}"
'';
in pkgs.dockerTools.buildImage {
name = "postgresql-${version}";
tag = "latest";
runAsRoot = ''
#!${pkgs.runtimeShell}
${pkgs.dockerTools.shadowSetup}
groupadd -r postgres
useradd -r -g postgres postgres
mkdir -p /data /run/postgresql
chown postgres:postgres /data /run/postgresql
'';
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = with pkgs; [
initScript coreutils bash binPackage
dockerTools.binSh sudo postgresqlConfig
];
pathsToLink = [ "/bin" "/etc" "/var" "/share" ];
};
config = {
Cmd = [ "/bin/init.sh" ];
ExposedPorts = { "${pgsqlDefaultPort}/tcp" = {}; };
WorkingDir = "/data";
Volumes = { "/data" = { }; };
};
};
# Create an attribute set, containing all the relevant packages for a
# PostgreSQL install, wrapped up with a bow on top. There are three
# packages:
#
# - bin: the postgresql package itself, with all the extensions
# installed, and a receipt.json file containing metadata about the
# install.
# - exts: an attrset containing all the extensions, mapped to their
# package names.
# - docker: a docker image containing the postgresql package, with all
# the extensions installed, and a receipt.json file containing
# metadata about the install.
makePostgres = version: rec {
bin = makePostgresBin version;
exts = makeOurPostgresPkgsSet version;
docker = makePostgresDocker version bin;
recurseForDerivations = true;
};
# The base set of packages that we export from this Nix Flake, that can
# be used with 'nix build'. Don't use the names listed below; check the
# name in 'nix flake show' in order to make sure exactly what name you
# want.
basePackages = {
# PostgreSQL versions.
psql_14 = makePostgres "14";
psql_15 = makePostgres "15";
# Start a version of the server.
start-server = pkgs.runCommand "start-postgres-server" {} ''
mkdir -p $out/bin
substitute ${./tools/run-server.sh.in} $out/bin/start-postgres-server \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
chmod +x $out/bin/start-postgres-server
'';
# Start a version of the client.
start-client = pkgs.runCommand "start-postgres-client" {} ''
mkdir -p $out/bin
substitute ${./tools/run-client.sh.in} $out/bin/start-postgres-client \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
chmod +x $out/bin/start-postgres-client
'';
# Migrate between two data directories.
migrate-tool =
let
configFile = ./tests/postgresql.conf.in;
getkeyScript = ./tests/util/pgsodium_getkey.sh;
primingScript = ./tests/prime.sql;
migrationData = ./tests/migrations/data.sql;
in pkgs.runCommand "migrate-postgres" {} ''
mkdir -p $out/bin
substitute ${./tools/migrate-tool.sh.in} $out/bin/migrate-postgres \
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
--subst-var-by 'PSQL_CONF_FILE' '${configFile}' \
--subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
--subst-var-by 'PRIMING_SCRIPT' '${primingScript}' \
--subst-var-by 'MIGRATION_DATA' '${migrationData}'
chmod +x $out/bin/migrate-postgres
'';
};
# Create a testing harness for a PostgreSQL package. This is used for
# 'nix flake check', and works with any PostgreSQL package you hand it.
makeCheckHarness = pgpkg:
let
sqlTests = ./tests/smoke;
in pkgs.runCommand "postgres-${pgpkg.version}-check-harness" {
nativeBuildInputs = with pkgs; [ coreutils bash pgpkg pg_prove procps ];
} ''
export PGDATA=/tmp/pgdata
mkdir -p $PGDATA
initdb --locale=C
substitute ${./tests/postgresql.conf.in} $PGDATA/postgresql.conf \
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${./tests/util/pgsodium_getkey.sh}"
postgres -k /tmp >logfile 2>&1 &
sleep 2
createdb -h localhost testing
psql -h localhost -d testing -Xaf ${./tests/prime.sql}
pg_prove -h localhost -d testing ${sqlTests}/*.sql
pkill postgres
mv logfile $out
'';
in rec {
# The list of all packages that can be built with 'nix build'. The list
# of names that can be used can be shown with 'nix flake show'
packages = flake-utils.lib.flattenTree basePackages // {
# Any extra packages we might want to include in our package
# set can go here.
inherit (pkgs)
# NOTE: comes from our cargo-pgrx.nix overlay
cargo-pgrx_0_9_7
;
};
# The list of exported 'checks' that are run with every run of 'nix
# flake check'. This is run in the CI system, as well.
checks = {
psql_14 = makeCheckHarness basePackages.psql_14.bin;
psql_15 = makeCheckHarness basePackages.psql_15.bin;
};
# Apps is a list of names of things that can be executed with 'nix run';
# these are distinct from the things that can be built with 'nix build',
# so they need to be listed here too.
apps =
let
mkApp = attrName: binName: {
type = "app";
program = "${basePackages."${attrName}"}/bin/${binName}";
};
in {
start-server = mkApp "start-server" "start-postgres-server";
start-client = mkApp "start-client" "start-postgres-client";
migration-test = mkApp "migrate-tool" "migrate-postgres";
};
# 'devShells.default' lists the set of packages that are included in the
# ambient $PATH environment when you run 'nix develop'. This is useful
# for development and puts many convenient devtools instantly within
# reach.
devShells.default = pkgs.mkShell {
packages = with pkgs; [
coreutils just nix-update
pg_prove shellcheck
basePackages.start-server
basePackages.start-client
basePackages.migrate-tool
];
shellHook = ''
export HISTFILE=.history
'';
};
}
);
}