NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.94k stars 1.53k forks source link

nix eval --json returns a wrong output with builtins.fetch{Mercurial/Git} #2354

Open AmineChikhaoui opened 6 years ago

AmineChikhaoui commented 6 years ago
[amine@nixos:~]$ nix eval '(builtins.fetchGit https://github.com/NixOS/hydra)'
{ outPath = "/nix/store/gmypid1qvrxpvqfh3dynhw8jap3j60cn-source"; rev = "b542b73ced7a1ef6bc8f83e2918672068ccbc52c"; revCount = 2544; shortRev = "b542b73"; }

[amine@nixos:~]$ nix eval '(builtins.fetchGit https://github.com/NixOS/hydra)' --json
"/nix/store/gmypid1qvrxpvqfh3dynhw8jap3j60cn-source"

[amine@nixos:~]$ nix-instantiate -E '(builtins.fetchGit https://github.com/NixOS/hydra)' --json --eval-only
"/nix/store/gmypid1qvrxpvqfh3dynhw8jap3j60cn-source"
AmineChikhaoui commented 6 years ago

Btw --xml seems to work fine:

[amine@nixos:~/src/nix]$ nix-instantiate -E '(builtins.fetchGit https://github.com/NixOS/hydra)' --xml --eval-only --strict 
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr name="outPath">
      <string value="/nix/store/gmypid1qvrxpvqfh3dynhw8jap3j60cn-source" />
    </attr>
    <attr name="rev">
      <string value="b542b73ced7a1ef6bc8f83e2918672068ccbc52c" />
    </attr>
    <attr name="revCount">
      <int value="2544" />
    </attr>
    <attr name="shortRev">
      <string value="b542b73" />
    </attr>
  </attrs>
</expr>
edolstra commented 6 years ago

The issue here is that --json uses the same code as builtins.toJSON, which renders derivations as strings to prevent infinite recursion. (Regular derivations are circular data structures because the <output> attributes refer back to themselves.)

AmineChikhaoui commented 6 years ago

@edolstra Hm ok, so I guess it would be a bit hard to fix. I was planning to use the json output to parse it from perl in hydra :/

copumpkin commented 6 years ago

@edolstra isn't it using type = "derivation" to decide that? Or simply looking for an outPath key?

copumpkin commented 6 years ago

Ah I guess it just looks for outPath, so:

nix-repl> builtins.toJSON { outPath = "blah"; x = 5; } 
"\"blah\""
lambdadog commented 3 years ago

@AmineChikhaoui This is an old issue, but this can be fixed by reassigning outPath to something like _outPath as I've done in my own projects. Since _outPath will still be pointing to a valid derivation, it will output as the path (as you're seeing here), but the root derivation will be printed to JSON properly.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info