Skip to content

Commit 51be9a8

Browse files
committed
nix: improve dev workflow
Solves #5. This is done by avoiding building the postgres extension inside the nix derivation. Instead we build it with `make` on the current directory and install it on postgres using the upcoming postgres `extension_control_path` GUC. This is taken from the patch on https://www.postgresql.org/message-id/flat/E7C7BFFB-8857-48D4-A71F-88B359FADCFD%40justatheory.com and backpatched to postgres 12, 13, 14, 15 and 16, Thanks to this, it's now possible to do watch commands: ``` nxpg-watch nxpg-16 nxpg-build (rebuilds when changing sources) nxpg-watch nxpg-13 nxpg-tmp nxpg-test (runs tests when changing sources) ``` Future improvements: * Makes possible to enable code coverage, since the `.gcno` files will live outside the nix store. * It's wasteful to rebuild the patched pgs everytime, later on this `nxpg` could live in another repo and benefit from Nix caching. Additionally: * removes clean_generated in favor of builtin EXTRA_CLEAN * Don't hardcode sources in Makefile * Update github action
1 parent 676178d commit 51be9a8

12 files changed

+2560
-79
lines changed

.github/workflows/ci.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
with:
1919
nix_path: nixpkgs=channel:nixos-unstable
2020
- name: Run tests
21-
run: nix-shell --run "with-pg-${{ matrix.pg-version }} make installcheck"
21+
run: nix-shell --run "nxpg-${{ matrix.pg-version }} nxpg-tmp nxpg-test"
2222
- if: ${{ failure() }}
23-
run: cat output/regression.diffs
23+
run: |
24+
cat regression.out
25+
cat regression.diffs

.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ results/
44
.history
55
*.so
66
*.bc
7+
*.gcda
8+
*.gcno
79
tags
810
plmustache.control
9-
sql/plmustache--*.sql
11+
plmustache--*.sql
12+
coverage.info
13+
coverage_html
14+
regression.diffs
15+
regression.out

Makefile

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1+
## Our variables
12
EXTENSION = plmustache
23
EXTVERSION = 0.1
4+
SED ?= sed
35

4-
DATA = $(wildcard sql/*--*.sql)
6+
## PGXS variables
7+
PG_CONFIG = pg_config
8+
PGXS := $(shell $(PG_CONFIG) --pgxs)
59

610
MODULE_big = $(EXTENSION)
7-
OBJS = src/plmustache.o src/observation.o src/build.o
11+
SRC = $(wildcard src/*.c)
12+
OBJS = $(patsubst src/%.c, src/%.o, $(SRC))
13+
SHLIB_LINK = -lmustach
14+
PG_CFLAGS = -std=c99 -Wno-declaration-after-statement -Wall -Werror -Wshadow
815

916
TESTS = $(wildcard test/sql/*.sql)
1017
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
1118
REGRESS_OPTS = --inputdir=test
1219

13-
PG_CONFIG = pg_config
14-
SHLIB_LINK = -lmustach
15-
16-
PG_CFLAGS = -std=c99 -Wno-declaration-after-statement -Wall -Werror -Wshadow
17-
18-
PGXS := $(shell $(PG_CONFIG) --pgxs)
19-
20-
all: sql/$(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control
20+
DATA = $(wildcard *--*.sql)
2121

22-
.PHONY: clean_generated
23-
clean_generated:
24-
rm -f $(EXTENSION).control
25-
rm -f sql/$(EXTENSION)--$(EXTVERSION).sql
22+
EXTRA_CLEAN = $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control
2623

27-
# extra dep for clean target in pgxs.mk
28-
clean: clean_generated
24+
all: $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control
2925

30-
sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
26+
$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
3127
cp $< $@
3228

3329
$(EXTENSION).control:
34-
sed "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $(EXTENSION).control
30+
$(SED) "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $@
3531

3632
include $(PGXS)

nix/nxpg.nix

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{ writeShellScriptBin, findutils, entr, callPackage, postgresql_16, postgresql_15, postgresql_14, postgresql_13, postgresql_12 } :
2+
let
3+
prefix = "nxpg";
4+
supportedPgs = [
5+
postgresql_16
6+
postgresql_15
7+
postgresql_14
8+
postgresql_13
9+
postgresql_12
10+
];
11+
build =
12+
writeShellScriptBin "${prefix}-build" ''
13+
set -euo pipefail
14+
15+
make clean
16+
make
17+
'';
18+
test =
19+
writeShellScriptBin "${prefix}-test" ''
20+
set -euo pipefail
21+
22+
make clean
23+
make
24+
make installcheck
25+
'';
26+
27+
watch =
28+
writeShellScriptBin "${prefix}-watch" ''
29+
set -euo pipefail
30+
31+
${findutils}/bin/find . -type f \( -name '*.c' -o -name '*.h' \) | ${entr}/bin/entr -dr "$@"
32+
'';
33+
34+
tmpDb =
35+
writeShellScriptBin "${prefix}-tmp" ''
36+
set -euo pipefail
37+
38+
export tmpdir="$(mktemp -d)"
39+
40+
export PGDATA="$tmpdir"
41+
export PGHOST="$tmpdir"
42+
export PGUSER=postgres
43+
export PGDATABASE=postgres
44+
45+
trap 'pg_ctl stop -m i && rm -rf "$tmpdir"' sigint sigterm exit
46+
47+
PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER"
48+
49+
# pg versions older than 16 don't support adding "-c" to initdb to add these options
50+
# so we just modify the resulting postgresql.conf to avoid an error
51+
echo "dynamic_library_path='\$libdir:$(pwd)'" >> $PGDATA/postgresql.conf
52+
echo "extension_control_path='\$system:$(pwd)'" >> $PGDATA/postgresql.conf
53+
54+
default_options="-F -c listen_addresses=\"\" -k $PGDATA"
55+
56+
pg_ctl start -o "$default_options"
57+
58+
"$@"
59+
'';
60+
allPgPaths = map (pg:
61+
let
62+
ver = builtins.head (builtins.splitVersion pg.version);
63+
patchedPg = pg.overrideAttrs(oldAttrs: {
64+
patches = oldAttrs.patches ++ [
65+
./patches/${ver}-add-extension_control_path-for.patch
66+
];
67+
});
68+
script = ''
69+
set -euo pipefail
70+
71+
export PATH=${patchedPg}/bin:"$PATH"
72+
73+
"$@"
74+
'';
75+
in
76+
writeShellScriptBin "${prefix}-${ver}" script
77+
) supportedPgs;
78+
in
79+
[
80+
build
81+
test
82+
watch
83+
tmpDb
84+
allPgPaths
85+
]

0 commit comments

Comments
 (0)