|
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenv, |
| 4 | + fetchFromGitHub, |
| 5 | + postgresql, |
| 6 | + buildEnv, |
| 7 | + lz4, |
| 8 | + patchelf, |
| 9 | + duckdb-lib, |
| 10 | + latestOnly ? false, |
| 11 | +}: |
| 12 | +let |
| 13 | + pname = "pg_duckdb"; |
| 14 | + version = "1.1.1"; |
| 15 | + |
| 16 | + drv = stdenv.mkDerivation { |
| 17 | + inherit pname version; |
| 18 | + |
| 19 | + src = fetchFromGitHub { |
| 20 | + owner = "duckdb"; |
| 21 | + repo = "pg_duckdb"; |
| 22 | + rev = "v${version}"; |
| 23 | + hash = "sha256-0cNfDZkd6x45xpWyPMfFoYAklE+4lAjO02SjV+V/dxU="; |
| 24 | + }; |
| 25 | + |
| 26 | + nativeBuildInputs = lib.optionals (!stdenv.isDarwin) [ patchelf ]; |
| 27 | + |
| 28 | + buildInputs = [ |
| 29 | + postgresql |
| 30 | + duckdb-lib.lib |
| 31 | + duckdb-lib.dev |
| 32 | + lz4 |
| 33 | + ]; |
| 34 | + |
| 35 | + # No configure script — uses PGXS directly via Makefile.global |
| 36 | + dontConfigure = true; |
| 37 | + |
| 38 | + postPatch = '' |
| 39 | + # The GitHub tarball does not include the third_party/duckdb git submodule. |
| 40 | + # pg_duckdb's Makefile expects DuckDB headers at: |
| 41 | + # third_party/duckdb/src/include |
| 42 | + # third_party/duckdb/third_party/re2 |
| 43 | + # and the built library at: |
| 44 | + # third_party/duckdb/build/release/src/libduckdb<ext> |
| 45 | + # We satisfy all three using our pre-built duckdb-lib derivation. |
| 46 | +
|
| 47 | + mkdir -p third_party/duckdb/src |
| 48 | + ln -sf ${duckdb-lib.dev}/include third_party/duckdb/src/include |
| 49 | +
|
| 50 | + # pg_duckdb adds re2 as a system include for warning suppression only; |
| 51 | + # it does not include re2 headers directly. |
| 52 | + mkdir -p third_party/duckdb/third_party/re2 |
| 53 | +
|
| 54 | + # Make the pre-built libduckdb visible at the path the Makefile's linker |
| 55 | + # flags expect: -L$(DUCKDB_BUILD_DIR)/src -lduckdb |
| 56 | + # (DUCKDB_BUILD_DIR = third_party/duckdb/build/release) |
| 57 | + mkdir -p third_party/duckdb/build/release/src |
| 58 | + ln -sf ${duckdb-lib.lib}/lib/libduckdb${postgresql.dlSuffix} \ |
| 59 | + third_party/duckdb/build/release/src/libduckdb${postgresql.dlSuffix} |
| 60 | +
|
| 61 | + # The Makefile has two separate dependencies on .git/modules/third_party/duckdb/HEAD: |
| 62 | + # 1. $(FULL_DUCKDB_LIB) — recipe overridden to no-op below |
| 63 | + # 2. $(OBJS) — every .o depends on it directly (Makefile:98) to ensure |
| 64 | + # DuckDB headers are present before compilation. |
| 65 | + # Fake the sentinel so make considers it satisfied without running git. |
| 66 | + mkdir -p .git/modules/third_party/duckdb |
| 67 | + echo "ref: refs/heads/main" > .git/modules/third_party/duckdb/HEAD |
| 68 | +
|
| 69 | + # Override the FULL_DUCKDB_LIB build recipe with a no-op. |
| 70 | + # GNU Make uses the last-defined recipe when a target appears multiple times. |
| 71 | + # This prevents make from invoking cmake/ninja to build DuckDB from source; |
| 72 | + # the pre-built library is already in place via the symlink above. |
| 73 | + printf '\n# Nix override: skip DuckDB cmake build\n$(FULL_DUCKDB_LIB):\n\t@:\n' >> Makefile |
| 74 | + ''; |
| 75 | + |
| 76 | + makeFlags = [ |
| 77 | + "PG_CONFIG=${postgresql}/bin/pg_config" |
| 78 | + ]; |
| 79 | + |
| 80 | + installPhase = '' |
| 81 | + runHook preInstall |
| 82 | + mkdir -p $out/{lib,share/postgresql/extension} |
| 83 | +
|
| 84 | + install -Dm755 pg_duckdb${postgresql.dlSuffix} $out/lib/pg_duckdb${postgresql.dlSuffix} |
| 85 | +
|
| 86 | + # Fix rpath so pg_duckdb.so finds libduckdb in the Nix store at runtime. |
| 87 | + # PostgreSQL uses dlopen() to load extensions, so the rpath in the .so |
| 88 | + # file must point to an absolute path where libduckdb lives. |
| 89 | + ${lib.optionalString (!stdenv.isDarwin) '' |
| 90 | + ${patchelf}/bin/patchelf \ |
| 91 | + --set-rpath "${duckdb-lib.lib}/lib:${postgresql}/lib" \ |
| 92 | + $out/lib/pg_duckdb${postgresql.dlSuffix} |
| 93 | + ''} |
| 94 | + ${lib.optionalString stdenv.isDarwin '' |
| 95 | + install_name_tool \ |
| 96 | + -add_rpath "${duckdb-lib.lib}/lib" \ |
| 97 | + -add_rpath "${postgresql}/lib" \ |
| 98 | + $out/lib/pg_duckdb${postgresql.dlSuffix} |
| 99 | + ''} |
| 100 | +
|
| 101 | + cp pg_duckdb.control $out/share/postgresql/extension/ |
| 102 | + cp sql/pg_duckdb--*.sql $out/share/postgresql/extension/ |
| 103 | +
|
| 104 | + runHook postInstall |
| 105 | + ''; |
| 106 | + |
| 107 | + meta = { |
| 108 | + description = "DuckDB-powered analytical queries inside PostgreSQL"; |
| 109 | + homepage = "https://github.com/duckdb/pg_duckdb"; |
| 110 | + platforms = postgresql.meta.platforms; |
| 111 | + license = lib.licenses.mit; |
| 112 | + }; |
| 113 | + }; |
| 114 | +in |
| 115 | +buildEnv { |
| 116 | + name = pname; |
| 117 | + paths = [ drv ]; |
| 118 | + pathsToLink = [ |
| 119 | + "/lib" |
| 120 | + "/share/postgresql/extension" |
| 121 | + ]; |
| 122 | + |
| 123 | + passthru = { |
| 124 | + inherit pname version latestOnly; |
| 125 | + }; |
| 126 | +} |
0 commit comments