Skip to content

nix: improve dev workflow #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Run tests
run: nix-shell --run "with-pg-${{ matrix.pg-version }} make installcheck"
run: nix-shell --run "nxpg-${{ matrix.pg-version }} nxpg-tmp nxpg-test"
- if: ${{ failure() }}
run: cat output/regression.diffs
run: |
cat regression.out
cat regression.diffs
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ results/
.history
*.so
*.bc
*.gcda
*.gcno
tags
plmustache.control
sql/plmustache--*.sql
plmustache--*.sql
coverage.info
coverage_html
regression.diffs
regression.out
32 changes: 14 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
## Our variables
EXTENSION = plmustache
EXTVERSION = 0.1
SED ?= sed

DATA = $(wildcard sql/*--*.sql)
## PGXS variables
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

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

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

PG_CONFIG = pg_config
SHLIB_LINK = -lmustach

PG_CFLAGS = -std=c99 -Wno-declaration-after-statement -Wall -Werror -Wshadow

PGXS := $(shell $(PG_CONFIG) --pgxs)

all: sql/$(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control
DATA = $(wildcard *--*.sql)

.PHONY: clean_generated
clean_generated:
rm -f $(EXTENSION).control
rm -f sql/$(EXTENSION)--$(EXTVERSION).sql
EXTRA_CLEAN = $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control

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

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
cp $< $@

$(EXTENSION).control:
sed "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $(EXTENSION).control
$(SED) "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $@

include $(PGXS)
85 changes: 85 additions & 0 deletions nix/nxpg.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{ writeShellScriptBin, findutils, entr, callPackage, postgresql_16, postgresql_15, postgresql_14, postgresql_13, postgresql_12 } :
let
prefix = "nxpg";
supportedPgs = [
postgresql_16
postgresql_15
postgresql_14
postgresql_13
postgresql_12
];
build =
writeShellScriptBin "${prefix}-build" ''
set -euo pipefail

make clean
make
'';
test =
writeShellScriptBin "${prefix}-test" ''
set -euo pipefail

make clean
make
make installcheck
'';

watch =
writeShellScriptBin "${prefix}-watch" ''
set -euo pipefail

${findutils}/bin/find . -type f \( -name '*.c' -o -name '*.h' \) | ${entr}/bin/entr -dr "$@"
'';

tmpDb =
writeShellScriptBin "${prefix}-tmp" ''
set -euo pipefail

export tmpdir="$(mktemp -d)"

export PGDATA="$tmpdir"
export PGHOST="$tmpdir"
export PGUSER=postgres
export PGDATABASE=postgres

trap 'pg_ctl stop -m i && rm -rf "$tmpdir"' sigint sigterm exit

PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER"

# pg versions older than 16 don't support adding "-c" to initdb to add these options
# so we just modify the resulting postgresql.conf to avoid an error
echo "dynamic_library_path='\$libdir:$(pwd)'" >> $PGDATA/postgresql.conf
echo "extension_control_path='\$system:$(pwd)'" >> $PGDATA/postgresql.conf

default_options="-F -c listen_addresses=\"\" -k $PGDATA"

pg_ctl start -o "$default_options"

"$@"
'';
allPgPaths = map (pg:
let
ver = builtins.head (builtins.splitVersion pg.version);
patchedPg = pg.overrideAttrs(oldAttrs: {
patches = oldAttrs.patches ++ [
./patches/${ver}-add-extension_control_path-for.patch
];
});
script = ''
set -euo pipefail

export PATH=${patchedPg}/bin:"$PATH"

"$@"
'';
in
writeShellScriptBin "${prefix}-${ver}" script
) supportedPgs;
in
[
build
test
watch
tmpDb
allPgPaths
]
Loading