Nix - list (print each element of a list) aka string to file aka debugging

I would like to print a list (like ["c" "d"]) - each element per row

1 Like

builtins.foldl’ (x: y: x + y + “\n”) “” [ “c” “d” ]

Explicit output is not part of the language. What you can do is to reduce the array to a multiline string.

thanks but
mh, that is kind of hard

I try to start debugging in nix

  • but for me there is missing a clear order and structure
    → that’s why I try to get more things in a readable way

How to debug not only efficiently but effective in nix??

I tried:

txt = (builtins.foldl' (x: y: x + y + "\n") ""  diff)
pkgs.writeScript "" txt

cat /nix/store/

but that is not better at all :slight_smile:

Is there a way to save the string to /home/user/?

can it be “pretty printed” like for json (but in the repl)?

nix show-derivation /nix/store/

  "/nix/store/": {
    "outputs": {
      "out": {
        "path": "/nix/store/"
    "inputSrcs": [


even something like
nix show-derivation /nix/store/["env"]["text"]
would help a little.

you can also use builtins.trace, and this will emit some value:

$ nix repl

nix-repl> b = 2

nix-repl> c = (builtins.trace b b) + 1

nix-repl> c
trace: 2

NOTE: I believe this is only evaluate the expressions one level. If you have a deeply nested object, then not everything will be evaluated.

nix-repl> b = { foo = { a = "bar"; }; "baz" = "biz"; }

nix-repl> builtins.trace b b
trace: { baz = "biz"; foo = <CODE>; }
{ baz = "biz"; foo = { ... }; }

you can try to do nix-instantiate --eval --strict to eagerly evaluate larger thunks, but it comes at the cost of evaluating potentially very large objects

Is there something specifically for a list?

thanks @jonringer,

is there a way to get stdin working → means not to save a file manually to execute a nix expression?

# nix-instantiate --eval --strict --json -E '{ foo = { a = "bar"; }; "baz" = "biz"; }' | jq 
nix-instantiate --eval --strict /dev/stdin << 'EOF'
{ foo = { a = "bar"; }; "baz" = "biz"; }
# error: getting status of '/tmp/sh-thd.naCONh (deleted)': No such file or directory

if you’re just playing around, I would use nix repl

is there a way to get stdin working → means not to save a file manually to execute a nix expression?

I’m not aware of one, but it might exist

1 Like

Functions in lib.debug (reference: NixOS - Nixpkgs 21.05 manual ) are easier to use than builtins.trace.


sure I use the repl as well but it is a hassle to delete all “;” to use it

  • to transfer a .nix file to repl for execution
nix-repl> b = { foo = { a = "bar"; }; "baz" = "biz"; };
error: syntax error, unexpected ';', expecting $end, at (string):1:42

Well, playing can get serious quite quickly … if you got the first two steps done :slight_smile:

Indeed, lib.debug.traceValSeqN looks very useful. For example, it can print values of nested structures inside each element of a list (for a given depth as its first argument).

1 Like

If you pass an argument of -, nix-instantiate will use stdin as the input file:

$ nix-instantiate --eval - <<<'1 + 1'

Personally I like to write Nix code into a file and then call nix-instantiate --eval file.nix. Perhaps even in a loop:

watch -n0.5 nix-instantiate --eval file.nix