Skip to content

Commit

Permalink
feat: packaging and devshell with various cargo-pgrx versions! (#1277)
Browse files Browse the repository at this point in the history
* feat: packaging and devshell with various cargo-pgrx versions!

* feat: cargo-pgrx_0_12_6 instead

* feat: devshell with rust versions

* rust versions in devshell + try to make wrappers build again

* feat: override rust versions in buildPgrxExtension

* chore: newline

---------

Co-authored-by: Sam Rose <[email protected]>
  • Loading branch information
samrose and Sam Rose authored Oct 22, 2024
1 parent d4709ba commit 743c6f7
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 16 deletions.
37 changes: 36 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 63 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
flake-utils.url = "github:numtide/flake-utils";
nix2container.url = "github:nlewo/nix2container";
nix-editor.url = "github:snowfallorg/nix-editor";
rust-overlay.url = "github:oxalica/rust-overlay";
};

outputs = { self, nixpkgs, flake-utils, nix2container, nix-editor, ...}:
outputs = { self, nixpkgs, flake-utils, nix2container, nix-editor, rust-overlay, ...}:
let
gitRev = "vcs=${self.shortRev or "dirty"}+${builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}";

Expand Down Expand Up @@ -53,10 +54,41 @@
};
inherit system;
overlays = [
# NOTE (aseipp): add any needed overlays here. in theory we could
# NOTE add any needed overlays here. in theory we could
# pull them from the overlays/ directory automatically, but we don't
# want to have an arbitrary order, since it might matter. being
# explicit is better.
(import rust-overlay)
(final: prev: {
cargo-pgrx = final.callPackage ./nix/cargo-pgrx/default.nix {
inherit (final) lib;
inherit (final) darwin;
inherit (final) fetchCrate;
inherit (final) openssl;
inherit (final) pkg-config;
inherit (final) makeRustPlatform;
inherit (final) stdenv;
inherit (final) rust-bin;
};

buildPgrxExtension = final.callPackage ./nix/cargo-pgrx/buildPgrxExtension.nix {
inherit (final) cargo-pgrx;
inherit (final) lib;
inherit (final) Security;
inherit (final) pkg-config;
inherit (final) makeRustPlatform;
inherit (final) stdenv;
inherit (final) writeShellScriptBin;
};

buildPgrxExtension_0_11_3 = prev.buildPgrxExtension.override {
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_3;
};

buildPgrxExtension_0_12_6 = prev.buildPgrxExtension.override {
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_12_6;
};
})
(final: prev: {
postgresql = final.callPackage ./nix/postgresql/default.nix {
inherit (final) lib;
Expand All @@ -66,7 +98,6 @@
inherit (final) callPackage;
};
})
(import ./nix/overlays/cargo-pgrx-0-11-3.nix)
];
};
sfcgal = pkgs.callPackage ./nix/ext/sfcgal/sfcgal.nix { };
Expand Down Expand Up @@ -308,6 +339,8 @@
in
postgresVersions //{
supabase-groonga = supabase-groonga;
cargo-pgrx_0_11_3 = pkgs.cargo-pgrx.cargo-pgrx_0_11_3;
cargo-pgrx_0_12_6 = pkgs.cargo-pgrx.cargo-pgrx_0_12_6;
# PostgreSQL versions.
psql_15 = postgresVersions.psql_15;
psql_16 = postgresVersions.psql_16;
Expand Down Expand Up @@ -590,10 +623,7 @@
packages = flake-utils.lib.flattenTree basePackages // {
# Any extra packages we might want to include in our package
# set can go here.
inherit (pkgs)
# NOTE: comes from our cargo-pgrx-0-11-3.nix overlay
cargo-pgrx_0_11_3;

inherit (pkgs);
};

# The list of exported 'checks' that are run with every run of 'nix
Expand Down Expand Up @@ -627,7 +657,21 @@
# ambient $PATH environment when you run 'nix develop'. This is useful
# for development and puts many convenient devtools instantly within
# reach.
devShells.default = pkgs.mkShell {

devShells = let
mkCargoPgrxDevShell = { pgrxVersion, rustVersion }: pkgs.mkShell {
packages = with pkgs; [
basePackages."cargo-pgrx_${pgrxVersion}"
(rust-bin.stable.${rustVersion}.default.override {
extensions = [ "rust-src" ];
})
];
shellHook = ''
export HISTFILE=.history
'';
};
in {
default = pkgs.mkShell {
packages = with pkgs; [
coreutils
just
Expand All @@ -650,6 +694,15 @@
export HISTFILE=.history
'';
};
}
);
cargo-pgrx_0_11_3 = mkCargoPgrxDevShell {
pgrxVersion = "0_11_3";
rustVersion = "1.80.0";
};
cargo-pgrx_0_12_6 = mkCargoPgrxDevShell {
pgrxVersion = "0_12_6";
rustVersion = "1.80.0";
};
};
}
);
}
161 changes: 161 additions & 0 deletions nix/cargo-pgrx/buildPgrxExtension.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# preBuildAndTest and some small other bits
# taken from https://github.com/tcdi/pgrx/blob/v0.9.4/nix/extension.nix
# (but now heavily modified)
# which uses MIT License with the following license file
#
# MIT License
#
# Portions Copyright 2019-2021 ZomboDB, LLC.
# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <[email protected]>.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

{ lib
, cargo-pgrx
, pkg-config
, rustPlatform
, stdenv
, Security
, writeShellScriptBin
}:

# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
# we hand most of the arguments down.
#
# Additional arguments are:
# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
# Needs to be the build platform variant.
# - `useFakeRustfmt` Whether to use a noop fake command as rustfmt. cargo-pgrx tries to call rustfmt.
# 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`.

{ buildAndTestSubdir ? null
, buildType ? "release"
, buildFeatures ? [ ]
, cargoBuildFlags ? [ ]
, 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
echo "unix_socket_directories = '$(mktemp -d)'" > "$PGDATA/postgresql.conf"
# This is primarily for Mac or other Nix systems that don't use the nixbld user.
export USER="$(whoami)"
pg_ctl start
createuser -h localhost --superuser --createdb "$USER" || true
pg_ctl stop
'';

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

# 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 \
--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
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}" ];
};
in
rustPlatform.buildRustPackage finalArgs
Loading

0 comments on commit 743c6f7

Please sign in to comment.