aboutsummaryrefslogtreecommitdiff
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2018-03-26 17:31:05 +0200
committerProfpatsch <mail@profpatsch.de>2018-03-29 16:53:06 +0200
commitfa71407f3656b0bf65d40e94a20513d56bcf3c61 (patch)
tree6983417618af26bb11f5c63f6c7dd6bb240c7b79 /lib/generators.nix
parenta7e45fdd8eddca06c2c5db013fe8f9dc4475e1b5 (diff)
lib/generators: introduce a sane default for `mkValueString`
So far, `mkValueString` defaulted to `toString`, which is a bad match for most configuration file formats, especially because how booleans are formatted. This also improves error messages for unsupported types. Add a test to codify the formatting.
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix26
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 6adf0c2afbc7..d1a8f6bf8dcd 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -28,6 +28,30 @@ rec {
## -- HELPER FUNCTIONS & DEFAULTS --
+ /* Convert a value to a sensible default string representation.
+ * The builtin `toString` function has some strange defaults,
+ * suitable for bash scripts but not much else.
+ */
+ mkValueStringDefault = {}: v: with builtins;
+ let err = t: v: abort
+ ("generators.mkValueStringDefault: " +
+ "${t} not supported: ${toPretty {} v}");
+ in if isInt v then toString v
+ # we default to not quoting strings
+ else if isString v then v
+ # isString returns "1", which is not a good default
+ else if true == v then "true"
+ # here it returns to "", which is even less of a good default
+ else if false == v then "false"
+ else if null == v then "null"
+ # if you have lists you probably want to replace this
+ else if isList v then err "lists" v
+ # same as for lists, might want to replace
+ else if isAttrs v then err "attrsets" v
+ else if isFunction v then err "functions" v
+ else err "this value is" (toString v);
+
+
/* Generate a line of key k and value v, separated by
* character sep. If sep appears in k, it is escaped.
* Helper for synaxes with different separators.
@@ -38,7 +62,7 @@ rec {
* > "f\:oo:bar"
*/
mkKeyValueDefault = {
- mkValueString ? toString
+ mkValueString ? mkValueStringDefault {}
}: sep: k: v:
"${libStr.escape [sep] k}${sep}${mkValueString v}";