Skip to content

Instantly share code, notes, and snippets.

@jorsn
Created January 4, 2024 19:39
Show Gist options
  • Save jorsn/012be3e868736359024007fc87b631cf to your computer and use it in GitHub Desktop.
Save jorsn/012be3e868736359024007fc87b631cf 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