2016-11-17 22:29:32 +01:00
|
|
|
/* Functions that generate widespread file
|
2016-11-06 01:51:13 +01:00
|
|
|
* formats from nix data structures.
|
2016-11-17 22:29:32 +01:00
|
|
|
*
|
|
|
|
* They all follow a similar interface:
|
|
|
|
* generator { config-attrs } data
|
|
|
|
*
|
2016-11-06 01:51:13 +01:00
|
|
|
* Tests can be found in ./tests.nix
|
2016-11-17 22:29:32 +01:00
|
|
|
* Documentation in the manual, #sec-generators
|
2016-11-06 01:51:13 +01:00
|
|
|
*/
|
|
|
|
with import ./trivial.nix;
|
|
|
|
let
|
|
|
|
libStr = import ./strings.nix;
|
|
|
|
libAttr = import ./attrsets.nix;
|
|
|
|
|
|
|
|
flipMapAttrs = flip libAttr.mapAttrs;
|
|
|
|
in
|
|
|
|
|
2016-11-06 14:14:24 +01:00
|
|
|
rec {
|
2016-11-06 01:51:13 +01:00
|
|
|
|
|
|
|
/* Generates an INI-style config file from an
|
|
|
|
* attrset of sections to an attrset of key-value pairs.
|
|
|
|
*
|
|
|
|
* generators.toINI {} {
|
|
|
|
* foo = { hi = "${pkgs.hello}"; ciao = "bar"; };
|
|
|
|
* baz = { "also, integers" = 42; };
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*> [baz]
|
|
|
|
*> also, integers=42
|
|
|
|
*>
|
|
|
|
*> [foo]
|
|
|
|
*> ciao=bar
|
|
|
|
*> hi=/nix/store/y93qql1p5ggfnaqjjqhxcw0vqw95rlz0-hello-2.10
|
|
|
|
*
|
|
|
|
* The mk* configuration attributes can generically change
|
|
|
|
* the way sections and key-value strings are generated.
|
|
|
|
*
|
|
|
|
* For more examples see the test cases in ./tests.nix.
|
|
|
|
*/
|
|
|
|
toINI = {
|
|
|
|
# apply transformations (e.g. escapes) to section names
|
|
|
|
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
|
|
|
|
# format a setting line from key and value
|
|
|
|
mkKeyValue ? (k: v: "${libStr.escape ["="] k}=${toString v}")
|
|
|
|
}: attrsOfAttrs:
|
|
|
|
let
|
|
|
|
# map function to string for each key val
|
|
|
|
mapAttrsToStringsSep = sep: mapFn: attrs:
|
|
|
|
libStr.concatStringsSep sep
|
|
|
|
(libAttr.mapAttrsToList mapFn attrs);
|
|
|
|
mkLine = k: v: mkKeyValue k v + "\n";
|
|
|
|
mkSection = sectName: sectValues: ''
|
|
|
|
[${mkSectionName sectName}]
|
|
|
|
'' + libStr.concatStrings (libAttr.mapAttrsToList mkLine sectValues);
|
|
|
|
in
|
|
|
|
# map input to ini sections
|
|
|
|
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
|
2016-11-06 14:14:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Generates JSON from an arbitrary (non-function) value.
|
|
|
|
* For more information see the documentation of the builtin.
|
|
|
|
*/
|
|
|
|
toJSON = {}: builtins.toJSON;
|
|
|
|
|
|
|
|
|
|
|
|
/* YAML has been a strict superset of JSON since 1.2, so we
|
|
|
|
* use toJSON. Before it only had a few differences referring
|
|
|
|
* to implicit typing rules, so it should work with older
|
|
|
|
* parsers as well.
|
|
|
|
*/
|
|
|
|
toYAML = {}@args: toJSON args;
|
2016-11-06 01:51:13 +01:00
|
|
|
}
|