1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-11 04:56:01 +01:00
nix/tests/structured-attrs.nix
Maximilian Bosch 0c5333a4f6
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] f4bd97b8fa/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
2024-12-08 15:45:05 +01:00

72 lines
1.4 KiB
Nix

with import ./config.nix;
let
dep = mkDerivation {
name = "dep";
buildCommand = ''
mkdir $out; echo bla > $out/bla
'';
};
in
mkDerivation {
name = "structured";
__structuredAttrs = true;
buildCommand = ''
set -x
[[ $int = 123456789 ]]
[[ -z $float ]]
[[ -n $boolTrue ]]
[[ -z $boolFalse ]]
[[ -n ''${hardening[format]} ]]
[[ -z ''${hardening[fortify]} ]]
[[ ''${#buildInputs[@]} = 7 ]]
[[ ''${buildInputs[2]} = c ]]
[[ -v nothing ]]
[[ -z $nothing ]]
mkdir ''${outputs[out]}
echo bar > $dest
json=$(cat .attrs.json)
[[ $json =~ '"narHash":"sha256:1r7yc43zqnzl5b0als5vnyp649gk17i37s7mj00xr8kc47rjcybk"' ]]
[[ $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 ];
hardening.format = true;
hardening.fortify = false;
outer.inner = [ 1 2 3 ];
int = 123456789;
float = 123.456;
boolTrue = true;
boolFalse = false;
nothing = null;
dest = "${placeholder "out"}/foo";
"foo bar" = "BAD";
"1foobar" = "BAD";
"foo$" = "BAD";
exportReferencesGraph.refs = [ dep ];
}