From aa167d3e26bd4fd0fedc1ea94b7069cca2100181 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 2 Aug 2016 20:24:45 +0200 Subject: lib: import shell from krebs/4lib --- lib/shell.nix | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/shell.nix (limited to 'lib/shell.nix') diff --git a/lib/shell.nix b/lib/shell.nix new file mode 100644 index 00000000..a8ff5dbe --- /dev/null +++ b/lib/shell.nix @@ -0,0 +1,21 @@ +{ lib, ... }: + +with lib; + +rec { + escape = + let + isSafeChar = c: match "[-+./0-9:=A-Z_a-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}"; +} -- cgit v1.2.3