2016-11-17 21:29:32 +00:00
|
|
|
|
/* Functions that generate widespread file
|
2016-11-06 00:51:13 +00:00
|
|
|
|
* formats from nix data structures.
|
2016-11-17 21:29:32 +00:00
|
|
|
|
*
|
|
|
|
|
* They all follow a similar interface:
|
|
|
|
|
* generator { config-attrs } data
|
|
|
|
|
*
|
2018-03-26 15:28:17 +00:00
|
|
|
|
* `config-attrs` are “holes” in the generators
|
|
|
|
|
* with sensible default implementations that
|
|
|
|
|
* can be overwritten. The default implementations
|
|
|
|
|
* are mostly generators themselves, called with
|
|
|
|
|
* their respective default values; they can be reused.
|
|
|
|
|
*
|
2022-03-14 09:30:20 +00:00
|
|
|
|
* Tests can be found in ./tests/misc.nix
|
2016-11-17 21:29:32 +00:00
|
|
|
|
* Documentation in the manual, #sec-generators
|
2016-11-06 00:51:13 +00:00
|
|
|
|
*/
|
2017-07-29 00:05:35 +00:00
|
|
|
|
{ lib }:
|
|
|
|
|
with (lib).trivial;
|
2016-11-06 00:51:13 +00:00
|
|
|
|
let
|
2017-07-29 00:05:35 +00:00
|
|
|
|
libStr = lib.strings;
|
|
|
|
|
libAttr = lib.attrsets;
|
2016-11-06 00:51:13 +00:00
|
|
|
|
|
2018-01-31 19:02:19 +00:00
|
|
|
|
inherit (lib) isFunction;
|
2016-11-06 00:51:13 +00:00
|
|
|
|
in
|
|
|
|
|
|
2016-11-06 13:14:24 +00:00
|
|
|
|
rec {
|
2016-11-06 00:51:13 +00:00
|
|
|
|
|
2018-03-26 15:28:17 +00:00
|
|
|
|
## -- HELPER FUNCTIONS & DEFAULTS --
|
|
|
|
|
|
2018-03-26 15:31:05 +00:00
|
|
|
|
/* 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
|
2021-09-12 04:23:30 +00:00
|
|
|
|
# convert derivations to store paths
|
|
|
|
|
else if lib.isDerivation v then toString v
|
2018-03-26 15:31:05 +00:00
|
|
|
|
# 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
|
2020-01-23 00:07:02 +00:00
|
|
|
|
# functions can’t be printed of course
|
2018-03-26 15:31:05 +00:00
|
|
|
|
else if isFunction v then err "functions" v
|
2019-12-12 23:24:30 +00:00
|
|
|
|
# Floats currently can't be converted to precise strings,
|
|
|
|
|
# condition warning on nix version once this isn't a problem anymore
|
|
|
|
|
# See https://github.com/NixOS/nix/pull/3480
|
|
|
|
|
else if isFloat v then libStr.floatToString v
|
2018-03-26 15:31:05 +00:00
|
|
|
|
else err "this value is" (toString v);
|
|
|
|
|
|
|
|
|
|
|
2016-12-04 21:11:24 +00:00
|
|
|
|
/* 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.
|
|
|
|
|
*
|
2017-11-09 14:58:14 +00:00
|
|
|
|
* mkValueString specifies how values should be formatted.
|
|
|
|
|
*
|
|
|
|
|
* mkKeyValueDefault {} ":" "f:oo" "bar"
|
2016-12-04 21:11:24 +00:00
|
|
|
|
* > "f\:oo:bar"
|
|
|
|
|
*/
|
2017-11-09 14:58:14 +00:00
|
|
|
|
mkKeyValueDefault = {
|
2018-03-26 15:31:05 +00:00
|
|
|
|
mkValueString ? mkValueStringDefault {}
|
2017-11-09 14:58:14 +00:00
|
|
|
|
}: sep: k: v:
|
|
|
|
|
"${libStr.escape [sep] k}${sep}${mkValueString v}";
|
2016-12-04 21:11:24 +00:00
|
|
|
|
|
|
|
|
|
|
2018-03-26 15:28:17 +00:00
|
|
|
|
## -- FILE FORMAT GENERATORS --
|
|
|
|
|
|
|
|
|
|
|
2016-12-04 21:11:24 +00:00
|
|
|
|
/* Generate a key-value-style config file from an attrset.
|
|
|
|
|
*
|
|
|
|
|
* mkKeyValue is the same as in toINI.
|
|
|
|
|
*/
|
|
|
|
|
toKeyValue = {
|
2020-03-10 01:38:28 +00:00
|
|
|
|
mkKeyValue ? mkKeyValueDefault {} "=",
|
|
|
|
|
listsAsDuplicateKeys ? false
|
|
|
|
|
}:
|
|
|
|
|
let mkLine = k: v: mkKeyValue k v + "\n";
|
|
|
|
|
mkLines = if listsAsDuplicateKeys
|
|
|
|
|
then k: v: map (mkLine k) (if lib.isList v then v else [v])
|
|
|
|
|
else k: v: [ (mkLine k v) ];
|
|
|
|
|
in attrs: libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs));
|
2016-12-04 21:11:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Generate an INI-style config file from an
|
2016-11-06 00:51:13 +00:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2022-03-14 09:30:20 +00:00
|
|
|
|
* For more examples see the test cases in ./tests/misc.nix.
|
2016-11-06 00:51:13 +00:00
|
|
|
|
*/
|
|
|
|
|
toINI = {
|
|
|
|
|
# apply transformations (e.g. escapes) to section names
|
|
|
|
|
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
|
|
|
|
|
# format a setting line from key and value
|
2020-03-10 01:38:28 +00:00
|
|
|
|
mkKeyValue ? mkKeyValueDefault {} "=",
|
|
|
|
|
# allow lists as values for duplicate keys
|
|
|
|
|
listsAsDuplicateKeys ? false
|
2016-11-06 00:51:13 +00:00
|
|
|
|
}: attrsOfAttrs:
|
|
|
|
|
let
|
|
|
|
|
# map function to string for each key val
|
|
|
|
|
mapAttrsToStringsSep = sep: mapFn: attrs:
|
|
|
|
|
libStr.concatStringsSep sep
|
|
|
|
|
(libAttr.mapAttrsToList mapFn attrs);
|
|
|
|
|
mkSection = sectName: sectValues: ''
|
|
|
|
|
[${mkSectionName sectName}]
|
2020-03-10 01:38:28 +00:00
|
|
|
|
'' + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
|
2016-11-06 00:51:13 +00:00
|
|
|
|
in
|
|
|
|
|
# map input to ini sections
|
|
|
|
|
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
|
2016-11-06 13:14:24 +00:00
|
|
|
|
|
2022-03-14 09:14:18 +00:00
|
|
|
|
/* Generate an INI-style config file from an attrset
|
|
|
|
|
* specifying the global section (no header), and an
|
|
|
|
|
* attrset of sections to an attrset of key-value pairs.
|
|
|
|
|
*
|
|
|
|
|
* generators.toINIWithGlobalSection {} {
|
|
|
|
|
* globalSection = {
|
|
|
|
|
* someGlobalKey = "hi";
|
|
|
|
|
* };
|
|
|
|
|
* sections = {
|
|
|
|
|
* foo = { hi = "${pkgs.hello}"; ciao = "bar"; };
|
|
|
|
|
* baz = { "also, integers" = 42; };
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
*> someGlobalKey=hi
|
|
|
|
|
*>
|
|
|
|
|
*> [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.
|
|
|
|
|
*
|
2022-03-14 09:30:20 +00:00
|
|
|
|
* For more examples see the test cases in ./tests/misc.nix.
|
2022-03-14 09:14:18 +00:00
|
|
|
|
*
|
|
|
|
|
* If you don’t need a global section, you can also use
|
|
|
|
|
* `generators.toINI` directly, which only takes
|
|
|
|
|
* the part in `sections`.
|
|
|
|
|
*/
|
|
|
|
|
toINIWithGlobalSection = {
|
|
|
|
|
# apply transformations (e.g. escapes) to section names
|
|
|
|
|
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
|
|
|
|
|
# format a setting line from key and value
|
|
|
|
|
mkKeyValue ? mkKeyValueDefault {} "=",
|
|
|
|
|
# allow lists as values for duplicate keys
|
|
|
|
|
listsAsDuplicateKeys ? false
|
|
|
|
|
}: { globalSection, sections }:
|
|
|
|
|
( if globalSection == {}
|
|
|
|
|
then ""
|
|
|
|
|
else (toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection)
|
|
|
|
|
+ "\n")
|
|
|
|
|
+ (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections);
|
|
|
|
|
|
2020-03-05 17:59:32 +00:00
|
|
|
|
/* Generate a git-config file from an attrset.
|
|
|
|
|
*
|
|
|
|
|
* It has two major differences from the regular INI format:
|
|
|
|
|
*
|
|
|
|
|
* 1. values are indented with tabs
|
|
|
|
|
* 2. sections can have sub-sections
|
|
|
|
|
*
|
|
|
|
|
* generators.toGitINI {
|
|
|
|
|
* url."ssh://git@github.com/".insteadOf = "https://github.com";
|
|
|
|
|
* user.name = "edolstra";
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
*> [url "ssh://git@github.com/"]
|
|
|
|
|
*> insteadOf = https://github.com/
|
|
|
|
|
*>
|
|
|
|
|
*> [user]
|
|
|
|
|
*> name = edolstra
|
|
|
|
|
*/
|
|
|
|
|
toGitINI = attrs:
|
|
|
|
|
with builtins;
|
|
|
|
|
let
|
|
|
|
|
mkSectionName = name:
|
|
|
|
|
let
|
|
|
|
|
containsQuote = libStr.hasInfix ''"'' name;
|
|
|
|
|
sections = libStr.splitString "." name;
|
|
|
|
|
section = head sections;
|
|
|
|
|
subsections = tail sections;
|
|
|
|
|
subsection = concatStringsSep "." subsections;
|
|
|
|
|
in if containsQuote || subsections == [ ] then
|
|
|
|
|
name
|
|
|
|
|
else
|
|
|
|
|
''${section} "${subsection}"'';
|
|
|
|
|
|
|
|
|
|
# generation for multiple ini values
|
|
|
|
|
mkKeyValue = k: v:
|
|
|
|
|
let mkKeyValue = mkKeyValueDefault { } " = " k;
|
|
|
|
|
in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v));
|
|
|
|
|
|
|
|
|
|
# converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
|
|
|
|
|
gitFlattenAttrs = let
|
|
|
|
|
recurse = path: value:
|
2021-09-12 04:19:25 +00:00
|
|
|
|
if isAttrs value && !lib.isDerivation value then
|
2020-03-05 17:59:32 +00:00
|
|
|
|
lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
|
|
|
|
|
else if length path > 1 then {
|
|
|
|
|
${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value;
|
|
|
|
|
} else {
|
|
|
|
|
${head path} = value;
|
|
|
|
|
};
|
|
|
|
|
in attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));
|
|
|
|
|
|
|
|
|
|
toINI_ = toINI { inherit mkKeyValue mkSectionName; };
|
|
|
|
|
in
|
|
|
|
|
toINI_ (gitFlattenAttrs attrs);
|
2016-11-06 13:14:24 +00: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.
|
|
|
|
|
*/
|
2022-09-28 20:34:55 +00:00
|
|
|
|
toYAML = toJSON;
|
2017-06-06 20:41:22 +00:00
|
|
|
|
|
2021-08-25 22:28:49 +00:00
|
|
|
|
withRecursion =
|
2022-09-27 02:04:07 +00:00
|
|
|
|
{
|
2021-09-28 20:26:51 +00:00
|
|
|
|
/* If this option is not null, the given value will stop evaluating at a certain depth */
|
2021-08-25 22:28:49 +00:00
|
|
|
|
depthLimit
|
|
|
|
|
/* If this option is true, an error will be thrown, if a certain given depth is exceeded */
|
|
|
|
|
, throwOnDepthLimit ? true
|
|
|
|
|
}:
|
|
|
|
|
assert builtins.isInt depthLimit;
|
|
|
|
|
let
|
2022-04-12 10:26:25 +00:00
|
|
|
|
specialAttrs = [
|
|
|
|
|
"__functor"
|
|
|
|
|
"__functionArgs"
|
|
|
|
|
"__toString"
|
|
|
|
|
"__pretty"
|
|
|
|
|
];
|
|
|
|
|
stepIntoAttr = evalNext: name:
|
|
|
|
|
if builtins.elem name specialAttrs
|
|
|
|
|
then id
|
|
|
|
|
else evalNext;
|
2021-08-25 22:28:49 +00:00
|
|
|
|
transform = depth:
|
|
|
|
|
if depthLimit != null && depth > depthLimit then
|
|
|
|
|
if throwOnDepthLimit
|
2021-09-28 20:26:51 +00:00
|
|
|
|
then throw "Exceeded maximum eval-depth limit of ${toString depthLimit} while trying to evaluate with `generators.withRecursion'!"
|
2021-08-25 22:28:49 +00:00
|
|
|
|
else const "<unevaluated>"
|
|
|
|
|
else id;
|
|
|
|
|
mapAny = with builtins; depth: v:
|
|
|
|
|
let
|
|
|
|
|
evalNext = x: mapAny (depth + 1) (transform (depth + 1) x);
|
|
|
|
|
in
|
2022-04-12 10:26:25 +00:00
|
|
|
|
if isAttrs v then mapAttrs (stepIntoAttr evalNext) v
|
2021-08-25 22:28:49 +00:00
|
|
|
|
else if isList v then map evalNext v
|
|
|
|
|
else transform (depth + 1) v;
|
|
|
|
|
in
|
|
|
|
|
mapAny 0;
|
2018-03-26 15:26:20 +00:00
|
|
|
|
|
2017-06-06 20:41:22 +00:00
|
|
|
|
/* Pretty print a value, akin to `builtins.trace`.
|
|
|
|
|
* Should probably be a builtin as well.
|
|
|
|
|
*/
|
|
|
|
|
toPretty = {
|
|
|
|
|
/* If this option is true, attrsets like { __pretty = fn; val = …; }
|
|
|
|
|
will use fn to convert val to a pretty printed representation.
|
|
|
|
|
(This means fn is type Val -> String.) */
|
2020-09-04 15:46:12 +00:00
|
|
|
|
allowPrettyValues ? false,
|
|
|
|
|
/* If this option is true, the output is indented with newlines for attribute sets and lists */
|
|
|
|
|
multiline ? true
|
2022-09-27 02:04:07 +00:00
|
|
|
|
}:
|
2021-07-23 08:43:44 +00:00
|
|
|
|
let
|
2020-09-04 15:46:12 +00:00
|
|
|
|
go = indent: v: with builtins;
|
2018-04-25 13:04:30 +00:00
|
|
|
|
let isPath = v: typeOf v == "path";
|
2020-09-04 15:46:12 +00:00
|
|
|
|
introSpace = if multiline then "\n${indent} " else " ";
|
|
|
|
|
outroSpace = if multiline then "\n${indent}" else " ";
|
2018-04-25 13:04:30 +00:00
|
|
|
|
in if isInt v then toString v
|
2018-10-13 17:55:00 +00:00
|
|
|
|
else if isFloat v then "~${toString v}"
|
2020-09-17 15:58:04 +00:00
|
|
|
|
else if isString v then
|
|
|
|
|
let
|
|
|
|
|
# Separate a string into its lines
|
|
|
|
|
newlineSplits = filter (v: ! isList v) (builtins.split "\n" v);
|
|
|
|
|
# For a '' string terminated by a \n, which happens when the closing '' is on a new line
|
|
|
|
|
multilineResult = "''" + introSpace + concatStringsSep introSpace (lib.init newlineSplits) + outroSpace + "''";
|
|
|
|
|
# For a '' string not terminated by a \n, which happens when the closing '' is not on a new line
|
|
|
|
|
multilineResult' = "''" + introSpace + concatStringsSep introSpace newlineSplits + "''";
|
|
|
|
|
# For single lines, replace all newlines with their escaped representation
|
|
|
|
|
singlelineResult = "\"" + libStr.escape [ "\"" ] (concatStringsSep "\\n" newlineSplits) + "\"";
|
|
|
|
|
in if multiline && length newlineSplits > 1 then
|
|
|
|
|
if lib.last newlineSplits == "" then multilineResult else multilineResult'
|
|
|
|
|
else singlelineResult
|
2018-03-26 15:26:20 +00:00
|
|
|
|
else if true == v then "true"
|
|
|
|
|
else if false == v then "false"
|
2018-04-25 13:04:30 +00:00
|
|
|
|
else if null == v then "null"
|
|
|
|
|
else if isPath v then toString v
|
2020-09-17 16:14:18 +00:00
|
|
|
|
else if isList v then
|
|
|
|
|
if v == [] then "[ ]"
|
|
|
|
|
else "[" + introSpace
|
2020-09-04 15:46:12 +00:00
|
|
|
|
+ libStr.concatMapStringsSep introSpace (go (indent + " ")) v
|
2020-09-17 16:14:18 +00:00
|
|
|
|
+ outroSpace + "]"
|
2020-09-17 16:18:50 +00:00
|
|
|
|
else if isFunction v then
|
2021-02-01 15:27:38 +00:00
|
|
|
|
let fna = lib.functionArgs v;
|
2020-09-17 16:18:50 +00:00
|
|
|
|
showFnas = concatStringsSep ", " (libAttr.mapAttrsToList
|
|
|
|
|
(name: hasDefVal: if hasDefVal then name + "?" else name)
|
2021-02-01 15:27:38 +00:00
|
|
|
|
fna);
|
|
|
|
|
in if fna == {} then "<function>"
|
|
|
|
|
else "<function, args: {${showFnas}}>"
|
2017-06-06 20:41:22 +00:00
|
|
|
|
else if isAttrs v then
|
|
|
|
|
# apply pretty values if allowed
|
|
|
|
|
if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
|
|
|
|
|
then v.__pretty v.val
|
2020-09-17 16:14:18 +00:00
|
|
|
|
else if v == {} then "{ }"
|
2018-04-25 13:04:30 +00:00
|
|
|
|
else if v ? type && v.type == "derivation" then
|
2021-08-11 13:50:33 +00:00
|
|
|
|
"<derivation ${v.drvPath or "???"}>"
|
2020-09-04 15:46:12 +00:00
|
|
|
|
else "{" + introSpace
|
|
|
|
|
+ libStr.concatStringsSep introSpace (libAttr.mapAttrsToList
|
2017-06-06 20:41:22 +00:00
|
|
|
|
(name: value:
|
2020-09-04 15:46:12 +00:00
|
|
|
|
"${libStr.escapeNixIdentifier name} = ${go (indent + " ") value};") v)
|
|
|
|
|
+ outroSpace + "}"
|
2018-06-28 15:12:39 +00:00
|
|
|
|
else abort "generators.toPretty: should never happen (v = ${v})";
|
2020-09-04 15:46:12 +00:00
|
|
|
|
in go "";
|
2017-06-06 20:41:22 +00:00
|
|
|
|
|
2018-06-27 19:35:07 +00:00
|
|
|
|
# PLIST handling
|
2018-06-28 15:12:39 +00:00
|
|
|
|
toPlist = {}: v: let
|
2018-07-03 16:24:54 +00:00
|
|
|
|
isFloat = builtins.isFloat or (x: false);
|
|
|
|
|
expr = ind: x: with builtins;
|
2019-04-24 03:48:22 +00:00
|
|
|
|
if x == null then "" else
|
2018-07-03 16:24:54 +00:00
|
|
|
|
if isBool x then bool ind x else
|
|
|
|
|
if isInt x then int ind x else
|
2018-06-28 15:12:39 +00:00
|
|
|
|
if isString x then str ind x else
|
2018-07-03 16:24:54 +00:00
|
|
|
|
if isList x then list ind x else
|
|
|
|
|
if isAttrs x then attrs ind x else
|
|
|
|
|
if isFloat x then float ind x else
|
2018-06-28 15:12:39 +00:00
|
|
|
|
abort "generators.toPlist: should never happen (v = ${v})";
|
2018-06-28 15:11:19 +00:00
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
literal = ind: x: ind + x;
|
2018-06-28 15:11:19 +00:00
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
bool = ind: x: literal ind (if x then "<true/>" else "<false/>");
|
|
|
|
|
int = ind: x: literal ind "<integer>${toString x}</integer>";
|
|
|
|
|
str = ind: x: literal ind "<string>${x}</string>";
|
|
|
|
|
key = ind: x: literal ind "<key>${x}</key>";
|
2018-07-03 16:24:54 +00:00
|
|
|
|
float = ind: x: literal ind "<real>${toString x}</real>";
|
2018-06-28 15:11:19 +00:00
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
indent = ind: expr "\t${ind}";
|
2018-06-28 15:11:19 +00:00
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
item = ind: libStr.concatMapStringsSep "\n" (indent ind);
|
2018-06-28 15:11:19 +00:00
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
list = ind: x: libStr.concatStringsSep "\n" [
|
|
|
|
|
(literal ind "<array>")
|
|
|
|
|
(item ind x)
|
|
|
|
|
(literal ind "</array>")
|
2018-06-28 15:11:19 +00:00
|
|
|
|
];
|
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
attrs = ind: x: libStr.concatStringsSep "\n" [
|
|
|
|
|
(literal ind "<dict>")
|
|
|
|
|
(attr ind x)
|
|
|
|
|
(literal ind "</dict>")
|
2018-06-28 15:11:19 +00:00
|
|
|
|
];
|
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
attr = let attrFilter = name: value: name != "_module" && value != null;
|
2018-06-28 15:11:19 +00:00
|
|
|
|
in ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList
|
2022-10-06 16:38:53 +00:00
|
|
|
|
(name: value: lib.optionals (attrFilter name value) [
|
2018-06-28 15:12:39 +00:00
|
|
|
|
(key "\t${ind}" name)
|
|
|
|
|
(expr "\t${ind}" value)
|
2018-06-28 15:11:19 +00:00
|
|
|
|
]) x));
|
|
|
|
|
|
2018-06-28 15:12:39 +00:00
|
|
|
|
in ''<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
|
<plist version="1.0">
|
|
|
|
|
${expr "" v}
|
|
|
|
|
</plist>'';
|
2018-06-27 19:35:07 +00:00
|
|
|
|
|
2021-03-24 10:20:36 +00:00
|
|
|
|
/* Translate a simple Nix expression to Dhall notation.
|
|
|
|
|
* Note that integers are translated to Integer and never
|
|
|
|
|
* the Natural type.
|
|
|
|
|
*/
|
|
|
|
|
toDhall = { }@args: v:
|
|
|
|
|
with builtins;
|
|
|
|
|
let concatItems = lib.strings.concatStringsSep ", ";
|
|
|
|
|
in if isAttrs v then
|
|
|
|
|
"{ ${
|
|
|
|
|
concatItems (lib.attrsets.mapAttrsToList
|
|
|
|
|
(key: value: "${key} = ${toDhall args value}") v)
|
|
|
|
|
} }"
|
|
|
|
|
else if isList v then
|
|
|
|
|
"[ ${concatItems (map (toDhall args) v)} ]"
|
|
|
|
|
else if isInt v then
|
|
|
|
|
"${if v < 0 then "" else "+"}${toString v}"
|
|
|
|
|
else if isBool v then
|
|
|
|
|
(if v then "True" else "False")
|
|
|
|
|
else if isFunction v then
|
|
|
|
|
abort "generators.toDhall: cannot convert a function to Dhall"
|
|
|
|
|
else if isNull v then
|
|
|
|
|
abort "generators.toDhall: cannot convert a null to Dhall"
|
|
|
|
|
else
|
|
|
|
|
builtins.toJSON v;
|
2016-11-06 00:51:13 +00:00
|
|
|
|
}
|