NixOS / nix.dev

Official documentation for getting things done with Nix.
https://nix.dev
Creative Commons Attribution Share Alike 4.0 International
2.48k stars 249 forks source link

JSON convert in modules error #1004

Closed jd1t25 closed 2 months ago

jd1t25 commented 3 months ago

Hi again, while continuing following and learning , under Section modules

when you run script "./eval.bash" it shows

{ pkgs ? import <nixpkgs> { } }:
  ^
 error: cannot convert a function to JSON

can we convert function to json in nix? ( did i miss something ) so i just declared pkgs inside "let .... in" block

let
  pkgs = import <nixpkgs> {};
....
in

after executing it shows

while evaluating attribute 'name'
error: cannot convert a function application to JSON

after reading wiki , i followed with --strict

nix-shell -p jq --run "nix-instantiate --eval --json --strict | jq"

now it does show the expected output.

{
  "name": "Boaty McBoatface"
}

Please check on your end , i did double check about the error and also there might be better solution than this

fricklerhandwerk commented 3 months ago

Oh damn, nix-instantiate doesn't do autocall like nix-build and nix-shell.

fricklerhandwerk commented 3 months ago

This sucks. But we don't have to change the default argument, because that's still useful if you end up building derivations. Just the command line invocation becomes cumbersome and introduces redundancy:

nix-shell -p jq --run "nix-instantiate --eval --json --strict --arg pkgs 'import <nixpkgs> {}' | jq"