nix icon indicating copy to clipboard operation
nix copied to clipboard

`nix-instantiate --eval` output is ambiguous

Open 9999years opened this issue 2 years ago • 6 comments

Describe the bug

nix-instantiate --eval output is ambiguous and can be misleading. nix-instantiate uses angle brackets like <CODE> to represent placeholders; this is ambiguous because <CODE> is valid Nix path syntax.

This results in the unintuitive result that nix-instantiate --parse and nix-instantiate --eval do not mirror each other or compose together. This also makes nix-instantiate --eval output unsuitable for machine consumption.

Steps To Reproduce

$ nix-instantiate --eval --expr '{ a = {}; }'
{ a = <CODE>; }
$ nix-instantiate --eval --expr '{ a = <CODE>; }'
{ a = <CODE>; }

Expected behavior

Placeholders in nix-instantiate output should not be valid/parsable Nix expressions.

If nix-instantiate's output used chevrons in the style of nix repl (e.g., «code» instead of <CODE>), then attempting to parse them as Nix expressions would fail instead of producing an unrelated expression. @edolstra cited this reasoning when changing <REPEAT> to «repeated» in nix-instantiate output in https://github.com/NixOS/nix/commit/732296ddc078f9cce8ffeb3131fef8898330b6ae.

nix-env --version output

nix-env (Nix) 2.18.1

Additional context

#8671, #8672, #9606, "Is <CODE> in nix-instantiate part of the stable API?" on NixOS discourse

Priorities

Add :+1: to issues you find important.

9999years avatar Jan 09 '24 21:01 9999years

While it is not particularly elegant, the output is not ambiguous. There is no way to make nix-instantiate --eval (using --strict or not) to print <CODE> except if the value shown is a thunk: nix-instantiate -E '{ x = <nixpkgs>; }' will produce { x = <CODE>; } as an output just the same as feeding that output into nix-instantiate again. The result of a forced <…> expression would be printed as a path and never as a string in angle brackets. (<CODE> would then fail given most people's NIX_PATH).

sternenseemann avatar Jan 12 '24 16:01 sternenseemann

Discussed during the Nix maintenance team meeting on 2024-01-12. We'll keep this open for reference, but probably don't want to act on this

Keep for reference, but we probably don't want to act on this.
  • @edolstra nix-instantiate --eval was never meant to generate a valid Nix expression any way

thufschmitt avatar Jan 15 '24 10:01 thufschmitt

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2021-01-11-nix-team-meeting-minutes-115/38277/1

nixos-discourse avatar Jan 15 '24 11:01 nixos-discourse

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/whats-nixs-stable-api-is-code-in-nix-instantiate-part-of-it/37986/5

nixos-discourse avatar Jan 31 '24 23:01 nixos-discourse

:+1: on changing <CODE> to «code».

edolstra avatar Feb 08 '24 09:02 edolstra

Coincidentally, is there a way to eagerly print blocks that would otherwise be <CODE> with nix-instantiate?

con-f-use avatar Aug 25 '24 01:08 con-f-use