1 # In the following context a parameter is an attribute set that
2 # contains a NixOS option and a render function. It also contains the
3 # attribute: '_type = "param"' so we can distinguish it from other
6 # The render function is used to convert the value of the option to a
7 # snippet of strongswan.conf. Most parameters simply render their
8 # value to a string. For example, take the following parameter:
10 # threads = mkIntParam 10 "Threads to use for request handling.";
12 # When a users defines the corresponding option as for example:
14 # services.strongswan-swanctl.strongswan.threads = 32;
16 # It will get rendered to the following snippet in strongswan.conf:
20 # Some parameters however need to be able to change the attribute
21 # name. For example, take the following parameter:
23 # id = mkPrefixedAttrsOfParam (mkOptionalStrParam "") "...";
25 # A user can define the corresponding option as for example:
32 # This will get rendered to the following snippet:
37 # For this reason the render function is not simply a function from
38 # value -> string but a function from a value to an attribute set:
39 # { "${name}" = string }. This allows parameters to change the attribute
40 # name like in the previous example.
45 with (import ./param-lib.nix lib);
48 mkParamOfType = type: strongswanDefault: description: {
51 type = types.nullOr type;
53 description = documentDefault description strongswanDefault;
55 render = single toString;
59 description: strongswanDefault:
60 if strongswanDefault == null then
68 StrongSwan default: ````${builtins.toJSON strongswanDefault}````
72 single = f: name: value: { ${name} = f value; };
74 mkStrParam = mkParamOfType types.str;
75 mkOptionalStrParam = mkStrParam null;
77 mkEnumParam = values: mkParamOfType (types.enum values);
79 mkIntParam = mkParamOfType types.int;
80 mkOptionalIntParam = mkIntParam null;
82 # We should have floats in Nix...
83 mkFloatParam = mkStrParam;
85 # TODO: Check for hex format:
86 mkHexParam = mkStrParam;
87 mkOptionalHexParam = mkOptionalStrParam;
89 # TODO: Check for duration format:
90 mkDurationParam = mkStrParam;
91 mkOptionalDurationParam = mkOptionalStrParam;
93 mkYesNoParam = strongswanDefault: description: {
96 type = types.nullOr types.bool;
98 description = documentDefault description strongswanDefault;
100 render = single (b: if b then "yes" else "no");
105 mkSpaceSepListParam = mkSepListParam " ";
106 mkCommaSepListParam = mkSepListParam ",";
108 mkSepListParam = sep: strongswanDefault: description: {
111 type = types.nullOr (types.listOf types.str);
113 description = documentDefault description strongswanDefault;
115 render = single (value: concatStringsSep sep value);
118 mkAttrsOfParams = params: mkAttrsOf params (types.submodule { options = paramsToOptions params; });
120 mkAttrsOfParam = param: mkAttrsOf param param.option.type;
122 mkAttrsOf = param: option: description: {
125 type = types.attrsOf option;
127 description = description;
129 render = single (attrs: (paramsToRenderedStrings attrs (mapAttrs (_n: _v: param) attrs)));
132 mkPrefixedAttrsOfParams =
133 params: mkPrefixedAttrsOf params (types.submodule { options = paramsToOptions params; });
135 mkPrefixedAttrsOfParam = param: mkPrefixedAttrsOf param param.option.type;
137 mkPrefixedAttrsOf = p: option: description: {
140 type = types.attrsOf option;
142 description = description;
147 prefixedAttrs = mapAttrs' (name: nameValuePair "${prefix}-${name}") attrs;
149 paramsToRenderedStrings prefixedAttrs (mapAttrs (_n: _v: p) prefixedAttrs);
152 mkPostfixedAttrsOfParams = params: description: {
155 type = types.attrsOf (types.submodule { options = paramsToOptions params; });
157 description = description;
162 postfixedAttrs = mapAttrs' (name: nameValuePair "${name}-${postfix}") attrs;
164 paramsToRenderedStrings postfixedAttrs (mapAttrs (_n: _v: params) postfixedAttrs);