Skip to content

Commit 310c9af

Browse files
jfrochesamrose
authored andcommitted
feat(cargo-pgrx): build extensions with specified Rust version
This change allows developers to target specific Rust versions for building extensions. It implements support for building cargo extensions and `cargo-pgrx` using the specified Rust version.
1 parent e321760 commit 310c9af

File tree

4 files changed

+156
-53
lines changed

4 files changed

+156
-53
lines changed

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
# SOFTWARE.
2929

30-
{ lib
31-
, cargo-pgrx
32-
, pkg-config
33-
, rustPlatform
34-
, stdenv
35-
, Security
36-
, writeShellScriptBin
30+
{
31+
lib,
32+
cargo-pgrx,
33+
pkg-config,
34+
rustPlatform,
35+
stdenv,
36+
darwin,
37+
writeShellScriptBin,
3738
}:
3839

3940
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
@@ -47,26 +48,31 @@
4748
# unnecessary and heavy dependency. If you set this to true, you also
4849
# have to add `rustfmt` to `nativeBuildInputs`.
4950

50-
{ buildAndTestSubdir ? null
51-
, buildType ? "release"
52-
, buildFeatures ? [ ]
53-
, cargoBuildFlags ? [ ]
54-
, postgresql
55-
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
56-
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
57-
# if you include the generated code in the output via postInstall.
58-
, useFakeRustfmt ? true
59-
, usePgTestCheckFeature ? true
60-
, ...
61-
} @ args:
51+
{
52+
buildAndTestSubdir ? null,
53+
buildType ? "release",
54+
buildFeatures ? [ ],
55+
cargoBuildFlags ? [ ],
56+
postgresql,
57+
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
58+
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
59+
# if you include the generated code in the output via postInstall.
60+
useFakeRustfmt ? true,
61+
usePgTestCheckFeature ? true,
62+
...
63+
}@args:
6264
let
63-
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (args.nativeBuildInputs or []);
65+
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (
66+
args.nativeBuildInputs or [ ]
67+
);
6468
in
6569

66-
assert lib.asserts.assertMsg ((args.installPhase or "") == "")
67-
"buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
68-
assert lib.asserts.assertMsg ((args.buildPhase or "") == "")
69-
"buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
70+
assert lib.asserts.assertMsg (
71+
(args.installPhase or "") == ""
72+
) "buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
73+
assert lib.asserts.assertMsg (
74+
(args.buildPhase or "") == ""
75+
) "buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
7076
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
7177
"The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
7278
assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
@@ -75,7 +81,7 @@ assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
7581
let
7682
fakeRustfmt = writeShellScriptBin "rustfmt" ''
7783
exit 0
78-
'';
84+
'';
7985
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
8086
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
8187
export CARGO_TARGET_DIR="$(pwd)/target"
@@ -97,19 +103,28 @@ let
97103
pg_ctl stop
98104
'';
99105

100-
argsForBuildRustPackage = builtins.removeAttrs args [ "postgresql" "useFakeRustfmt" "usePgTestCheckFeature" ];
106+
argsForBuildRustPackage = builtins.removeAttrs args [
107+
"postgresql"
108+
"useFakeRustfmt"
109+
"usePgTestCheckFeature"
110+
];
101111

102112
# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
103113
# we forgot parentheses
104114
finalArgs = argsForBuildRustPackage // {
105-
buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.hostPlatform.isDarwin [ Security ];
106-
107-
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
108-
cargo-pgrx
109-
postgresql
110-
pkg-config
111-
rustPlatform.bindgenHook
112-
] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
115+
buildInputs =
116+
(args.buildInputs or [ ])
117+
++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.apple_sdk.frameworks.Security ];
118+
119+
nativeBuildInputs =
120+
(args.nativeBuildInputs or [ ])
121+
++ [
122+
cargo-pgrx
123+
postgresql
124+
pkg-config
125+
rustPlatform.bindgenHook
126+
]
127+
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
113128

114129
buildPhase = ''
115130
runHook preBuild
@@ -143,6 +158,7 @@ let
143158
cargo-pgrx pgrx stop all
144159
145160
mv $out/${postgresql}/* $out
161+
mv $out/${postgresql.lib}/* $out
146162
rm -rf $out/nix
147163
148164
${maybeLeaveBuildAndTestSubdir}
@@ -155,7 +171,10 @@ let
155171
RUST_BACKTRACE = "full";
156172

157173
checkNoDefaultFeatures = true;
158-
checkFeatures = (args.checkFeatures or [ ]) ++ (lib.optionals usePgTestCheckFeature [ "pg_test" ]) ++ [ "pg${pgrxPostgresMajor}" ];
174+
checkFeatures =
175+
(args.checkFeatures or [ ])
176+
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
177+
++ [ "pg${pgrxPostgresMajor}" ];
159178
};
160179
in
161180
rustPlatform.buildRustPackage finalArgs

nix/cargo-pgrx/default.nix

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
{ lib
2-
, darwin
3-
, fetchCrate
4-
, openssl
5-
, pkg-config
6-
, makeRustPlatform
7-
, stdenv
8-
, rust-bin
1+
{
2+
lib,
3+
darwin,
4+
fetchCrate,
5+
openssl,
6+
pkg-config,
7+
makeRustPlatform,
8+
stdenv,
9+
rust-bin,
10+
rustVersion ? "1.85.1",
911
}:
1012
let
11-
rustVersion = "1.85.1";
1213
rustPlatform = makeRustPlatform {
1314
cargo = rust-bin.stable.${rustVersion}.default;
1415
rustc = rust-bin.stable.${rustVersion}.default;
1516
};
16-
generic =
17-
{ version
18-
, hash
19-
, cargoHash
17+
mkCargoPgrx =
18+
{
19+
version,
20+
hash,
21+
cargoHash,
2022
}:
2123
rustPlatform.buildRustPackage rec {
2224
# rust-overlay uses 'cargo-auditable' wrapper for 'cargo' command, but it
@@ -61,25 +63,25 @@ let
6163
};
6264
in
6365
{
64-
cargo-pgrx_0_11_3 = generic {
66+
cargo-pgrx_0_11_3 = mkCargoPgrx {
6567
version = "0.11.3";
6668
hash = "sha256-UHIfwOdXoJvR4Svha6ud0FxahP1wPwUtviUwUnTmLXU=";
6769
cargoHash = "sha256-j4HnD8Zt9uhlV5N7ldIy9564o9qFEqs5KfXHmnQ1WEw=";
6870
};
69-
cargo-pgrx_0_12_6 = generic {
71+
cargo-pgrx_0_12_6 = mkCargoPgrx {
7072
version = "0.12.6";
7173
hash = "sha256-7aQkrApALZe6EoQGVShGBj0UIATnfOy2DytFj9IWdEA=";
7274
cargoHash = "sha256-Di4UldQwAt3xVyvgQT1gUhdvYUVp7n/a72pnX45kP0w=";
7375
};
74-
cargo-pgrx_0_12_9 = generic {
76+
cargo-pgrx_0_12_9 = mkCargoPgrx {
7577
version = "0.12.9";
7678
hash = "sha256-aR3DZAjeEEAjLQfZ0ZxkjLqTVMIEbU0UiZ62T4BkQq8=";
7779
cargoHash = "sha256-KTKcol9qSNLQZGW32e6fBb6cPkUGItknyVpLdBYqrBY=";
7880
};
79-
cargo-pgrx_0_14_3 = generic {
81+
cargo-pgrx_0_14_3 = mkCargoPgrx {
8082
version = "0.14.3";
8183
hash = "sha256-3TsNpEqNm3Uol5XPW1i0XEbP2fF2+RKB2d7lO6BDnvQ=";
8284
cargoHash = "sha256-Ny7j56pwB+2eEK62X0nWfFKQy5fBz+Q1oyvecivxLkk=";
8385
};
84-
inherit rustPlatform;
86+
inherit mkCargoPgrx;
8587
}

nix/cargo-pgrx/mkPgrxExtension.nix

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
callPackage,
3+
rustVersion,
4+
pgrxVersion,
5+
makeRustPlatform,
6+
rust-bin,
7+
}:
8+
let
9+
inherit
10+
(
11+
(callPackage ./default.nix {
12+
inherit rustVersion;
13+
})
14+
)
15+
mkCargoPgrx
16+
;
17+
18+
rustPlatform = makeRustPlatform {
19+
cargo = rust-bin.stable.${rustVersion}.default;
20+
rustc = rust-bin.stable.${rustVersion}.default;
21+
};
22+
23+
versions = builtins.fromJSON (builtins.readFile ./versions.json);
24+
25+
cargo-pgrx =
26+
let
27+
pgrx =
28+
versions.${pgrxVersion}
29+
or (throw "Unsupported pgrx version ${pgrxVersion}. Available versions: ${builtins.attrNames versions}. Change 'nix/cargo-pgrx/versions.json' to add support for new versions.");
30+
mapping = {
31+
inherit (pgrx) hash;
32+
cargoHash =
33+
pgrx.rust."${rustVersion}".cargoHash
34+
or (throw "Unsupported rust version ${rustVersion} for pgrx version ${pgrxVersion}. Available Rust versions: ${builtins.attrNames pgrx.rust}. Change 'nix/cargo-pgrx/versions.json' to add support for new versions.");
35+
};
36+
in
37+
mkCargoPgrx {
38+
inherit (mapping) hash cargoHash;
39+
version = pgrxVersion;
40+
};
41+
in
42+
callPackage ./buildPgrxExtension.nix {
43+
inherit rustPlatform;
44+
inherit cargo-pgrx;
45+
}

nix/cargo-pgrx/versions.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"0.11.2": {
3+
"hash": "sha256-8NlpMDFaltTIA8G4JioYm8LaPJ2RGKH5o6sd6lBHmmM=",
4+
"rust": {
5+
"1.70.0": {
6+
"cargoHash": "sha256-qTb3JV3u42EilaK2jP9oa5D09mkuHyRbGGRs9Rg4TzI="
7+
},
8+
"1.85.1": {
9+
"cargoHash": "sha256-CbU5B0pvB9ApTZOdYP/ZwuIG8bqGzk/ING2PCM0q2bQ="
10+
}
11+
}
12+
},
13+
"0.11.3": {
14+
"hash": "sha256-UHIfwOdXoJvR4Svha6ud0FxahP1wPwUtviUwUnTmLXU=",
15+
"rust": {
16+
"1.85.1": {
17+
"cargoHash": "sha256-KBlT3FARjGcbtHIGDoC6ir3aNXXfDRmIoy990SOqoFg="
18+
}
19+
}
20+
},
21+
"0.12.6": {
22+
"hash": "sha256-7aQkrApALZe6EoQGVShGBj0UIATnfOy2DytFj9IWdEA=",
23+
"rust": {
24+
"1.81.0": {
25+
"cargoHash": "sha256-Di4UldQwAt3xVyvgQT1gUhdvYUVp7n/a72pnX45kP0w="
26+
}
27+
}
28+
},
29+
"0.12.9": {
30+
"hash": "sha256-aR3DZAjeEEAjLQfZ0ZxkjLqTVMIEbU0UiZ62T4BkQq8=",
31+
"rust": {
32+
"1.81.0": {
33+
"cargoHash": "sha256-53HKhvsKLTa2JCByLEcK3UzWXoM+LTatd98zvS1C9no="
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)