Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgresqlPackages.pgvecto-rs: init at 0.2.1 #281192

Merged
merged 3 commits into from
Mar 11, 2024
Merged

Conversation

diogotcorreia
Copy link
Member

@diogotcorreia diogotcorreia commented Jan 15, 2024

Description of changes

Description: Scalable Vector Search in Postgres. Revolutionize Vector Search, not Database.
https://github.com/tensorchord/pgvecto.rs

Used for Immich.

Closes #274509

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

I have not tested this with Immich before, but as per #274509 (comment), it seems like this version would not work (because of upstream).

Keeping this as a draft until Immich moves to a more recent pgvecto.rs version.

Edit: see #281192 (comment) (next version of Immich will use pgvecto.rs v0.2.0)


Add a 👍 reaction to pull requests you find important.

@diogotcorreia
Copy link
Member Author

It looks like v0.2.0 has been released and that Immich is moving to that version of pgvecto.rs in the next release: immich-app/immich#6785

I'll be working tomorrow to get this ready in time for the release!

@diogotcorreia diogotcorreia changed the title pgvecto.rs: init at 0.1.14-beta pgvecto.rs: init at 0.2.0 Feb 11, 2024
@diogotcorreia diogotcorreia marked this pull request as ready for review February 11, 2024 16:01
@ofborg ofborg bot added 8.has: package (new) This PR adds a new package 11.by: package-maintainer This PR was created by the maintainer of the package it changes 10.rebuild-darwin: 11-100 10.rebuild-linux: 11-100 labels Feb 11, 2024
Copy link
Member

@Atemu Atemu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already looks quite good. A couple minor things.

I'd also like to know how you test this works as expected.

# avoid future incompatibility.
# See https://docs.pgvecto.rs/developers/development.html#environment, step 4
rustPlatform' = rustPlatform // {
inherit (callPackage ../../../../../build-support/rust/hooks {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relative paths are not part of a package's interface. We'll have to find a different solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I'll investigate alternative solutions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a way to override it. This is what I've tried:

nix-repl> pkgs.rustPlatform.bindgenHook.override { clang = pkgs.clang_17; }
error:
       … while calling a functor (an attribute set with a '__functor' attribute)

         at «string»:1:1:

            1| pkgs.rustPlatform.bindgenHook.override { clang = pkgs.clang_17; }
             | ^

       … while calling a functor (an attribute set with a '__functor' attribute)

         at /home/dtc/documents/vcs/nixpkgs/lib/customisation.nix:104:43:

          103|       # Re-call the function but with different arguments
          104|       overrideArgs = mirrorArgs (newArgs: makeOverridable f (overrideWith newArgs));
             |                                           ^
          105|       # Change the result of the function call by applying g to it

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: function 'anonymous lambda' called with unexpected argument 'clang'

       at /home/dtc/documents/vcs/nixpkgs/pkgs/build-support/rust/hooks/default.nix:92:32:

           91|
           92|     bindgenHook = callPackage ({}: makeSetupHook {
             |                                ^
           93|       name = "rust-bindgen-hook";

nix-repl> (pkgs.makeRustPlatform {rustc = pkgs.rustc; cargo = pkgs.cargo; clang = pkgs.clang_17;}).bindgenHook.clang
«derivation /nix/store/j16048gsic45ijdpw82172pfxdz9nw2w-clang-wrapper-16.0.6.drv»

For now, I'll setup a similar script just for this package that supports pinning the clang version, but this should be revisited at a later date to allow overriding this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a bug. It can be fixed with this:

diff --git a/pkgs/development/compilers/rust/make-rust-platform.nix b/pkgs/development/compilers/rust/make-rust-platform.nix
index e22cb6d594af..6ed724aae821 100644
--- a/pkgs/development/compilers/rust/make-rust-platform.nix
+++ b/pkgs/development/compilers/rust/make-rust-platform.nix
@@ -1,4 +1,4 @@
-{ lib, buildPackages, callPackage, cargo-auditable, stdenv, runCommand }@prev:
+{ lib, buildPackages, callPackage, callPackages, cargo-auditable, stdenv, runCommand }@prev:
 
 { rustc
 , cargo
@@ -34,7 +34,7 @@ rec {
   };
 
   # Hooks
-  inherit (callPackage ../../../build-support/rust/hooks {
+  inherit (callPackages ../../../build-support/rust/hooks {
     inherit stdenv cargo rustc;
   }) cargoBuildHook cargoCheckHook cargoInstallHook cargoNextestHook cargoSetupHook maturinBuildHook bindgenHook;
 }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll investigate that later then, thanks :)
Should this be made in another PR, or does it make sense to include it in this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to include here; it's a requirement for this to work. If you needed to refactor something else for this to work, you'd also put it here.

If this was to stall because of something related to pgvector directly, I'd advocate for spinning the refactors out into their own PRs but we'll cross that bridge when we get there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, it seems to work fine in the repl. I've tested it by overriding clang to 17 and having it fail the build.
I'm just finishing building the package to make sure everything works fine, and then I'll push.
This will likely trigger a lot of package rebuilds in the CI, but we'll see 🙃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relative paths are not part of a package's interface. We'll have to find a different solution.

Sorry, I didn't understand that comment, and I'm curious about this... Why is the relative path a problem? Also, for example, pkgs/os-specific/darwin/apple-sdk-11.0/default.nix does something similar:

    rustPlatform = pkgs.makeRustPlatform {
      inherit (pkgs.darwin.apple_sdk_11_0) stdenv;
      inherit (pkgs) rustc cargo;
    } // {
      inherit (pkgs.callPackage ../../../build-support/rust/hooks {
        inherit (pkgs.darwin.apple_sdk_11_0) stdenv;
        inherit (pkgs) cargo rustc;
        clang = mkCc pkgs.clang;
      }) bindgenHook;
    };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relative paths are not guaranteed to remain stable.

People who reorganise one file in nixpkgs should not be expected to check and adjust relative paths in all other files.

This is relevant as we'll soon see the great RFC140 migration.

Now that the hooks override is fixed, the the apple SDK should use this style aswell as it doesn't have this obvious issue. (It may still have less obvious issues.)

@github-actions github-actions bot added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label Feb 19, 2024
@PhilippWoelfel
Copy link
Contributor

Can you add the source files from sql/upgrade to the output? E.g.,

  postInstall = ''
    cp sql/upgrade/* $out/share/postgresql/extension/
  '';

@diogotcorreia
Copy link
Member Author

@PhilippWoelfel that was also in my todo list as well, yes :)
I was still going to investigate how they do it on their CI, since the normal build process doesn't seem to include them.

I'm not sure if I'm going to be able to finish the remaining problems this with PR this weekend, but I'll do my best 😅

@diogotcorreia
Copy link
Member Author

I think it should be ready now :) 🤞

Changes I've made:

  • Check extension version in the tests
  • Properly pinned clang 16 by setting an env variable in preBuild
  • Changed env variable name in patch 0001 to C_CRATE_EXTRA_CLANG_ARGS to avoid conflict with rustPlatform.bindgenHook
  • Replaced use of sed with substituteInPlace as per the review
  • Included the upgrade SQL scripts (thanks @PhilippWoelfel !)
  • Updated package description as per upstream

Since I'm force pushing, here's the diff: https://bin.diogotc.com/xuzecylyju.diff

@Atemu

This comment was marked as off-topic.

Comment on lines +20 to +24
rustPlatform' = rustPlatform // {
bindgenHook = rustPlatform.bindgenHook.override { inherit clang; };
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer a full makeRustPlatform call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, but it seems to not be overriding as expected.

nix-repl> (pkgs.makeRustPlatform {rustc = pkgs.rustc; cargo = pkgs.cargo; clang = pkgs.clang_17;}).bindgenHook.clang
«derivation /nix/store/j16048gsic45ijdpw82172pfxdz9nw2w-clang-wrapper-16.0.6.drv»

Am I doing it wrong?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makeRustPlatform doesn't take a clang arg but does accept arbitrary args, so it doesn't throw an error to do this.

I don't see another option short of refactoring makeRustPlatform then.

I really don't know how this all works together; doesn't rust internally depend on clang aswell? Shouldnt'y you need to override that aswell?

I'd like input of the rust people on this. It appears they were pinged already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what we do for firefox:

buildStdenv = overrideCC llvmPackages.stdenv (llvmPackages.stdenv.cc.override {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it also applies to this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mic92 Thanks for you help, but it seems that it still doesn't work. Let me know if I'm using this wrong:

nix-repl> (pkgs.makeRustPlatform { inherit (pkgs) rustc cargo; stdenv = pkgs.overrideCC pkgs.llvmPackages.stdenv (llvmPackages.stdenv.override { cc = pkgs.clang_17; }); }).bindgenHook.clang
«derivation /nix/store/c9ym6jk1pf0bx62j29g51k00pp16zf9l-clang-wrapper-16.0.6.drv»

nix-repl> (pkgs.makeRustPlatform { inherit (pkgs) rustc cargo; stdenv = pkgs.overrideCC pkgs.llvmPackages.stdenv.override { cc = pkgs.clang_17; }; }).bindgenHook.clang
«derivation /nix/store/c9ym6jk1pf0bx62j29g51k00pp16zf9l-clang-wrapper-16.0.6.drv»

nix-repl> (pkgs.makeRustPlatform { inherit (pkgs) rustc cargo; stdenv = pkgs.llvmPackages.stdenv.override { cc = pkgs.clang_17; }; }).bindgenHook.clang
«derivation /nix/store/c9ym6jk1pf0bx62j29g51k00pp16zf9l-clang-wrapper-16.0.6.drv»

For context, this is what I want to achieve (this is the current solution in the code):

nix-repl> (pkgs.rustPlatform.bindgenHook.override { clang = pkgs.clang_17; }).clang
«derivation /nix/store/cxxk9njrx1zm5ynmsi0wyy9dv58h2zci-clang-wrapper-17.0.6.drv»

(Also, I do want clang 16, but I want to pin it; I'm just using clang 17 in the repl to make sure it is being overridden)

Is there something I'm overlooking?

nixos/tests/pgvecto-rs.nix Outdated Show resolved Hide resolved
@diogotcorreia
Copy link
Member Author

I've added the requested changes, updated to 0.2.1, as well as incorporated some changes from the other PR that were missing from here.
Additionally, the patch 0002 is not needed anymore apparently, so I've removed it.

I've ran nix-build -A nixosTests.pgvecto-rs.postgresql_14 locally and it passed the tests.

Let me know if any other change in needed, otherwise I'll squash these commits into the other ones.

@diogotcorreia diogotcorreia changed the title postgresqlPackages.pgvecto-rs: init at 0.2.0 postgresqlPackages.pgvecto-rs: init at 0.2.1 Mar 7, 2024
Previously, trying to use `.override {}` on one of the hooks,
specifically `bindgenHook` would yield in an error.
By replacing `callPackage` with `callPackages`, this error is fixed and
the overrides are propagated to the hooks.

Co-Authored-By: Atemu <[email protected]>
@diogotcorreia
Copy link
Member Author

diogotcorreia commented Mar 7, 2024

Ofborg was failing to build nix (which I think is unrelated), so I've rebased this on top of master (nixpkgs-unstable, to be exact) and let's see if it works now.

EDIT: Seems to still be failing (but a different error now), so I'll wait for someone else's opinion.

@Atemu
Copy link
Member

Atemu commented Mar 7, 2024

@ofborg build postgresqlPackages.pgvecto-rs, postgresqlPackages.pgvecto-rs.passthru.tests

The CI failure is unrelated, you can ignore it.

@diogotcorreia
Copy link
Member Author

Just deployed this on my Immich server, everything working perfectly!

Copy link
Member

@Atemu Atemu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is GTG for an initial implementation.

Could you squash the fixup commits?

pkgs/servers/sql/postgresql/ext/pgvecto-rs/default.nix Outdated Show resolved Hide resolved
};

meta = with lib; {
broken = (stdenv.isLinux && stdenv.isAarch64) || stdenv.isDarwin;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a short comment why this isn't working or set platforms to just x86_64-linux?

Copy link
Member Author

@diogotcorreia diogotcorreia Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @Atemu advocated for broken instead of platforms here: #287632 (comment)

As for why it's broken in linux_aarch64, I personally have no idea as I personally don't have any ARM machine to try it out. Judging from comments in the other PR, it seems to be an issue with bindgen that has already been resolved, but pgvecto-rs hasn't incorporated the changes yet; I'll add that as a comment.
As for darwin, I don't see any official builds from pgvecto-rs, but darwin is listed in the targets section of the rust-toolchain.toml file upstream, so in theory it should work. Again, I don't own any darwin device, so I can't really test this.

Copy link
Contributor

@esclear esclear Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The breakage on aarch64 is essentially this bindgen issue, reported here for pgrx, which is the issue seen in the failed aarch64 build.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the comment with the issue link for pgrx since it's more relevant, thanks!

pkgs/servers/sql/postgresql/ext/pgvecto-rs/default.nix Outdated Show resolved Hide resolved
pkgs/servers/sql/postgresql/ext/pgvecto-rs/default.nix Outdated Show resolved Hide resolved
@diogotcorreia diogotcorreia force-pushed the pgvecto.rs branch 2 times, most recently from c174bce to 8c8a26e Compare March 7, 2024 23:33
Copy link
Contributor

@marsam marsam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks a lot!

@diogotcorreia
Copy link
Member Author

Just added @esclear as maintainer as well, as requested.

Hopefully that's all 🚀

@ofborg ofborg bot requested a review from esclear March 11, 2024 01:28
@Atemu
Copy link
Member

Atemu commented Mar 11, 2024

Thanks!

@Atemu Atemu merged commit ce8ddcd into NixOS:master Mar 11, 2024
20 checks passed
@diogotcorreia diogotcorreia deleted the pgvecto.rs branch March 11, 2024 08:25
@jvanbruegge jvanbruegge mentioned this pull request Mar 11, 2024
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: rust 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 11-100 10.rebuild-linux: 11-100 11.by: package-maintainer This PR was created by the maintainer of the package it changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Package request: pgvecto.rs
8 participants