Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 138 additions & 136 deletions pkgs/development/tools/rust/cargo-pgrx/buildPgrxExtension.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,146 +47,148 @@
# If the generated rust bindings aren't needed to use the extension, its a
# unnecessary and heavy dependency. If you set this to true, you also
# have to add `rustfmt` to `nativeBuildInputs`.
# - `usePgTestCheckFeature` Whether to enable the `pg_test` feature during the check phase.
lib.extendMkDerivation {
constructDrv = rustPlatform.buildRustPackage;

{
buildAndTestSubdir ? null,
buildType ? "release",
buildFeatures ? [ ],
cargoBuildFlags ? [ ],
cargoPgrxFlags ? [ ],
postgresql,
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
# if you include the generated code in the output via postInstall.
useFakeRustfmt ? true,
usePgTestCheckFeature ? true,
...
}@args:
let
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (
args.nativeBuildInputs or [ ]
);
in

assert lib.asserts.assertMsg (
(args.installPhase or "") == ""
) "buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
assert lib.asserts.assertMsg (
(args.buildPhase or "") == ""
) "buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";

let
fakeRustfmt = writeShellScriptBin "rustfmt" ''
exit 0
'';
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
export CARGO_TARGET_DIR="$(pwd)/target"
pushd "${buildAndTestSubdir}"
'';
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";

pgrxPostgresMajor = lib.versions.major postgresql.version;
preBuildAndTest = ''
export PGRX_HOME="$(mktemp -d)"
export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/"
cargo-pgrx pgrx init "--pg${pgrxPostgresMajor}" ${lib.getDev postgresql}/bin/pg_config

# unix sockets work in sandbox, too.
export PGHOST="$(mktemp -d)"
cat > "$PGDATA/postgresql.conf" <<EOF
listen_addresses = ''\''
unix_socket_directories = '$PGHOST'
EOF

# This is primarily for Mac or other Nix systems that don't use the nixbld user.
export USER="$(whoami)"
pg_ctl start
createuser --superuser --createdb "$USER" || true
pg_ctl stop
'';

argsForBuildRustPackage = builtins.removeAttrs args [
excludeDrvArgNames = [
"postgresql"
"useFakeRustfmt"
"usePgTestCheckFeature"
];

cargoPgrxFlags' = lib.escapeShellArgs cargoPgrxFlags;

# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
# we forgot parentheses
finalArgs = argsForBuildRustPackage // {
buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.hostPlatform.isDarwin [ Security ];

nativeBuildInputs =
(args.nativeBuildInputs or [ ])
++ [
cargo-pgrx
postgresql
pkg-config
rustPlatform.bindgenHook
]
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];

buildPhase = ''
runHook preBuild

echo "Executing cargo-pgrx buildPhase"
${preBuildAndTest}
${maybeEnterBuildAndTestSubdir}

PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
cargo pgrx package \
${cargoPgrxFlags'} \
--pg-config ${lib.getDev postgresql}/bin/pg_config \
${maybeDebugFlag} \
--features "${builtins.concatStringsSep " " buildFeatures}" \
--out-dir "$out"

${maybeLeaveBuildAndTestSubdir}

runHook postBuild
'';

preCheck = preBuildAndTest + args.preCheck or "";

installPhase = ''
runHook preInstall

echo "Executing buildPgrxExtension install"

${maybeEnterBuildAndTestSubdir}

cargo-pgrx pgrx stop all ${cargoPgrxFlags'}

mv $out/${postgresql}/* $out
rm -rf $out/nix

${maybeLeaveBuildAndTestSubdir}

runHook postInstall
'';

PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";

checkNoDefaultFeatures = true;
checkFeatures =
(args.checkFeatures or [ ])
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
++ [ "pg${pgrxPostgresMajor}" ];

meta = (args.meta or { }) // {
# See comment in postgresql's generic.nix doInstallCheck section
broken = (args.meta.broken or false) || stdenv.hostPlatform.isDarwin;
extendDrvArgs =
finalAttrs:
{
buildAndTestSubdir ? null,
buildType ? "release",
buildFeatures ? [ ],
cargoBuildFlags ? [ ],
cargoPgrxFlags ? [ ],
postgresql,
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
# if you include the generated code in the output via postInstall.
useFakeRustfmt ? true,
usePgTestCheckFeature ? true,
...
}@args:
let
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (
args.nativeBuildInputs or [ ]
);
in

assert lib.asserts.assertMsg (
(args.installPhase or "") == ""
) "buildPgrxExtension overwrites the installPhase, so providing one does nothing";
assert lib.asserts.assertMsg (
(args.buildPhase or "") == ""
) "buildPgrxExtension overwrites the buildPhase, so providing one does nothing";
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";

let
fakeRustfmt = writeShellScriptBin "rustfmt" ''
exit 0
'';
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
export CARGO_TARGET_DIR="$(pwd)/target"
pushd "${buildAndTestSubdir}"
'';
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";

pgrxPostgresMajor = lib.versions.major postgresql.version;
preBuildAndTest = ''
export PGRX_HOME="$(mktemp -d)"
export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/"
cargo-pgrx pgrx init "--pg${pgrxPostgresMajor}" ${lib.getDev postgresql}/bin/pg_config

# unix sockets work in sandbox, too.
export PGHOST="$(mktemp -d)"
cat > "$PGDATA/postgresql.conf" <<EOF
listen_addresses = ''\''
unix_socket_directories = '$PGHOST'
EOF

# This is primarily for Mac or other Nix systems that don't use the nixbld user.
export USER="$(whoami)"
pg_ctl start
createuser --superuser --createdb "$USER" || true
pg_ctl stop
'';

cargoPgrxFlags' = lib.escapeShellArgs cargoPgrxFlags;
in
{
buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.hostPlatform.isDarwin [ Security ];

nativeBuildInputs =
(args.nativeBuildInputs or [ ])
++ [
cargo-pgrx
postgresql
pkg-config
rustPlatform.bindgenHook
]
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];

buildPhase = ''
runHook preBuild

echo "Executing cargo-pgrx buildPhase"
${preBuildAndTest}
${maybeEnterBuildAndTestSubdir}

PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
cargo pgrx package \
${cargoPgrxFlags'} \
--pg-config ${lib.getDev postgresql}/bin/pg_config \
${maybeDebugFlag} \
--features "${builtins.concatStringsSep " " buildFeatures}" \
--out-dir "$out"

${maybeLeaveBuildAndTestSubdir}

runHook postBuild
'';

preCheck = preBuildAndTest + args.preCheck or "";

installPhase = ''
runHook preInstall

echo "Executing buildPgrxExtension install"

${maybeEnterBuildAndTestSubdir}

cargo-pgrx pgrx stop all ${cargoPgrxFlags'}

mv $out/${postgresql}/* $out
rm -rf $out/nix

${maybeLeaveBuildAndTestSubdir}

runHook postInstall
'';

PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";

checkNoDefaultFeatures = true;
checkFeatures =
(args.checkFeatures or [ ])
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
++ [ "pg${pgrxPostgresMajor}" ];

meta = (args.meta or { }) // {
# See comment in postgresql's generic.nix doInstallCheck section
broken = (args.meta.broken or false) || stdenv.hostPlatform.isDarwin;
};
};
};
in
rustPlatform.buildRustPackage finalArgs
}
10 changes: 5 additions & 5 deletions pkgs/servers/sql/postgresql/ext/pgvecto-rs/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ in
cargo-pgrx = cargo-pgrx_0_12_0_alpha_1;
rustPlatform = rustPlatform';
})
rec {
(finalAttrs: {
inherit postgresql;

pname = "pgvecto-rs";
Expand All @@ -49,7 +49,7 @@ in
src = fetchFromGitHub {
owner = "tensorchord";
repo = "pgvecto.rs";
tag = "v${version}";
tag = "v${finalAttrs.version}";
hash = "sha256-X7BY2Exv0xQNhsS/GA7GNvj9OeVDqVCd/k3lUkXtfgE=";
};

Expand All @@ -58,7 +58,7 @@ in

# Set appropriate version on vectors.control, otherwise it won't show up on PostgreSQL
postPatch = ''
substituteInPlace ./vectors.control --subst-var-by CARGO_VERSION ${version}
substituteInPlace ./vectors.control --subst-var-by CARGO_VERSION ${finalAttrs.version}
'';

# Include upgrade scripts in the final package
Expand Down Expand Up @@ -90,7 +90,7 @@ in
||
# PostgreSQL 17 support issue upstream: https://github.com/tensorchord/pgvecto.rs/issues/607
# Check after next package update.
lib.versionAtLeast postgresql.version "17" && version == "0.3.0";
lib.versionAtLeast postgresql.version "17" && finalAttrs.version == "0.3.0";
description = "Scalable, Low-latency and Hybrid-enabled Vector Search in Postgres";
homepage = "https://github.com/tensorchord/pgvecto.rs";
license = lib.licenses.asl20;
Expand All @@ -99,4 +99,4 @@ in
esclear
];
};
}
})
Loading