7

when developing in nix i usually use builtins.trace for debugging:

in code

a = { foo = "bar"; ... more complex nested attrset ...};
builtins.trace a a;

in nix-repl

using :p a on a complex data structure is really hard to read also.

the problem

however, the output is just a single line without any formating and for complex data structure this is barely usable for debugging.

the question

is there a pretty print function in nix which does some indentation and adding of newline? or even better colored output?

ideal output

i'd like to see something like this:

default = {
  active = { 
    check_ssl = [
      {
        tags = [ "mycustomtag" ];
        host = "kotalla.de";
        ipv6 = false;
        name = "ssl11";
      }
      {
        tags = [ "mycustomtag" ];
        host = "kotalla.de";
        ipv6 = false;
        name = "ssl2";
      }
    ];
    check_http = [
      {
        host = "kotalla.de";
        port = 80;
        url = "/foo";
        contains = "Labor";
        name = "http";
      }
    ];
    check_ssh = [
      {
        host = "mail.lastlog.de";
        port = 20202;
        name = "ssh";
      }
    ];
  };

my hack

what i'm aware of:

qknight
  • 866
  • 10
  • 23

3 Answers3

3

we've now written our own formatter:

https://github.com/nixcloud/nix-beautify

qknight
  • 866
  • 10
  • 23
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/34900812) – Rich Aug 26 '23 at 02:28
2

Nixfmt is a popular Nix code formatter: https://github.com/Gabriel439/nixfmt

ens
  • 1,068
  • 13
  • 14
1

https://github.com/haskell-nix/hnix/tree/50e63f80afa8323b25b692533e731eea641e56af#parse--print

To parse a file with hnix and pretty print the result:

hnix file.nix

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286