6 paramsToConf = cfg: ps: mkConf 0 (paramsToRenderedStrings cfg ps);
8 # mkConf takes an indentation level (which usually starts at 0) and a nested
9 # attribute set of strings and will render that set to a strongswan.conf style
10 # configuration format. For example:
12 # mkConf 0 {a = "1"; b = { c = { "foo" = "2"; "bar" = "3"; }; d = "4";};} => ''
23 concatMapStringsSep "\n" (
27 indentation = replicate indent " ";
32 "${name} {\n" + mkConf (indent + 2) value + "\n" + indentation + "}"
38 replicate = n: c: concatStrings (builtins.genList (_x: c) n);
40 # `paramsToRenderedStrings cfg ps` converts the NixOS configuration `cfg`
41 # (typically the "config" argument of a NixOS module) and the set of
42 # parameters `ps` (an attribute set where the values are constructed using the
43 # parameter constructors in ./param-constructors.nix) to a nested attribute
44 # set of strings (rendered parameters).
45 paramsToRenderedStrings =
51 value = attrByPath path null cfg;
53 optionalAttrs (value != null) (param.render name value)
59 filterAttrs (n: v: (v != null)) (
64 value' = filterEmptySets value;
66 if value' == { } then null else value'
72 # Recursively map over every parameter in the given attribute set.
73 mapParamsRecursive = mapAttrsRecursiveCond' (as: (!(as ? _type && as._type == "param")));
75 mapAttrsRecursiveCond' =
83 if isAttrs value && cond value then
84 { ${name} = recurse (path ++ [ name ]) value; }
86 f (path ++ [ name ]) name value;
92 mapAttrs'' = f: set: foldl' (a: b: a // b) { } (map (attr: f attr set.${attr}) (attrNames set));
94 # Extract the options from the given set of parameters.
95 paramsToOptions = ps: mapParamsRecursive (_path: name: param: { ${name} = param.option; }) ps;