nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 buildPgrxExtension,
3 cargo-pgrx_0_16_0,
4 fetchFromGitHub,
5 lib,
6 nix-update-script,
7 postgresql,
8 postgresqlTestExtension,
9}:
10buildPgrxExtension (finalAttrs: {
11 inherit postgresql;
12 cargo-pgrx = cargo-pgrx_0_16_0;
13
14 pname = "vectorchord";
15 version = "1.0.0";
16
17 src = fetchFromGitHub {
18 owner = "tensorchord";
19 repo = "vectorchord";
20 tag = finalAttrs.version;
21 hash = "sha256-+BOuiinbKPZZaDl9aYsIoZPgvLZ4FA6Rb4/W+lAz4so=";
22 };
23
24 cargoHash = "sha256-kwe2x7OTjpdPonZsvnR1C/89D5W/R5JswYF79YcSFEA=";
25
26 # Include upgrade scripts in the final package
27 # https://github.com/tensorchord/VectorChord/blob/0.5.0/crates/make/src/main.rs#L366
28 postInstall = ''
29 cp sql/upgrade/* $out/share/postgresql/extension/
30 '';
31
32 # This crate does not have the "pg_test" feature
33 usePgTestCheckFeature = false;
34
35 passthru = {
36 updateScript = nix-update-script { };
37
38 tests.extension = postgresqlTestExtension {
39 inherit (finalAttrs) finalPackage;
40 withPackages = [ "pgvector" ]; # vectorchord depends on pgvector at runtime
41 postgresqlExtraSettings = ''
42 shared_preload_libraries = 'vchord'
43 '';
44
45 sql = ''
46 CREATE EXTENSION vchord CASCADE;
47
48 CREATE TABLE items (id bigint PRIMARY KEY, embedding vector(3));
49 INSERT INTO items (id, embedding) VALUES
50 (1, '[1,2,4]'),
51 (2, '[1,2,5]'),
52 (3, '[0,0,3]'),
53 (4, '[0,0,2]'),
54 (5, '[0,0,1]');
55
56 CREATE INDEX ON items USING vchordrq (embedding vector_l2_ops) WITH (options = $$
57 residual_quantization = true
58 [build.internal]
59 lists = [4096]
60 spherical_centroids = false
61 $$);
62
63 SET vchordrq.probes = 1;
64 '';
65
66 asserts = [
67 {
68 query = "SELECT extversion FROM pg_extension WHERE extname = 'vchord'";
69 expected = "'${finalAttrs.version}'";
70 description = "Expected installed version to match the derivation's version";
71 }
72 {
73 query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 1";
74 expected = "1";
75 description = "Expected vector of row with ID=1 to have an euclidean distance from [1,2,3] of 1.";
76 }
77 {
78 query = "SELECT id FROM items WHERE embedding <-> '[1,2,3]' = 2";
79 expected = "2";
80 description = "Expected vector of row with ID=2 to have an euclidean distance from [1,2,3] of 2.";
81 }
82 {
83 query = "SELECT id FROM items ORDER BY embedding <-> '[2,3,7]' LIMIT 1";
84 expected = "2";
85 description = "Expected vector of row with ID=2 to be the closest to [2,3,7].";
86 }
87 ];
88 };
89 };
90
91 meta = {
92 changelog = "https://github.com/tensorchord/VectorChord/releases/tag/${finalAttrs.version}";
93 description = "Scalable, fast, and disk-friendly vector search in Postgres, the successor of pgvecto.rs";
94 homepage = "https://github.com/tensorchord/VectorChord";
95 license = lib.licenses.agpl3Only; # dual licensed with Elastic License v2 (ELv2)
96 maintainers = with lib.maintainers; [
97 diogotcorreia
98 ];
99 platforms = postgresql.meta.platforms;
100 };
101})