···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` Whether 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+ pkg-config
110110+ rustPlatform.bindgenHook
111111+ ] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
112112+113113+ buildPhase = ''
114114+ runHook preBuild
115115+116116+ echo "Executing cargo-pgx buildPhase"
117117+ ${preBuildAndTest}
118118+ ${maybeEnterBuildAndTestSubdir}
119119+120120+ NIX_PGLIBDIR="${postgresql}/lib" \
121121+ PGX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
122122+ cargo-pgx pgx package \
123123+ --pg-config ${postgresql}/bin/pg_config \
124124+ ${maybeDebugFlag} \
125125+ --features "${builtins.concatStringsSep " " buildFeatures}" \
126126+ --out-dir "$out"
127127+128128+ ${maybeLeaveBuildAndTestSubdir}
129129+130130+ runHook postBuild
131131+ '';
132132+133133+ preCheck = preBuildAndTest + args.preCheck or "";
134134+135135+ installPhase = ''
136136+ runHook preInstall
137137+138138+ echo "Executing buildPgxExtension install"
139139+140140+ ${maybeEnterBuildAndTestSubdir}
141141+142142+ cargo-pgx pgx stop all
143143+144144+ mv $out/${postgresql}/* $out
145145+ rm -rf $out/nix
146146+147147+ ${maybeLeaveBuildAndTestSubdir}
148148+149149+ runHook postInstall
150150+ '';
151151+152152+ PGX_PG_SYS_SKIP_BINDING_REWRITE = "1";
153153+ CARGO_BUILD_INCREMENTAL = "false";
154154+ RUST_BACKTRACE = "full";
155155+156156+ checkNoDefaultFeatures = true;
157157+ checkFeatures = (args.checkFeatures or [ ]) ++ [ "pg_test pg${pgxPostgresMajor}" ];
158158+ };
159159+in
160160+rustPlatform.buildRustPackage finalArgs