···11+{22+ buildPgrxExtension,33+ cargo-pgrx_0_14_1,44+ clang,55+ fetchFromGitHub,66+ lib,77+ nix-update-script,88+ postgresql,99+ postgresqlTestExtension,1010+ replaceVars,1111+ rust-jemalloc-sys,1212+ stdenv,1313+}:1414+let1515+ buildPgrxExtension' = buildPgrxExtension.override {1616+ # Upstream only works with a fixed version of cargo-pgrx for each release,1717+ # so we're pinning it here to avoid future incompatibility.1818+ cargo-pgrx = cargo-pgrx_0_14_1;1919+ };2020+2121+ # Follow upstream and use rust-jemalloc-sys on linux aarch64 and x86_642222+ # Additionally, disable init exec TLS, since it causes issues with postgres.2323+ # https://github.com/tensorchord/VectorChord/blob/0.4.2/Cargo.toml#L43-L442424+ useSystemJemalloc =2525+ stdenv.hostPlatform.isLinux && (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64);2626+ rust-jemalloc-sys' = (2727+ rust-jemalloc-sys.override (old: {2828+ jemalloc = old.jemalloc.override { disableInitExecTls = true; };2929+ })3030+ );3131+in3232+buildPgrxExtension' (finalAttrs: {3333+ inherit postgresql;3434+3535+ pname = "vectorchord";3636+ version = "0.4.2";3737+3838+ src = fetchFromGitHub {3939+ owner = "tensorchord";4040+ repo = "vectorchord";4141+ tag = finalAttrs.version;4242+ hash = "sha256-EdMuSNcWwCBsAY0e3d0WVug1KBWYWldvKStF6cf/uRs=";4343+ };4444+4545+ patches = [4646+ # Tell the `simd` crate to use the flags from the rust bindgen hook4747+ (replaceVars ./0001-read-clang-flags-from-environment.diff {4848+ clang = lib.getExe clang;4949+ })5050+ # Add feature flags needed for features not yet stabilised in rustc stable5151+ ./0002-add-feature-flags.diff5252+ # The select_predictable function has been moved from std::bool to std::hint before it has been stabilized.5353+ # This move isn't present in rustc 1.87, but upstream is using nightly so they have already updated their code.5454+ # This patch changes the code to use the function on std::bool instead.5555+ # See https://github.com/rust-lang/rust/pull/1397265656+ ./0003-select_unpredictable-on-bool.diff5757+ ];5858+5959+ buildInputs = lib.optionals (useSystemJemalloc) [6060+ rust-jemalloc-sys'6161+ ];6262+6363+ useFetchCargoVendor = true;6464+ cargoHash = "sha256-8NwfsJn5dnvog3fexzLmO3v7/3+L7xtv+PHWfCCWoHY=";6565+6666+ # Include upgrade scripts in the final package6767+ # https://github.com/tensorchord/VectorChord/blob/0.4.2/crates/make/src/main.rs#L2246868+ postInstall = ''6969+ cp sql/upgrade/* $out/share/postgresql/extension/7070+ '';7171+7272+ env = {7373+ # Bypass rust nightly features not being available on rust stable7474+ RUSTC_BOOTSTRAP = 1;7575+ };7676+7777+ # This crate does not have the "pg_test" feature7878+ usePgTestCheckFeature = false;7979+8080+ passthru = {8181+ updateScript = nix-update-script { };8282+8383+ tests.extension = postgresqlTestExtension {8484+ inherit (finalAttrs) finalPackage;8585+ withPackages = [ "pgvector" ]; # vectorchord depends on pgvector at runtime8686+ postgresqlExtraSettings = ''8787+ shared_preload_libraries = 'vchord'8888+ '';8989+9090+ sql = ''9191+ CREATE EXTENSION vchord CASCADE;9292+9393+ CREATE TABLE items (id bigint PRIMARY KEY, embedding vector(3));9494+ INSERT INTO items (id, embedding) VALUES9595+ (1, '[1,2,4]'),9696+ (2, '[1,2,5]'),9797+ (3, '[0,0,3]'),9898+ (4, '[0,0,2]'),9999+ (5, '[0,0,1]');100100+101101+ CREATE INDEX ON items USING vchordrq (embedding vector_l2_ops) WITH (options = $$102102+ residual_quantization = true103103+ [build.internal]104104+ lists = [4096]105105+ spherical_centroids = false106106+ $$);107107+108108+ SET vchordrq.probes = 1;109109+ '';110110+111111+ asserts = [112112+ {113113+ query = "SELECT extversion FROM pg_extension WHERE extname = 'vchord'";114114+ expected = "'${finalAttrs.version}'";115115+ description = "Expected installed version to match the derivation's version";116116+ }117117+ {118118+ query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 1";119119+ expected = "1";120120+ description = "Expected vector of row with ID=1 to have an euclidean distance from [1,2,3] of 1.";121121+ }122122+ {123123+ query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 2";124124+ expected = "2";125125+ description = "Expected vector of row with ID=2 to have an euclidean distance from [1,2,3] of 2.";126126+ }127127+ {128128+ query = "SELECT id FROM items ORDER BY embedding <-> '[2,3,7]' LIMIT 1";129129+ expected = "2";130130+ description = "Expected vector of row with ID=2 to be the closest to [2,3,7].";131131+ }132132+ ];133133+ };134134+ };135135+136136+ meta = {137137+ changelog = "https://github.com/tensorchord/VectorChord/releases/tag/${finalAttrs.version}";138138+ description = "Scalable, fast, and disk-friendly vector search in Postgres, the successor of pgvecto.rs";139139+ homepage = "https://github.com/tensorchord/VectorChord";140140+ license = lib.licenses.agpl3Only; # dual licensed with Elastic License v2 (ELv2)141141+ maintainers = with lib.maintainers; [142142+ diogotcorreia143143+ ];144144+ platforms = postgresql.meta.platforms;145145+ };146146+})