nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# preBuildAndTest and some small other bits
2# taken from https://github.com/tcdi/pgrx/blob/v0.9.4/nix/extension.nix
3# (but now heavily modified)
4# which uses MIT License with the following license file
5#
6# MIT License
7#
8# Portions Copyright 2019-2021 ZomboDB, LLC.
9# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>.
10# All rights reserved.
11#
12# Permission is hereby granted, free of charge, to any person obtaining a copy
13# of this software and associated documentation files (the "Software"), to deal
14# in the Software without restriction, including without limitation the rights
15# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16# copies of the Software, and to permit persons to whom the Software is
17# furnished to do so, subject to the following conditions:
18#
19# The above copyright notice and this permission notice shall be included in all
20# copies or substantial portions of the Software.
21#
22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28# SOFTWARE.
29
30{
31 lib,
32 pkg-config,
33 rustPlatform,
34 stdenv,
35 writeShellScriptBin,
36}:
37
38# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
39# we hand most of the arguments down.
40#
41# Additional arguments are:
42# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
43# Needs to be the build platform variant.
44# - `useFakeRustfmt` Whether to use a noop fake command as rustfmt. cargo-pgrx tries to call rustfmt.
45# If the generated rust bindings aren't needed to use the extension, its a
46# unnecessary and heavy dependency. If you set this to true, you also
47# have to add `rustfmt` to `nativeBuildInputs`.
48# - `usePgTestCheckFeature` Whether to enable the `pg_test` feature during the check phase.
49lib.extendMkDerivation {
50 constructDrv = rustPlatform.buildRustPackage;
51
52 excludeDrvArgNames = [
53 "postgresql"
54 "useFakeRustfmt"
55 "usePgTestCheckFeature"
56 ];
57
58 extendDrvArgs =
59 finalAttrs:
60 {
61 buildAndTestSubdir ? null,
62 buildType ? "release",
63 buildFeatures ? [ ],
64 cargoBuildFlags ? [ ],
65 cargoPgrxFlags ? [ ],
66 # pinned dependencies
67 cargo-pgrx,
68 postgresql,
69 # cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
70 # dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
71 # if you include the generated code in the output via postInstall.
72 useFakeRustfmt ? true,
73 usePgTestCheckFeature ? true,
74 ...
75 }@args:
76 let
77 rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (
78 args.nativeBuildInputs or [ ]
79 );
80 in
81
82 assert lib.asserts.assertMsg (
83 (args.installPhase or "") == ""
84 ) "buildPgrxExtension overwrites the installPhase, so providing one does nothing";
85 assert lib.asserts.assertMsg (
86 (args.buildPhase or "") == ""
87 ) "buildPgrxExtension overwrites the buildPhase, so providing one does nothing";
88 assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
89 "The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
90 assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
91 "The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";
92
93 let
94 fakeRustfmt = writeShellScriptBin "rustfmt" ''
95 exit 0
96 '';
97 maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
98 maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
99 export CARGO_TARGET_DIR="$(pwd)/target"
100 pushd "${buildAndTestSubdir}"
101 '';
102 maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
103
104 pgrxPostgresMajor = lib.versions.major postgresql.version;
105 preBuildAndTest = ''
106 export PGRX_HOME="$(mktemp -d)"
107 export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/"
108 cargo-pgrx pgrx init "--pg${pgrxPostgresMajor}" ${postgresql.pg_config}/bin/pg_config
109
110 # unix sockets work in sandbox, too.
111 export PGHOST="$(mktemp -d)"
112 cat > "$PGDATA/postgresql.conf" <<EOF
113 listen_addresses = ''\''
114 unix_socket_directories = '$PGHOST'
115 EOF
116
117 # This is primarily for Mac or other Nix systems that don't use the nixbld user.
118 export USER="$(whoami)"
119 pg_ctl start
120 createuser --superuser --createdb "$USER" || true
121 pg_ctl stop
122 '';
123
124 cargoPgrxFlags' = lib.escapeShellArgs cargoPgrxFlags;
125 in
126 {
127 buildInputs = (args.buildInputs or [ ]);
128
129 nativeBuildInputs =
130 (args.nativeBuildInputs or [ ])
131 ++ [
132 cargo-pgrx
133 postgresql
134 pkg-config
135 rustPlatform.bindgenHook
136 ]
137 ++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
138
139 buildPhase = ''
140 runHook preBuild
141
142 echo "Executing cargo-pgrx buildPhase"
143 ${preBuildAndTest}
144 ${maybeEnterBuildAndTestSubdir}
145
146 PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
147 ${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
148 cargo pgrx package \
149 ${cargoPgrxFlags'} \
150 --pg-config ${postgresql.pg_config}/bin/pg_config \
151 ${maybeDebugFlag} \
152 --features "${builtins.concatStringsSep " " buildFeatures}" \
153 --out-dir "$out"
154
155 ${maybeLeaveBuildAndTestSubdir}
156
157 runHook postBuild
158 '';
159
160 preCheck = preBuildAndTest + args.preCheck or "";
161
162 installPhase = ''
163 runHook preInstall
164
165 echo "Executing buildPgrxExtension install"
166
167 ${maybeEnterBuildAndTestSubdir}
168
169 cargo-pgrx pgrx stop all ${cargoPgrxFlags'}
170
171 mv $out/${postgresql}/* $out
172 rm -rf $out/nix
173
174 ${maybeLeaveBuildAndTestSubdir}
175
176 runHook postInstall
177 '';
178
179 PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
180 CARGO_BUILD_INCREMENTAL = "false";
181 RUST_BACKTRACE = "full";
182
183 checkNoDefaultFeatures = true;
184 checkFeatures =
185 (args.checkFeatures or [ ])
186 ++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
187 ++ [ "pg${pgrxPostgresMajor}" ];
188
189 meta = (args.meta or { }) // {
190 # See comment in postgresql's generic.nix doInstallCheck section
191 broken = (args.meta.broken or false) || stdenv.hostPlatform.isDarwin;
192 };
193 };
194}