|
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenv, |
| 4 | + fetchurl, |
| 5 | + pkg-config, |
| 6 | + postgresql, |
| 7 | + msgpack-c, |
| 8 | + callPackage, |
| 9 | + mecab, |
| 10 | + makeWrapper, |
| 11 | + xxHash, |
| 12 | + buildEnv, |
| 13 | +}: |
| 14 | +let |
| 15 | + pname = "pgroonga"; |
| 16 | + |
| 17 | + # Load version configuration from external file |
| 18 | + allVersions = (builtins.fromJSON (builtins.readFile ../versions.json)).${pname}; |
| 19 | + |
| 20 | + # Filter versions compatible with current PostgreSQL version |
| 21 | + supportedVersions = lib.filterAttrs ( |
| 22 | + _: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql |
| 23 | + ) allVersions; |
| 24 | + |
| 25 | + # Derived version information |
| 26 | + versions = lib.naturalSort (lib.attrNames supportedVersions); |
| 27 | + latestVersion = lib.last versions; |
| 28 | + numberOfVersions = builtins.length versions; |
| 29 | + packages = builtins.attrValues ( |
| 30 | + lib.mapAttrs (name: value: build name value.hash) supportedVersions |
| 31 | + ); |
| 32 | + |
| 33 | + # List of C extensions to be included in the build |
| 34 | + cExtensions = [ |
| 35 | + "pgroonga" |
| 36 | + "pgroonga_database" |
| 37 | + ]; |
| 38 | + |
| 39 | + # Build function for individual versions |
| 40 | + build = |
| 41 | + version: hash: |
| 42 | + let |
| 43 | + supabase-groonga = callPackage ./supabase-groonga-14.0.5.nix { }; |
| 44 | + mecab-naist-jdic = callPackage ../mecab-naist-jdic { }; |
| 45 | + in |
| 46 | + stdenv.mkDerivation rec { |
| 47 | + inherit pname version; |
| 48 | + |
| 49 | + src = fetchurl { |
| 50 | + url = "https://packages.groonga.org/source/${pname}/${pname}-${version}.tar.gz"; |
| 51 | + inherit hash; |
| 52 | + }; |
| 53 | + nativeBuildInputs = [ |
| 54 | + pkg-config |
| 55 | + makeWrapper |
| 56 | + ]; |
| 57 | + |
| 58 | + buildInputs = [ |
| 59 | + postgresql |
| 60 | + msgpack-c |
| 61 | + supabase-groonga |
| 62 | + mecab |
| 63 | + ] ++ lib.optionals stdenv.isDarwin [ xxHash ]; |
| 64 | + |
| 65 | + propagatedBuildInputs = [ |
| 66 | + supabase-groonga |
| 67 | + mecab-naist-jdic |
| 68 | + ]; |
| 69 | + configureFlags = [ |
| 70 | + "--with-mecab=${mecab}" |
| 71 | + "--enable-mecab" |
| 72 | + "--with-groonga=${supabase-groonga}" |
| 73 | + "--with-groonga-plugin-dir=${supabase-groonga}/lib/groonga/plugins" |
| 74 | + ]; |
| 75 | + |
| 76 | + makeFlags = [ |
| 77 | + "HAVE_MSGPACK=1" |
| 78 | + "MSGPACK_PACKAGE_NAME=msgpack-c" |
| 79 | + "HAVE_MECAB=1" |
| 80 | + ]; |
| 81 | + |
| 82 | + NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isDarwin ( |
| 83 | + builtins.concatStringsSep " " [ |
| 84 | + "-Wno-error=incompatible-function-pointer-types" |
| 85 | + "-Wno-error=format" |
| 86 | + "-Wno-format" |
| 87 | + "-I${supabase-groonga}/include/groonga" |
| 88 | + "-I${xxHash}/include" |
| 89 | + "-DPGRN_VERSION=\"${version}\"" |
| 90 | + ] |
| 91 | + ); |
| 92 | + |
| 93 | + preConfigure = '' |
| 94 | + export GROONGA_LIBS="-L${supabase-groonga}/lib -lgroonga" |
| 95 | + export GROONGA_CFLAGS="-I${supabase-groonga}/include" |
| 96 | + export MECAB_CONFIG="${mecab}/bin/mecab-config" |
| 97 | + export MECAB_DICDIR="${mecab-naist-jdic}/lib/mecab/dic/naist-jdic" |
| 98 | + ${lib.optionalString stdenv.isDarwin '' |
| 99 | + export CPPFLAGS="-I${supabase-groonga}/include/groonga -I${xxHash}/include -DPGRN_VERSION=\"${version}\"" |
| 100 | + export CFLAGS="-I${supabase-groonga}/include/groonga -I${xxHash}/include -DPGRN_VERSION=\"${version}\"" |
| 101 | + export PG_CPPFLAGS="-Wno-error=incompatible-function-pointer-types -Wno-error=format" |
| 102 | + ''} |
| 103 | + ''; |
| 104 | + |
| 105 | + installPhase = '' |
| 106 | + runHook preInstall |
| 107 | +
|
| 108 | + mkdir -p $out/{lib,share/postgresql/extension} |
| 109 | +
|
| 110 | + for ext in ${lib.concatStringsSep " " cExtensions}; do |
| 111 | + # Install shared library with version suffix |
| 112 | + mv $ext${postgresql.dlSuffix} $out/lib/$ext-${version}${postgresql.dlSuffix} |
| 113 | +
|
| 114 | + # Create version-specific control file |
| 115 | + sed -e "/^default_version =/d" \ |
| 116 | + -e "s|^module_pathname = .*|module_pathname = '\$libdir/$ext'|" \ |
| 117 | + $ext.control > $out/share/postgresql/extension/$ext--${version}.control |
| 118 | +
|
| 119 | + # Copy SQL file to install the specific version |
| 120 | + cp data/$ext--${version}.sql $out/share/postgresql/extension |
| 121 | +
|
| 122 | + # Create versioned control file with modified module path |
| 123 | + sed -e "/^default_version =/d" \ |
| 124 | + -e "s|^module_pathname = .*|module_pathname = '\$libdir/$ext'|" \ |
| 125 | + $ext.control > $out/share/postgresql/extension/$ext--${version}.control |
| 126 | +
|
| 127 | + # For the latest version, create default control file and symlink and copy SQL upgrade scripts |
| 128 | + if [[ "${version}" == "${latestVersion}" ]]; then |
| 129 | + { |
| 130 | + echo "default_version = '${version}'" |
| 131 | + cat $out/share/postgresql/extension/$ext--${version}.control |
| 132 | + } > $out/share/postgresql/extension/$ext.control |
| 133 | + ln -sfn $ext-${version}${postgresql.dlSuffix} $out/lib/$ext${postgresql.dlSuffix} |
| 134 | + cp data/$ext--*--*.sql $out/share/postgresql/extension |
| 135 | + fi |
| 136 | + done |
| 137 | + ''; |
| 138 | + |
| 139 | + meta = with lib; { |
| 140 | + description = "A PostgreSQL extension to use Groonga as the index"; |
| 141 | + longDescription = '' |
| 142 | + PGroonga is a PostgreSQL extension to use Groonga as the index. |
| 143 | + PostgreSQL supports full text search against languages that use only alphabet and digit. |
| 144 | + It means that PostgreSQL doesn't support full text search against Japanese, Chinese and so on. |
| 145 | + You can use super fast full text search feature against all languages by installing PGroonga into your PostgreSQL. |
| 146 | + ''; |
| 147 | + homepage = "https://pgroonga.github.io/"; |
| 148 | + changelog = "https://github.com/pgroonga/pgroonga/releases/tag/${version}"; |
| 149 | + license = licenses.postgresql; |
| 150 | + inherit (postgresql.meta) platforms; |
| 151 | + }; |
| 152 | + }; |
| 153 | +in |
| 154 | +buildEnv { |
| 155 | + name = pname; |
| 156 | + paths = packages; |
| 157 | + |
| 158 | + pathsToLink = [ |
| 159 | + "/lib" |
| 160 | + "/share/postgresql/extension" |
| 161 | + ]; |
| 162 | + postBuild = '' |
| 163 | + # Verify all expected library files are present |
| 164 | + expectedFiles=${toString ((numberOfVersions + 1) * (builtins.length cExtensions))} |
| 165 | + actualFiles=$(ls -l $out/lib/${pname}*${postgresql.dlSuffix} | wc -l) |
| 166 | +
|
| 167 | + if [[ "$actualFiles" != "$expectedFiles" ]]; then |
| 168 | + echo "Error: Expected $expectedFiles library files, found $actualFiles" |
| 169 | + echo "Files found:" |
| 170 | + ls -la $out/lib/*${postgresql.dlSuffix} || true |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | + ''; |
| 174 | + |
| 175 | + passthru = { |
| 176 | + inherit versions numberOfVersions; |
| 177 | + pname = "${pname}-all"; |
| 178 | + version = |
| 179 | + "multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions); |
| 180 | + }; |
| 181 | +} |
0 commit comments