Use of $NIX_BUILD_TOP considered harmful?

Hi folks,

After buildRustPackage breaks with keepBuildTree or in nix-shell · Issue #138554 · NixOS/nixpkgs · GitHub (" buildRustPackage breaks with keepBuildTree or in nix-shell") being idle for a year, I made a more general issue Use of $NIX_BUILD_TOP considered harmful · Issue #189691 · NixOS/nixpkgs · GitHub (“Use of $NIX_BUILD_TOP considered harmful”) and thought I’d post here for more visibility.

What do you think?

1 Like

I think buildRustOPackage just needs proper support for keepBuildTree and I would guess that using relative directories would create the same issue.

But generally $NIX_BUILD_TOP must be used instead of /build and should be avoided if possible which is not always easy or handy.

In the case of buildRustPackage this happens in postPatchHooks though, I think we can rely on sourceRoot being set correctly (and with that pwd as well) instead. That’s already done in other places in that hook as well, see https://github.com/NixOS/nixpkgs/blob/042adf08d1c1886dfbe11603b012f987c60b1148/pkgs/build-support/rust/hooks/cargo-setup-hook.sh#L26. Relative paths are not an issue because we can realpath when setting the variables.

It does not look like NIX_BUILD_TOP is required at all, I will be opening a PR fixing this later.

1 Like

Opened said PR, executing from a nix-shell and keepBuildTree work fine with it from my testing :+1:

https://github.com/NixOS/nixpkgs/pull/190925

1 Like

I should’ve targeted staging instead for this. Since I had a few commits in the same PR that didn’t cause any rebuilds, I opened a separate PR to fix the NIX_BUILD_TOP problem: buildRustPackage: dont rely on NIX_BUILD_TOP in cargoSetupPostPatchHook by IvarWithoutBones · Pull Request #191251 · NixOS/nixpkgs · GitHub