Skip to content

Instantly share code, notes, and snippets.

@datsfilipe
Forked from jorsn/prettyUnsafe.nix
Created January 3, 2025 18:11
Show Gist options
  • Save datsfilipe/cafd4f3c3b38b956d4ae49dc3fbf7930 to your computer and use it in GitHub Desktop.
Save datsfilipe/cafd4f3c3b38b956d4ae49dc3fbf7930 to your computer and use it in GitHub Desktop.
Unsafely pretty-print Nix values
with builtins;
let
printChild = prefix: x:
let
names = attrNames x;
in
if isAttrs x && length names == 1
then "." + head names + printChild prefix x.${head names}
else " = " + print prefix x
;
mapAttrsToList = f: attrs: attrValues (mapAttrs f attrs);
mapAttrsToLines = f: attrs: concatStringsSep "\n" (mapAttrsToList f attrs);
print = prefix: x:
if isString x
then "\"${x}\""
else if ! isAttrs x
then toString x
else let prefix' = prefix + " "; in ''
{
${mapAttrsToLines (n: v: prefix' + n + printChild prefix' v + ";") x}
${prefix}}'';
in print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment