···11+# preBuildAndTest and some small other bits
22+# taken from https://github.com/tcdi/pgx/blob/v0.4.5/nix/extension.nix
33+# (but now heavily modified)
44+# which uses MIT License with the following license file
55+#
66+# MIT License
77+#
88+# Portions Copyright 2019-2021 ZomboDB, LLC.
99+# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>.
1010+# All rights reserved.
1111+#
1212+# Permission is hereby granted, free of charge, to any person obtaining a copy
1313+# of this software and associated documentation files (the "Software"), to deal
1414+# in the Software without restriction, including without limitation the rights
1515+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1616+# copies of the Software, and to permit persons to whom the Software is
1717+# furnished to do so, subject to the following conditions:
1818+#
1919+# The above copyright notice and this permission notice shall be included in all
2020+# copies or substantial portions of the Software.
2121+#
2222+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2323+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2424+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2525+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2626+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2727+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828+# SOFTWARE.
2929+3030+{ lib
3131+, cargo-pgx
3232+, pkg-config
3333+, rustPlatform
3434+, stdenv
3535+, Security
3636+, writeShellScriptBin
3737+}:
3838+3939+# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
4040+# we hand most of the arguments down.
4141+#
4242+# Additional arguments are:
4343+# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
4444+# Needs to be the build platform variant.
4545+# - `useFakeRustfmt` Wether to use a noop fake command as rustfmt. cargo-pgx tries to call rustfmt.
4646+# If the generated rust bindings aren't needed to use the extension, its a
4747+# unnecessary and heavy dependency. If you set this to true, you also
4848+# have to add `rustfmt` to `nativeBuildInputs`.
4949+5050+{ buildAndTestSubdir ? null
5151+, buildType ? "release"
5252+, buildFeatures ? [ ]
5353+, cargoBuildFlags ? [ ]
5454+, postgresql
5555+# cargo-pgx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
5656+# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
5757+# if you include the generated code in the output via postInstall.
5858+, useFakeRustfmt ? true
5959+, ...
6060+} @ args:
6161+let
6262+ rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (args.nativeBuildInputs or []);
6363+in
6464+6565+assert lib.asserts.assertMsg ((args.installPhase or "") == "")
6666+ "buildPgxExtensions overwrites the installPhase, so providing one does nothing";
6767+assert lib.asserts.assertMsg ((args.buildPhase or "") == "")
6868+ "buildPgxExtensions overwrites the buildPhase, so providing one does nothing";
6969+assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
7070+ "The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
7171+assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
7272+ "The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";
7373+7474+let
7575+ fakeRustfmt = writeShellScriptBin "rustfmt" ''
7676+ exit 0
7777+ '';
7878+ maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
7979+ maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
8080+ export CARGO_TARGET_DIR="$(pwd)/target"
8181+ pushd "${buildAndTestSubdir}"
8282+ '';
8383+ maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
8484+8585+ pgxPostgresMajor = lib.versions.major postgresql.version;
8686+ preBuildAndTest = ''
8787+ export PGX_HOME=$(mktemp -d)
8888+ export PGDATA="$PGX_HOME/data-${pgxPostgresMajor}/"
8989+ cargo-pgx pgx init "--pg${pgxPostgresMajor}" ${postgresql}/bin/pg_config
9090+ echo "unix_socket_directories = '$(mktemp -d)'" > "$PGDATA/postgresql.conf"
9191+9292+ # This is primarily for Mac or other Nix systems that don't use the nixbld user.
9393+ export USER="$(whoami)"
9494+ pg_ctl start
9595+ createuser -h localhost --superuser --createdb "$USER" || true
9696+ pg_ctl stop
9797+ '';
9898+9999+ argsForBuildRustPackage = builtins.removeAttrs args [ "postgresql" "useFakeRustfmt" ];
100100+101101+ # so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
102102+ # we forgot parentheses
103103+ finalArgs = argsForBuildRustPackage // {
104104+ buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.isDarwin [ Security ];
105105+106106+ nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
107107+ cargo-pgx
108108+ postgresql
109109+ rustPlatform.rust.rustc
110110+ pkg-config
111111+ rustPlatform.bindgenHook
112112+ ] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
113113+114114+ buildPhase = ''
115115+ runHook preBuild
116116+117117+ echo "Executing cargo-pgx buildPhase"
118118+ ${preBuildAndTest}
119119+ ${maybeEnterBuildAndTestSubdir}
120120+121121+ NIX_PGLIBDIR="${postgresql}/lib" \
122122+ PGX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
123123+ cargo-pgx pgx package \
124124+ --pg-config ${postgresql}/bin/pg_config \
125125+ ${maybeDebugFlag} \
126126+ --features "${builtins.concatStringsSep " " buildFeatures}" \
127127+ --out-dir "$out"
128128+129129+ ${maybeLeaveBuildAndTestSubdir}
130130+131131+ runHook postBuild
132132+ '';
133133+134134+ preCheck = preBuildAndTest + args.preCheck or "";
135135+136136+ installPhase = ''
137137+ runHook preInstall
138138+139139+ echo "Executing buildPgxExtension install"
140140+141141+ ${maybeEnterBuildAndTestSubdir}
142142+143143+ cargo-pgx pgx stop all
144144+145145+ mv $out/${postgresql}/* $out
146146+ rm -rf $out/nix
147147+148148+ ${maybeLeaveBuildAndTestSubdir}
149149+150150+ runHook postInstall
151151+ '';
152152+153153+ PGX_PG_SYS_SKIP_BINDING_REWRITE = "1";
154154+ CARGO_BUILD_INCREMENTAL = "false";
155155+ RUST_BACKTRACE = "full";
156156+157157+ checkNoDefaultFeatures = true;
158158+ checkFeatures = (args.checkFeatures or [ ]) ++ [ "pg_test pg${pgxPostgresMajor}" ];
159159+ };
160160+in
161161+rustPlatform.buildRustPackage finalArgs