Created
January 4, 2024 19:39
-
-
Save jorsn/012be3e868736359024007fc87b631cf to your computer and use it in GitHub Desktop.
Unsafely pretty-print Nix values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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