From bbb42450c52d5db817b5011aea2212dc270f2154 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Thu, 22 Aug 2024 13:28:15 -0700 Subject: [PATCH] default-crate-overrides: proc-macro-crate assumes env::var("CARGO") Since version 2.0.0, proc-macro-crate has assumed it can exec() `env::var("CARGO")` in order to run `cargo locate-project`. This commit adds a crate override to proc-macro-crate which simply writes the path to buildPlatform.cargo into the proc-macro-crate sources. This way we don't need to set `env.CARGO` for every build that depends on proc-macro-crate -- if we do that, the $CARGO environment variable would be visible to the entire build. This could potentially lead to incredibly hard-to-troubleshoot heisenbugs if there is some other crate that expects `env::var("CARGO")` to exist -- that other crate would mysteriously work only in projects that use proc-macro-crate but not anywhere else! --- pkgs/build-support/rust/default-crate-overrides.nix | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkgs/build-support/rust/default-crate-overrides.nix b/pkgs/build-support/rust/default-crate-overrides.nix index 1aebfe60a8d2..126be2ad99eb 100644 --- a/pkgs/build-support/rust/default-crate-overrides.nix +++ b/pkgs/build-support/rust/default-crate-overrides.nix @@ -43,6 +43,7 @@ , udev , webkitgtk_4_1 , zlib +, buildPackages , ... }: @@ -357,4 +358,15 @@ in buildInputs = [ atk ]; }; + # Assumes it can run Command::new(env::var("CARGO")).arg("locate-project") + # https://github.com/bkchr/proc-macro-crate/blame/master/src/lib.rs#L244 + proc-macro-crate = attrs: lib.optionalAttrs (lib.versionAtLeast attrs.version "2.0") { + prePatch = (attrs.prePatch or "") + '' + substituteInPlace \ + src/lib.rs \ + --replace-fail \ + 'env::var("CARGO").map_err(|_| Error::CargoEnvVariableNotSet)?' \ + '"${lib.getBin buildPackages.cargo}/bin/cargo"' + ''; + }; }