Skip to content

Commit 8796278

Browse files
committed
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 57ccdb2 commit 8796278

File tree

7 files changed

+157
-74
lines changed

7 files changed

+157
-74
lines changed

flake.nix

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@
6464
inherit (final) writeShellScriptBin;
6565
};
6666

67-
buildPgrxExtension_0_11_2 = prev.buildPgrxExtension.override {
68-
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_2;
69-
};
70-
7167
buildPgrxExtension_0_11_3 = prev.buildPgrxExtension.override {
7268
cargo-pgrx = final.cargo-pgrx.cargo-pgrx_0_11_3;
7369
};

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: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
makeRustPlatform,
88
stdenv,
99
rust-bin,
10+
rustVersion ? "1.85.1",
1011
}:
1112
let
12-
rustVersion = "1.85.1";
1313
rustPlatform = makeRustPlatform {
1414
cargo = rust-bin.stable.${rustVersion}.default;
1515
rustc = rust-bin.stable.${rustVersion}.default;
1616
};
17-
generic =
17+
mkCargoPgrx =
1818
{
1919
version,
2020
hash,
@@ -65,30 +65,25 @@ let
6565
};
6666
in
6767
{
68-
cargo-pgrx_0_11_2 = generic {
69-
version = "0.11.2";
70-
hash = "sha256-8NlpMDFaltTIA8G4JioYm8LaPJ2RGKH5o6sd6lBHmmM=";
71-
cargoHash = "sha256-CbU5B0pvB9ApTZOdYP/ZwuIG8bqGzk/ING2PCM0q2bQ=";
72-
};
73-
cargo-pgrx_0_11_3 = generic {
68+
cargo-pgrx_0_11_3 = mkCargoPgrx {
7469
version = "0.11.3";
7570
hash = "sha256-UHIfwOdXoJvR4Svha6ud0FxahP1wPwUtviUwUnTmLXU=";
7671
cargoHash = "sha256-j4HnD8Zt9uhlV5N7ldIy9564o9qFEqs5KfXHmnQ1WEw=";
7772
};
78-
cargo-pgrx_0_12_6 = generic {
73+
cargo-pgrx_0_12_6 = mkCargoPgrx {
7974
version = "0.12.6";
8075
hash = "sha256-7aQkrApALZe6EoQGVShGBj0UIATnfOy2DytFj9IWdEA=";
8176
cargoHash = "sha256-Di4UldQwAt3xVyvgQT1gUhdvYUVp7n/a72pnX45kP0w=";
8277
};
83-
cargo-pgrx_0_12_9 = generic {
78+
cargo-pgrx_0_12_9 = mkCargoPgrx {
8479
version = "0.12.9";
8580
hash = "sha256-aR3DZAjeEEAjLQfZ0ZxkjLqTVMIEbU0UiZ62T4BkQq8=";
8681
cargoHash = "sha256-KTKcol9qSNLQZGW32e6fBb6cPkUGItknyVpLdBYqrBY=";
8782
};
88-
cargo-pgrx_0_14_3 = generic {
83+
cargo-pgrx_0_14_3 = mkCargoPgrx {
8984
version = "0.14.3";
9085
hash = "sha256-3TsNpEqNm3Uol5XPW1i0XEbP2fF2+RKB2d7lO6BDnvQ=";
9186
cargoHash = "sha256-Ny7j56pwB+2eEK62X0nWfFKQy5fBz+Q1oyvecivxLkk=";
9287
};
93-
inherit rustPlatform;
88+
inherit mkCargoPgrx;
9489
}

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+
}

nix/ext/pg_graphql.nix

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
{
2-
pkgs,
2+
callPackages,
33
lib,
44
stdenv,
5+
buildEnv,
56
fetchFromGitHub,
67
postgresql,
7-
buildPgrxExtension_0_11_2,
8-
buildPgrxExtension_0_11_3,
9-
buildPgrxExtension_0_12_6,
10-
buildPgrxExtension_0_12_9,
118
rust-bin,
9+
rsync,
1210
}:
1311

1412
let
1513
pname = "pg_graphql";
1614
build =
17-
version: hash: rustVersion: buildPgrxExtension:
15+
version: hash: rustVersion: pgrxVersion:
1816
let
1917
cargo = rust-bin.stable.${rustVersion}.default;
20-
previousVersions = lib.filter (v: v != version) versions;
18+
previousVersions = lib.filter (v: v != version) versions; # FIXME
19+
mkPgrxExtension = callPackages ../cargo-pgrx/mkPgrxExtension.nix {
20+
inherit rustVersion pgrxVersion;
21+
};
22+
2123
in
22-
buildPgrxExtension rec {
24+
mkPgrxExtension rec {
2325
inherit pname version postgresql;
2426

2527
src = fetchFromGitHub {
@@ -106,7 +108,7 @@ let
106108
preCheck = ''
107109
export PGRX_HOME=$(mktemp -d)
108110
export NIX_PGLIBDIR=$PGRX_HOME/${lib.versions.major postgresql.version}/lib
109-
${lib.getExe pkgs.rsync} --chmod=ugo+w -a ${postgresql}/ ${postgresql.lib}/ $PGRX_HOME/${lib.versions.major postgresql.version}/
111+
${lib.getExe rsync} --chmod=ugo+w -a ${postgresql}/ ${postgresql.lib}/ $PGRX_HOME/${lib.versions.major postgresql.version}/
110112
cargo pgrx init --pg${lib.versions.major postgresql.version} $PGRX_HOME/${lib.versions.major postgresql.version}/bin/pg_config
111113
'';
112114

@@ -126,23 +128,12 @@ let
126128
versions = lib.naturalSort (lib.attrNames supportedVersions);
127129
latestVersion = lib.last versions;
128130
numberOfVersions = builtins.length versions;
129-
mapPgrxExtension =
130-
version:
131-
{
132-
"0.11.2" = buildPgrxExtension_0_11_2;
133-
"0.11.3" = buildPgrxExtension_0_11_3;
134-
"0.12.6" = buildPgrxExtension_0_12_6;
135-
"0.12.9" = buildPgrxExtension_0_12_9;
136-
}
137-
."${version}";
138131
packages = builtins.attrValues (
139-
lib.mapAttrs (
140-
name: value: build name value.hash value.rust (mapPgrxExtension value.pgrx)
141-
) supportedVersions
132+
lib.mapAttrs (name: value: build name value.hash value.rust value.pgrx) supportedVersions
142133
);
143134

144135
in
145-
pkgs.buildEnv {
136+
buildEnv {
146137
name = pname;
147138
paths = packages;
148139
pathsToLink = [

nix/ext/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"hash": "sha256-Kxo4o8+hfSTOjvhYyGF2BpksWfW/AMCCH4qom4AGw18=",
88
"pgrx": "0.11.2",
9-
"rust": "1.85.1"
9+
"rust": "1.70.0"
1010
},
1111
"1.5.0": {
1212
"postgresql": [

0 commit comments

Comments
 (0)