diff options
Diffstat (limited to 'krebs/4lib')
-rw-r--r-- | krebs/4lib/default.nix | 7 | ||||
-rw-r--r-- | krebs/4lib/shell.nix | 22 |
2 files changed, 29 insertions, 0 deletions
diff --git a/krebs/4lib/default.nix b/krebs/4lib/default.nix index b67585335..ca7219c7e 100644 --- a/krebs/4lib/default.nix +++ b/krebs/4lib/default.nix @@ -14,5 +14,12 @@ builtins // lib // rec { dns = import ./dns.nix { inherit lib; }; listset = import ./listset.nix { inherit lib; }; + shell = import ./shell.nix { inherit lib; }; tree = import ./tree.nix { inherit lib; }; + + toC = x: { + list = "{ ${concatStringsSep ", " (map toC x)} }"; + null = "NULL"; + string = toJSON x; # close enough + }.${typeOf x}; } diff --git a/krebs/4lib/shell.nix b/krebs/4lib/shell.nix new file mode 100644 index 000000000..2a6da5c16 --- /dev/null +++ b/krebs/4lib/shell.nix @@ -0,0 +1,22 @@ +{ lib, ... }: + +with builtins; +with lib; + +rec { + escape = + let + isSafeChar = c: match "[-./0-9_a-zA-Z]" c != null; + in + stringAsChars (c: + if isSafeChar c then c + else if c == "\n" then "'\n'" + else "\\${c}"); + + # + # shell script generators + # + + # example: "${cat (toJSON { foo = "bar"; })} | jq -r .foo" + cat = s: "printf '%s' ${escape s}"; +} |