Skip to content

Commit 8283677

Browse files
committed
refactor: DRY NixOS test configurations
Extract common NixOS test node configuration into common.nix to eliminate duplication across extension tests. Tests with custom requirements use lib.mkMerge to override defaults. This helps us to see at a glance why a test deviates from the standard setup.
1 parent 79cbab5 commit 8283677

File tree

14 files changed

+347
-806
lines changed

14 files changed

+347
-806
lines changed

nix/ext/plpgsql-check.nix

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,6 @@ buildEnv {
133133
inherit versions numberOfVersions switch-ext-version;
134134
pname = "${pname}-all";
135135
hasBackgroundWorker = true;
136-
defaultSettings = {
137-
shared_preload_libraries = [
138-
"plpgsql"
139-
"plpgsql_check"
140-
];
141-
};
142136
version =
143137
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
144138
};

nix/ext/tests/default.nix

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ let
33
testsDir = ./.;
44
testFiles = builtins.attrNames (builtins.readDir testsDir);
55
nixFiles = builtins.filter (
6-
name: builtins.match ".*\\.nix$" name != null && name != "default.nix"
6+
name: builtins.match ".*\\.nix$" name != null && name != "default.nix" && name != "lib.nix"
77
) testFiles;
88
extTest =
99
extension_name:
1010
let
1111
pname = extension_name;
1212
inherit (pkgs) lib;
13-
installedExtension =
14-
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
15-
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
13+
versions = postgresqlMajorVersion: (testLib.installedExtension postgresqlMajorVersion).versions;
1614
postgresqlWithExtension =
1715
postgresql:
1816
let
@@ -22,7 +20,7 @@ let
2220
paths = [
2321
postgresql
2422
postgresql.lib
25-
(installedExtension majorVersion)
23+
(testLib.installedExtension majorVersion)
2624
];
2725
passthru = {
2826
inherit (postgresql) version psqlSchema;
@@ -43,97 +41,17 @@ let
4341
};
4442
in
4543
pkg;
44+
testLib = import ./lib.nix {
45+
inherit self pkgs;
46+
testedExtensionName = extension_name;
47+
};
4648
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
4749
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
4850
in
4951
self.inputs.nixpkgs.lib.nixos.runTest {
5052
name = pname;
5153
hostPkgs = pkgs;
52-
nodes.server =
53-
{ config, ... }:
54-
{
55-
virtualisation = {
56-
forwardPorts = [
57-
{
58-
from = "host";
59-
host.port = 13022;
60-
guest.port = 22;
61-
}
62-
];
63-
};
64-
services.openssh = {
65-
enable = true;
66-
};
67-
68-
services.postgresql = {
69-
enable = true;
70-
package = psql_15;
71-
enableTCPIP = true;
72-
authentication = ''
73-
local all postgres peer map=postgres
74-
local all all peer map=root
75-
'';
76-
identMap = ''
77-
root root supabase_admin
78-
postgres postgres postgres
79-
'';
80-
ensureUsers = [
81-
{
82-
name = "supabase_admin";
83-
ensureClauses.superuser = true;
84-
}
85-
];
86-
settings = (installedExtension "15").defaultSettings or { };
87-
};
88-
89-
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
90-
91-
specialisation.postgresql17.configuration = {
92-
services.postgresql = {
93-
package = lib.mkForce psql_17;
94-
settings = (installedExtension "17").defaultSettings or { };
95-
};
96-
97-
systemd.services.postgresql-migrate = {
98-
serviceConfig = {
99-
Type = "oneshot";
100-
RemainAfterExit = true;
101-
User = "postgres";
102-
Group = "postgres";
103-
StateDirectory = "postgresql";
104-
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
105-
};
106-
script =
107-
let
108-
oldPostgresql = psql_15;
109-
newPostgresql = psql_17;
110-
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
111-
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
112-
in
113-
''
114-
if [[ ! -d ${newDataDir} ]]; then
115-
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
116-
${newPostgresql}/bin/initdb -D "${newDataDir}"
117-
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
118-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
119-
${
120-
if config.services.postgresql.settings.shared_preload_libraries != null then
121-
" --old-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}' --new-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}'"
122-
else
123-
""
124-
}
125-
else
126-
echo "${newDataDir} already exists"
127-
fi
128-
'';
129-
};
130-
131-
systemd.services.postgresql = {
132-
after = [ "postgresql-migrate.service" ];
133-
requires = [ "postgresql-migrate.service" ];
134-
};
135-
};
136-
};
54+
nodes.server = { config, ... }: testLib.mkDefaultNixosTestNode { inherit config psql_15 psql_17; };
13755
testScript =
13856
{ nodes, ... }:
13957
let
@@ -148,10 +66,10 @@ let
14866
extension_name = "${pname}"
14967
pg17_configuration = "${pg17-configuration}"
15068
ext_has_background_worker = ${
151-
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
69+
if (testLib.installedExtension "15") ? hasBackgroundWorker then "True" else "False"
15270
}
15371
sql_test_directory = Path("${../../tests}")
154-
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
72+
pg_regress_test_name = "${(testLib.installedExtension "15").pgRegressTestName or pname}"
15573
15674
${builtins.readFile ./lib.py}
15775

nix/ext/tests/http.nix

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,70 +35,17 @@ let
3535
};
3636
in
3737
pkg;
38+
testLib = import ./lib.nix {
39+
inherit self pkgs;
40+
testedExtensionName = pname;
41+
};
42+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
43+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
3844
in
3945
self.inputs.nixpkgs.lib.nixos.runTest {
4046
name = pname;
4147
hostPkgs = pkgs;
42-
nodes.server =
43-
{ config, ... }:
44-
{
45-
virtualisation = {
46-
forwardPorts = [
47-
{
48-
from = "host";
49-
host.port = 13022;
50-
guest.port = 22;
51-
}
52-
];
53-
};
54-
services.openssh = {
55-
enable = true;
56-
};
57-
58-
services.postgresql = {
59-
enable = true;
60-
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
61-
};
62-
63-
specialisation.postgresql17.configuration = {
64-
services.postgresql = {
65-
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17);
66-
};
67-
68-
systemd.services.postgresql-migrate = {
69-
serviceConfig = {
70-
Type = "oneshot";
71-
RemainAfterExit = true;
72-
User = "postgres";
73-
Group = "postgres";
74-
StateDirectory = "postgresql";
75-
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
76-
};
77-
script =
78-
let
79-
oldPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
80-
newPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
81-
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
82-
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
83-
in
84-
''
85-
if [[ ! -d ${newDataDir} ]]; then
86-
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
87-
${newPostgresql}/bin/initdb -D "${newDataDir}"
88-
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
89-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
90-
else
91-
echo "${newDataDir} already exists"
92-
fi
93-
'';
94-
};
95-
96-
systemd.services.postgresql = {
97-
after = [ "postgresql-migrate.service" ];
98-
requires = [ "postgresql-migrate.service" ];
99-
};
100-
};
101-
};
48+
nodes.server = { config, ... }: testLib.mkDefaultNixosTestNode { inherit config psql_15 psql_17; };
10249
testScript =
10350
{ nodes, ... }:
10451
let

nix/ext/tests/lib.nix

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# lib.nix
2+
#
3+
# Common utilities and configuration builders for PostgreSQL extension tests.
4+
#
5+
# This module provides reusable functions to create NixOS test nodes with
6+
# standard PostgreSQL configurations, reducing duplication across extension
7+
# test files.
8+
#
9+
# ## Exports
10+
#
11+
# - `installedExtension`: Function to get the installed extension package for a PostgreSQL version
12+
# - `mkDefaultNixosTestNode`: Creates a NixOS test node with standard PostgreSQL setup
13+
#
14+
# ## Examples
15+
#
16+
# See existing test files for real-world examples:
17+
# - Simple tests: postgis.nix, http.nix, plpgsql_check.nix
18+
# - With custom settings: vault.nix, pgsodium.nix
19+
# - With environment variables: pgroonga.nix
20+
# - Complex specialisations: pgrouting.nix
21+
{
22+
self,
23+
pkgs,
24+
testedExtensionName,
25+
}:
26+
rec {
27+
# Get the installed extension package for a specific PostgreSQL major version.
28+
#
29+
# Type: installedExtension :: String -> Derivation
30+
#
31+
# Example:
32+
# installedExtension "15" => derivation
33+
installedExtension =
34+
postgresMajorVersion:
35+
self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${testedExtensionName}-all";
36+
37+
# Create a default NixOS test node with PostgreSQL configured for extension testing.
38+
#
39+
# When psql_17 is null, the postgresql17 specialisation is disabled.
40+
# This is useful for extensions that only support PostgreSQL 15.
41+
#
42+
# Override or extend configuration using lib.mkMerge:
43+
# Example:
44+
# lib.mkMerge [
45+
# (mkDefaultNixosTestNode { inherit config psql_15 psql_17; })
46+
# { services.postgresql.settings.shared_preload_libraries = lib.mkForce "my_ext"; }
47+
# ]
48+
mkDefaultNixosTestNode =
49+
{
50+
config, # The node's config attribute
51+
psql_15, # PostgreSQL 15 package with extension
52+
psql_17 ? null, # PostgreSQL 17 package with extension (optional)
53+
...
54+
}:
55+
{
56+
virtualisation = {
57+
forwardPorts = [
58+
{
59+
from = "host";
60+
host.port = 13022;
61+
guest.port = 22;
62+
}
63+
];
64+
};
65+
services.openssh = {
66+
enable = true;
67+
};
68+
69+
services.postgresql = {
70+
enable = true;
71+
package = psql_15;
72+
enableTCPIP = true;
73+
authentication = ''
74+
local all postgres peer map=postgres
75+
local all all peer map=root
76+
'';
77+
identMap = ''
78+
root root supabase_admin
79+
postgres postgres postgres
80+
'';
81+
ensureUsers = [
82+
{
83+
name = "supabase_admin";
84+
ensureClauses.superuser = true;
85+
}
86+
];
87+
settings = (installedExtension "15").defaultSettings or { };
88+
};
89+
90+
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
91+
92+
specialisation.postgresql17.configuration = pkgs.lib.mkIf (psql_17 != null) {
93+
services.postgresql = {
94+
package = pkgs.lib.mkForce psql_17;
95+
settings = (installedExtension "17").defaultSettings or { };
96+
};
97+
98+
systemd.services.postgresql-migrate = {
99+
serviceConfig = {
100+
Type = "oneshot";
101+
RemainAfterExit = true;
102+
User = "postgres";
103+
Group = "postgres";
104+
StateDirectory = "postgresql";
105+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
106+
};
107+
script =
108+
let
109+
oldPostgresql = psql_15;
110+
newPostgresql = psql_17;
111+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
112+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
113+
in
114+
''
115+
if [[ ! -d ${newDataDir} ]]; then
116+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
117+
${newPostgresql}/bin/initdb -D "${newDataDir}"
118+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
119+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
120+
${
121+
if config.services.postgresql.settings.shared_preload_libraries != null then
122+
" --old-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}' --new-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}'"
123+
else
124+
""
125+
}
126+
else
127+
echo "${newDataDir} already exists"
128+
fi
129+
'';
130+
};
131+
132+
systemd.services.postgresql = {
133+
after = [ "postgresql-migrate.service" ];
134+
requires = [ "postgresql-migrate.service" ];
135+
};
136+
};
137+
};
138+
}

0 commit comments

Comments
 (0)