aboutsummaryrefslogtreecommitdiff
path: root/modules/lib
diff options
context:
space:
mode:
authorMarcial Gaißert <gaisseml@studi.informatik.uni-stuttgart.de>2018-12-13 15:19:07 +0100
committerNikita Uvarov <uv.nikita@gmail.com>2019-01-11 10:26:12 +0100
commit62eb7ebebaba32afc09b96a3c6b464352e86ee1d (patch)
tree92c0757413f64de777a3fc89ce1ee79fb6fcdd34 /modules/lib
parentc48fd9d8422519d6f171e7cfc301743d28d08631 (diff)
lib.zsh: add module
Added utilities to generate export statements and definitions for zsh scripts. Currently, there is only lib.shell which generates export statements in bash syntax. However, this does not allow to generate export statements for zsh arrays (syntax: NAME=(elem1 elem2 ...) ), which would be the natural representation of lists in the nix language.
Diffstat (limited to 'modules/lib')
-rw-r--r--modules/lib/default.nix1
-rw-r--r--modules/lib/zsh.nix28
2 files changed, 29 insertions, 0 deletions
diff --git a/modules/lib/default.nix b/modules/lib/default.nix
index 8291be0b2ee..9ca0dad6b88 100644
--- a/modules/lib/default.nix
+++ b/modules/lib/default.nix
@@ -17,4 +17,5 @@
};
shell = import ./shell.nix { inherit lib; };
+ zsh = import ./zsh.nix { inherit lib; };
}
diff --git a/modules/lib/zsh.nix b/modules/lib/zsh.nix
new file mode 100644
index 00000000000..1d3e96b54bb
--- /dev/null
+++ b/modules/lib/zsh.nix
@@ -0,0 +1,28 @@
+{ lib }:
+
+rec {
+ # Produces a Zsh shell like value
+ toZshValue = v: if builtins.isBool v then
+ if v then "true" else "false"
+ else if builtins.isString v then
+ "\"${v}\""
+ else if builtins.isList v then
+ "(${lib.concatStringsSep " " (map toZshValue v)})"
+ else "\"${toString v}\"";
+
+ # Produces a Zsh shell like definition statement
+ define = n: v: "${n}=${toZshValue v}";
+
+ # Given an attribute set containing shell variable names and their
+ # assignments, this function produces a string containing a definition
+ # statement for each set entry.
+ defineAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList define vars);
+
+ # Produces a Zsh shell like export statement
+ export = n: v: "export ${define n v}";
+
+ # Given an attribute set containing shell variable names and their
+ # assignments, this function produces a string containing an export
+ # statement for each set entry.
+ exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars);
+}