summaryrefslogtreecommitdiff
path: root/pkgs/servers/sql/postgresql/ext/pg_duckdb.nix
blob: c761d5a1cab35998311b5e0951eddbb632385e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
  lib,
  fetchFromGitHub,
  writeText,

  postgresqlBuildExtension,
  postgresqlTestExtension,
  postgresql,

  openssl,
  curl,

  cmake,
  ninja,
  pkg-config,
  python3,
  git,
  which,
}:

postgresqlBuildExtension (finalAttrs: {
  pname = "pg_duckdb";
  version = "1.1.1";

  # duckdbVersionFull is used to set OVERRIDE_GIT_DESCRIBE, which effectively suppresses
  # build script attempts to use git to figure it out.
  # To get the version first run `git submodule update --init --recursive` inside pg_duckdb/,
  # then run `git describe --tags --long --match "v*.*.*"` inside pg_duckdb/third_party/duckdb
  duckdbVersion = "1.4.3";
  duckdbVersionFull = "${finalAttrs.duckdbVersion}-0-gd1dc88f950";

  src = fetchFromGitHub {
    owner = "duckdb";
    repo = "pg_duckdb";
    tag = "v${finalAttrs.version}";
    fetchSubmodules = true;
    hash = "sha256-B/9U1j29zqNMNgK2t2MFJemCrLgQo1qRrCacSjPzYdg=";
  };

  nativeBuildInputs = [
    cmake
    ninja
    pkg-config
    python3
    git
    which
  ];

  # Avoid build errors by suppressing these hooks.
  # Extension build scripts drive cmake and ninja themselves.
  dontUseCmakeConfigure = true;
  dontUseNinjaBuild = true;
  dontUseNinjaInstall = true;
  dontUseNinjaCheck = true;

  # curl is required by httpfs (duckdb-httpfs/vcpkg.json), but not included in postgresql.buildInputs
  buildInputs = postgresql.buildInputs ++ [
    openssl
    curl
  ];

  # 1. Disable calling `git submodule update --init --recursive` from Makefile:
  # submodules are already in place thanks to fetchFromGitHub's fetchSubmodules option.
  # 2. duckdb-httpfs prefers to use vcpkg to build its dependencies (see
  # duckdb-httpfs/README.md), but this derivation won't run vcpkg, so the
  # libraries are linked dynamically. But because the -lssl and -lcrypto flags
  # are missing, libssl/libcrypto symbols can't be resolved during the link time.
  postPatch = ''
    substituteInPlace Makefile \
      --replace-fail \
        'git submodule update --init --recursive' \
        'true'

    substituteInPlace Makefile \
      --replace-fail \
        'PG_DUCKDB_LINK_FLAGS += -Wl,-rpath,$(PG_LIB)/ -L$(DUCKDB_BUILD_DIR)/src -L$(PG_LIB) -lstdc++ -llz4' \
        'PG_DUCKDB_LINK_FLAGS += -Wl,-rpath,$(PG_LIB)/ -L$(DUCKDB_BUILD_DIR)/src -L$(PG_LIB) -lstdc++ -llz4 -lssl -lcrypto'
  '';

  # Download httpfs extension source code and override pg_duckdb's attempt to
  # get it using git in pg_duckdb_extensions.cmake by using EXTENSION_CONFIGS
  httpfsSrc = fetchFromGitHub {
    owner = "duckdb";
    repo = "duckdb-httpfs";
    rev = "9c7d34977b10346d0b4cbbde5df807d1dab0b2bf";
    fetchSubmodules = true;
    hash = "sha256-/gR99nrks2nRmfk1ypZCSAKpok1DGizXgNz0u5Bw3Jk=";
  };

  makeFlags =
    let
      httpfsCmake = writeText "pg_duckdb_httpfs.cmake" ''
        duckdb_extension_load(httpfs
          SOURCE_DIR ${finalAttrs.httpfsSrc}
          EXTENSION_VERSION v${finalAttrs.duckdbVersion}
        )
      '';
    in
    [
      "GEN=ninja"
      "DUCKDB_BUILD=ReleaseStatic"
      "PG_CONFIG=${postgresql.pg_config}/bin/pg_config"
      "DUCKDB_VERSION=v${finalAttrs.duckdbVersionFull}"
      "EXTENSION_CONFIGS=${httpfsCmake};../pg_duckdb_extensions.cmake"
    ];

  passthru.tests.extension = postgresqlTestExtension {
    inherit (finalAttrs) finalPackage;
    postgresqlExtraSettings = ''
      shared_preload_libraries = 'pg_duckdb'
    '';
    sql = ''
      CREATE EXTENSION pg_duckdb;
      SELECT duckdb.raw_query('SELECT 42');
    '';
  };

  meta = {
    description = "DuckDB-powered Postgres extension for high-performance analytics";
    homepage = "https://github.com/duckdb/pg_duckdb";
    changelog = "https://github.com/duckdb/pg_duckdb/releases/tag/v${finalAttrs.version}";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ skonotopov ];
    platforms = lib.platforms.linux ++ lib.platforms.darwin;
  };
})