Skip to content

Commit 4fd45ce

Browse files
committed
fix: enable sccache on Darwin using __noChroot
Disable Nix sandbox on Darwin pgrx builds to allow sccache to work across parallel builds without permission errors
1 parent 1ddc1a5 commit 4fd45ce

1 file changed

Lines changed: 76 additions & 68 deletions

File tree

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -148,98 +148,106 @@ let
148148

149149
# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
150150
# we forgot parentheses
151-
finalArgs = argsForBuildRustPackage // {
152-
buildInputs = (args.buildInputs or [ ]);
153-
154-
nativeBuildInputs =
155-
(args.nativeBuildInputs or [ ])
156-
++ [
157-
cargo-pgrx
158-
postgresql
159-
pkg-config
160-
bindgenHook
161-
sccache
162-
]
163-
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
164-
165-
buildPhase = ''
166-
runHook preBuild
167-
168-
${lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
151+
finalArgs =
152+
argsForBuildRustPackage
153+
// {
154+
buildInputs = (args.buildInputs or [ ]);
155+
156+
nativeBuildInputs =
157+
(args.nativeBuildInputs or [ ])
158+
++ [
159+
cargo-pgrx
160+
postgresql
161+
pkg-config
162+
bindgenHook
163+
sccache
164+
]
165+
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
166+
167+
buildPhase = ''
168+
runHook preBuild
169+
169170
if [[ -d "/nix/var/cache/sccache" && -w "/nix/var/cache/sccache" ]]; then
170171
# Test if sccache can actually write to the directory (sandbox check)
171172
if touch "/nix/var/cache/sccache/.test" 2>/dev/null && rm -f "/nix/var/cache/sccache/.test" 2>/dev/null; then
172173
echo "sccache: cache directory available and writable in sandbox, enabling"
173174
export RUSTC_WRAPPER="${sccache}/bin/sccache"
174175
export SCCACHE_DIR="/nix/var/cache/sccache"
175176
export SCCACHE_CACHE_SIZE="50G"
177+
${lib.optionalString stdenv.hostPlatform.isDarwin ''
178+
export SCCACHE_NO_DAEMON=1
179+
export SCCACHE_DIRECT=true
180+
''}
176181
else
177182
echo "sccache: cache directory not accessible in sandbox (needs extra-sandbox-paths configuration), skipping"
178183
fi
179184
else
180185
echo "sccache: cache directory not available, skipping"
181186
fi
182-
''}
183-
184-
echo "Executing cargo-pgrx buildPhase"
185-
${preBuildAndTest}
186-
${maybeEnterBuildAndTestSubdir}
187-
188-
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
189-
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
190-
191-
${lib.optionalString needsRustcWrapper ''
192-
export ORIGINAL_RUSTC="$(command -v ${stdenv.cc.targetPrefix}rustc || command -v rustc)"
193-
export PATH="${rustcWrapper}/bin:$PATH"
194-
export RUSTC="${rustcWrapper}/bin/rustc"
195-
''}
196-
197-
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
198-
cargo ${pgrxBinaryName} package \
199-
--pg-config ${lib.getDev postgresql}/bin/pg_config \
200-
${maybeDebugFlag} \
201-
--features "${builtins.concatStringsSep " " buildFeatures}" \
202-
--out-dir "$out"
203-
204-
if [[ -n "''${RUSTC_WRAPPER:-}" ]]; then
205-
echo "sccache stats:"
206-
${sccache}/bin/sccache --show-stats
207-
fi
208187
209-
${maybeLeaveBuildAndTestSubdir}
188+
echo "Executing cargo-pgrx buildPhase"
189+
${preBuildAndTest}
190+
${maybeEnterBuildAndTestSubdir}
191+
192+
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
193+
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
194+
195+
${lib.optionalString needsRustcWrapper ''
196+
export ORIGINAL_RUSTC="$(command -v ${stdenv.cc.targetPrefix}rustc || command -v rustc)"
197+
export PATH="${rustcWrapper}/bin:$PATH"
198+
export RUSTC="${rustcWrapper}/bin/rustc"
199+
''}
200+
201+
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
202+
cargo ${pgrxBinaryName} package \
203+
--pg-config ${lib.getDev postgresql}/bin/pg_config \
204+
${maybeDebugFlag} \
205+
--features "${builtins.concatStringsSep " " buildFeatures}" \
206+
--out-dir "$out"
207+
208+
if [[ -n "''${RUSTC_WRAPPER:-}" ]]; then
209+
echo "sccache stats:"
210+
${sccache}/bin/sccache --show-stats
211+
fi
212+
213+
${maybeLeaveBuildAndTestSubdir}
210214
211-
runHook postBuild
212-
'';
215+
runHook postBuild
216+
'';
213217

214-
preCheck = preBuildAndTest + args.preCheck or "";
218+
preCheck = preBuildAndTest + args.preCheck or "";
215219

216-
installPhase = ''
217-
runHook preInstall
220+
installPhase = ''
221+
runHook preInstall
218222
219-
echo "Executing buildPgrxExtension install"
223+
echo "Executing buildPgrxExtension install"
220224
221-
${maybeEnterBuildAndTestSubdir}
225+
${maybeEnterBuildAndTestSubdir}
222226
223-
cargo-${pgrxBinaryName} ${pgrxBinaryName} stop all
227+
cargo-${pgrxBinaryName} ${pgrxBinaryName} stop all
224228
225-
mv $out/${postgresql}/* $out
226-
mv $out/${postgresql.lib}/* $out
227-
rm -rf $out/nix
229+
mv $out/${postgresql}/* $out
230+
mv $out/${postgresql.lib}/* $out
231+
rm -rf $out/nix
228232
229-
${maybeLeaveBuildAndTestSubdir}
233+
${maybeLeaveBuildAndTestSubdir}
230234
231-
runHook postInstall
232-
'';
235+
runHook postInstall
236+
'';
233237

234-
PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
235-
CARGO_BUILD_INCREMENTAL = "false";
236-
RUST_BACKTRACE = "full";
238+
PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
239+
CARGO_BUILD_INCREMENTAL = "false";
240+
RUST_BACKTRACE = "full";
237241

238-
checkNoDefaultFeatures = true;
239-
checkFeatures =
240-
(args.checkFeatures or [ ])
241-
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
242-
++ [ "pg${pgrxPostgresMajor}" ];
243-
};
242+
checkNoDefaultFeatures = true;
243+
checkFeatures =
244+
(args.checkFeatures or [ ])
245+
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
246+
++ [ "pg${pgrxPostgresMajor}" ];
247+
}
248+
// lib.optionalAttrs stdenv.hostPlatform.isDarwin {
249+
# Disable sandbox on Darwin to allow sccache to work across parallel builds
250+
__noChroot = true;
251+
};
244252
in
245253
rustPlatform.buildRustPackage finalArgs

0 commit comments

Comments
 (0)