Skip to content

Commit

Permalink
build: backport NIX_ATTRS_*_FILE
Browse files Browse the repository at this point in the history
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] NixOS/nixpkgs#357053 (comment)
  • Loading branch information
Ma27 committed Dec 8, 2024
1 parent 99f14d4 commit 0c5333a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libstore/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down Expand Up @@ -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";
}


Expand Down
6 changes: 6 additions & 0 deletions tests/structured-attrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down

0 comments on commit 0c5333a

Please sign in to comment.