{ description = "pg_pure (Pure as a PostgreSQL procedural language) for PostgreSQL 17, 18 and 19, with libpure and the vetted Pure add-on modules built from the gburd/pure-lang fork"; # The fork carries the embeddable changes pg_pure needs (safe mode, hidden # LLVM symbols). It is not in nixpkgs, so a host that wants pg_pure would # otherwise maintain a bespoke LLVM build by hand; this flake removes that # cost. See pg_pure's DESIGN.md and ROADMAP 2.A. inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; pure-lang = { url = "github:gburd/pure-lang/libpure-embeddable"; flake = false; }; }; outputs = { self, nixpkgs, flake-utils, pure-lang }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; inherit (pkgs) lib; llvm = pkgs.llvmPackages_21; # libpure: the Pure runtime and interpreter. Built from the fork with # LLVM 21; GMP and MPFR are runtime math dependencies. The fork hides # LLVM symbols from the shared library so it can coexist with a server # whose own JIT links a different LLVM (DESIGN.md section 5). libpure = pkgs.stdenv.mkDerivation { pname = "libpure"; version = "0.68-fork-${builtins.substring 0 7 pure-lang.rev}"; src = pure-lang; sourceRoot = "source/pure"; nativeBuildInputs = [ pkgs.autoreconfHook pkgs.pkg-config pkgs.bison pkgs.flex llvm.llvm.dev ]; buildInputs = [ pkgs.gmp pkgs.mpfr pkgs.readline llvm.llvm ]; # The configure script finds llvm-config-21 on PATH. preConfigure = '' export PATH=${llvm.llvm.dev}/bin:$PATH ''; configureFlags = [ "--with-llvm-version=21" ]; enableParallelBuilding = true; meta = with pkgs.lib; { description = "The Pure programming language runtime (gburd fork, embeddable)"; homepage = "https://github.com/gburd/pure-lang"; license = licenses.lgpl3Plus; platforms = platforms.unix; }; }; # pure-gsl: the GSL bindings, including gsl::linalg::cholesky_solve (the # compiled SPD solve pg_pure exposes). A plain Makefile that derives its # install dir from `pkg-config pure`, so point it at this libpure. pure-gsl = pkgs.stdenv.mkDerivation { pname = "pure-gsl"; version = libpure.version; src = pure-lang; sourceRoot = "source/pure-gsl"; nativeBuildInputs = [ pkgs.pkg-config pkgs.patchelf ]; buildInputs = [ libpure pkgs.gsl pkgs.gmp pkgs.mpfr ]; # Install into THIS derivation's $out, not into libpure's store path # (which is read-only). pure finds the module via PURELIB at runtime. makeFlags = [ "prefix=${placeholder "out"}" "libdir=${placeholder "out"}/lib" ]; installFlags = [ "prefix=${placeholder "out"}" "libdir=${placeholder "out"}/lib" ]; # The module's gsl.so dlopen's libgsl by bare name at runtime; give # it an RPATH to the gsl store path so it resolves without the caller # having to set LD_LIBRARY_PATH. postInstall = '' patchelf --set-rpath "${pkgs.gsl}/lib:${libpure}/lib" \ $out/lib/pure/gsl.so ''; meta = with pkgs.lib; { description = "GSL bindings for Pure, with a Cholesky/SPD solve in gsl::linalg"; homepage = "https://github.com/gburd/pure-lang"; license = licenses.gpl3Plus; platforms = platforms.unix; }; }; # A helper for the remaining vetted modules, which share the plain # Makefile shape: build against this libpure, install into $out, and # rpath any shared object back to its native libraries. mkPureModule = { name, buildInputs ? [], rpathLibs ? [] }: pkgs.stdenv.mkDerivation { pname = "pure-${name}"; version = libpure.version; src = pure-lang; sourceRoot = "source/pure-${name}"; nativeBuildInputs = [ pkgs.pkg-config pkgs.patchelf ]; buildInputs = [ libpure ] ++ buildInputs; makeFlags = [ "prefix=${placeholder "out"}" "libdir=${placeholder "out"}/lib" ]; installFlags = [ "prefix=${placeholder "out"}" "libdir=${placeholder "out"}/lib" ]; # Point any installed .so at libpure and its native libs so it # resolves without the caller setting LD_LIBRARY_PATH. postInstall = pkgs.lib.optionalString (rpathLibs != []) '' for so in $out/lib/pure/*.so; do [ -e "$so" ] || continue patchelf --set-rpath "${pkgs.lib.makeLibraryPath (rpathLibs ++ [ libpure ])}" "$so" done ''; meta = with pkgs.lib; { description = "Pure ${name} module (gburd fork)"; homepage = "https://github.com/gburd/pure-lang"; license = licenses.gpl3Plus; platforms = platforms.unix; }; }; # mpfr: arbitrary-precision floats (links libmpfr and gmp). pure-mpfr = mkPureModule { name = "mpfr"; buildInputs = [ pkgs.mpfr pkgs.gmp ]; rpathLibs = [ pkgs.mpfr pkgs.gmp ]; }; # stldict: STL-backed associative maps (C++). runtime.h pulls in # gmp.h and mpfr.h, so it needs those headers at build time. pure-stldict = mkPureModule { name = "stldict"; buildInputs = [ pkgs.gmp pkgs.mpfr ]; }; # rational: utilities on math's exact rationals (pure Pure, no .so). pure-rational = mkPureModule { name = "rational"; }; # A single prefix with libpure and the vetted modules merged, which is # what a host points PURE_PREFIX / PURELIB at. PURELIB must list both # the libpure prelude dir and the module dir. pure-with-modules = pkgs.symlinkJoin { name = "pure-with-modules-${libpure.version}"; paths = [ libpure pure-gsl pure-mpfr pure-stldict pure-rational ]; }; # The pg_pure source is the repository root; this flake lives in nix/, # so take the root from the flake's own source tree rather than ./.. pgPureSrc = self.sourceInfo.outPath; pgPureVersion = (builtins.fromJSON (builtins.readFile "${pgPureSrc}/META.json")).version; # PostgreSQL majors pg_pure supports. nixpkgs builds these with LLVM # JIT support; the llvmjit module itself is a separate `jit` output, # which the installcheck below adds so a JIT-compiled query and a Pure # call meet in one backend (the case t/001_crash_matrix.pl guards, # DESIGN.md section 5). pgVersions = { pg17 = pkgs.postgresql_17; pg18 = pkgs.postgresql_18; pg19 = pkgs.postgresql_19; }; # pg_pure for one PostgreSQL, built with PGXS against that server and # linked to pure-with-modules (the rpath in SHLIB_LINK points at it). # nixpkgs keeps pg_config in its own output, not in the server's bin/. # PGXS compiles with the compiler the server was built with (clang), # so use the server's own stdenv. Install into $out in the layout # postgresql.withPackages merges. mkPgPure = postgresql: postgresql.stdenv.mkDerivation { pname = "pg_pure-pg${lib.versions.major postgresql.version}"; version = pgPureVersion; src = pgPureSrc; nativeBuildInputs = [ postgresql.pg_config ]; buildInputs = [ postgresql pure-with-modules pkgs.gmp pkgs.mpfr ]; makeFlags = [ "PG_CONFIG=${postgresql.pg_config}/bin/pg_config" "PURE_PREFIX=${pure-with-modules}" ]; enableParallelBuilding = true; installPhase = '' runHook preInstall install -D -m 755 -t $out/lib pg_pure.so install -D -m 644 -t $out/share/postgresql/extension \ pg_pure.control pg_pureu.control pg_pure--*.sql pg_pureu--*.sql runHook postInstall ''; passthru = { inherit postgresql; }; meta = with lib; { description = "Pure as a PostgreSQL procedural language (pg_pure, pg_pureu)"; homepage = "https://github.com/gburd/pg_pure"; license = licenses.postgresql; platforms = platforms.linux; }; }; pgPure = builtins.mapAttrs (_: mkPgPure) pgVersions; # Both regression suites (pg_pureu, pg_pure) against a nixpkgs server # with pg_pure installed, jit forced on for every statement, and the # modules the suites use allow-listed. A backend crash is read from # the server log, never from a client exit code (AGENTS.md rule 2). # The TAP suite (t/*.pl) is not run here: nixpkgs' PostgreSQL ships # without the PostgreSQL::Test harness; ci/gate.sh runs it. mkInstallcheck = name: ext: # nixpkgs ships llvmjit in a separate `jit` output; include it, or the # server silently no-ops jit=on and the coexistence check is void. let pg = ext.postgresql.withPackages (_: [ ext ext.postgresql.jit ]); in pkgs.stdenv.mkDerivation { name = "pg_pure-installcheck-${name}-${pgPureVersion}"; src = pgPureSrc; nativeBuildInputs = [ pg ext.postgresql.pg_config ]; dontConfigure = true; dontInstall = true; buildPhase = '' runHook preBuild export PGDATA=$TMPDIR/pgdata PGHOST=$TMPDIR PGPORT=5999 PGUSER=postgres initdb -A trust -U postgres -D "$PGDATA" >/dev/null cat >> "$PGDATA/postgresql.conf" < "$TMPDIR/installcheck.out" 2>&1 || rc=$? cat "$TMPDIR/installcheck.out" # Prove the server's JIT was live, not a silent no-op: a # JIT-compiled query reports its function count in EXPLAIN. psql -X -tA -c "EXPLAIN (ANALYZE, FORMAT JSON) SELECT sum(g) FROM generate_series(1,100000) g" \ > "$TMPDIR/jit.json" pg_ctl -D "$PGDATA" -m fast -w stop if grep -E 'terminated by signal|PANIC' "$TMPDIR/server.log"; then echo "FAIL: a backend crashed during the regression suites"; exit 1 fi if [ "$rc" -ne 0 ]; then cat regression.diffs || true; exit "$rc"; fi grep -q '"JIT"' "$TMPDIR/jit.json" || { echo "FAIL: the server's JIT did not run"; exit 1; } mkdir -p $out cp "$TMPDIR/installcheck.out" "$TMPDIR/jit.json" $out/ runHook postBuild ''; }; in { packages = { inherit libpure pure-gsl pure-mpfr pure-stldict pure-rational pure-with-modules; pg_pure-pg17 = pgPure.pg17; pg_pure-pg18 = pgPure.pg18; pg_pure-pg19 = pgPure.pg19; default = pure-with-modules; }; # `nix flake check` builds every pg_pure package and runs both # regression suites against each PostgreSQL major. checks = { inherit (self.packages.${system}) pg_pure-pg17 pg_pure-pg18 pg_pure-pg19; installcheck-pg17 = mkInstallcheck "pg17" pgPure.pg17; installcheck-pg18 = mkInstallcheck "pg18" pgPure.pg18; installcheck-pg19 = mkInstallcheck "pg19" pgPure.pg19; }; # A shell with pure on PATH and the module search path set, for trying # `using gsl::linalg` interactively or building pg_pure against it. devShells.default = pkgs.mkShell { packages = [ pure-with-modules pkgs.gsl pkgs.gmp pkgs.mpfr ]; shellHook = '' export PURELIB=${pure-with-modules}/lib/pure export LD_LIBRARY_PATH=${pure-with-modules}/lib:${pkgs.gsl}/lib''${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} echo "pure $(${pure-with-modules}/bin/pure --version 2>/dev/null | head -1)" echo "PURE_PREFIX=${pure-with-modules} (point pg_pure's build here)" ''; }; }); }