From 0c5333a4f625e35530437205c804951270c5de98 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 8 Dec 2024 15:34:01 +0100 Subject: [PATCH] build: backport NIX_ATTRS_*_FILE This was originally added in #4770 to support structured attrs in `nix-shell` & `nix develop`: the issue was that it was somewhat awkward to just write those files into a project directory, especially since it'd break in case of multiple `nix-shell` invocations from the same directory. Now the files are written to another, temporary location when using `nix-shell`/`nix develop` and the correct path is referenced by NIX_ATTRS_*_FILE. In `nixpkgs`, it's now common to use these environment variables, however we still fall back to checking to `.attrs.sh` & `.attrs.json` since the minimum Nix version we support is 2.3.17[1] which doesn't have this change. This however makes implementing structured attrs support more complicated than needed[2] and in fact we have a few places where the check for `.attrs.sh`/`.attrs.json` isn't made, so these only break with Nix 2.3[3]. The idea is now to * get this into 2.3.18 * bump minver once again to 2.3.18 in nixpkgs * remove all occurrences of `.attrs.sh`/`.attrs.json` from nixpkgs. [1] https://github.com/NixOS/nixpkgs/blob/f4bd97b8face6dc14b0ce048c20cff7e558c7be2/lib/minver.nix [2] https://github.com/NixOS/nixpkgs/pull/357053/files#diff-791a01ef89c157eb74d9c87ab8cbc3b81e2cf082cab70b8fec3472cd75ce860dR3-R5 [3] https://github.com/NixOS/nixpkgs/pull/357053#discussion_r1857362490 --- src/libstore/build.cc | 2 ++ tests/structured-attrs.nix | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/libstore/build.cc b/src/libstore/build.cc index 3fb827a154b..6910e52c5f4 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -2584,6 +2584,7 @@ void DerivationGoal::writeStructuredAttrs() writeFile(tmpDir + "/.attrs.json", rewriteStrings(json.dump(), inputRewrites)); chownToBuilder(tmpDir + "/.attrs.json"); + env["NIX_ATTRS_JSON_FILE"] = tmpDirInSandbox + "/.attrs.json"; /* As a convenience to bash scripts, write a shell file that maps all attributes that are representable in bash - @@ -2653,6 +2654,7 @@ void DerivationGoal::writeStructuredAttrs() writeFile(tmpDir + "/.attrs.sh", rewriteStrings(jsonSh, inputRewrites)); chownToBuilder(tmpDir + "/.attrs.sh"); + env["NIX_ATTRS_SH_FILE"] = tmpDirInSandbox + "/.attrs.sh"; } diff --git a/tests/structured-attrs.nix b/tests/structured-attrs.nix index 6c77a43913a..7a9a83d603b 100644 --- a/tests/structured-attrs.nix +++ b/tests/structured-attrs.nix @@ -38,6 +38,12 @@ mkDerivation { [[ $json =~ '"narSize":288' ]] [[ $json =~ '"closureSize":288' ]] [[ $json =~ '"references":[]' ]] + + [[ -e "$NIX_ATTRS_SH_FILE" ]] + [[ -e "$NIX_ATTRS_JSON_FILE" ]] + + [[ "$(<"$NIX_ATTRS_SH_FILE")" = "$(<.attrs.sh)" ]] + [[ "$(<"$NIX_ATTRS_JSON_FILE")" = "$(<.attrs.json)" ]] ''; buildInputs = [ "a" "b" "c" 123 "'" "\"" null ];