git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #32212 from ryantrinkle/nat-port-forwarding-ranges

Nat port forwarding ranges

commit3807408c383b57805d27eb8293bd19cac591291a authorzimbatm <zimbatm@zimbatm.com> dateMon 4 Dec 2017, 12:05 (9 years ago) parent852fe55a8f1ebf
4 files changed+24−3
lib/strings.nix+8 lib/types.nix+7 nixos/doc/manual/development/option-types.xml+6 nixos/modules/services/networking/nat.nix+3−3
lib/strings.nix+8
@@ -219,6 +219,14 @@ rec {
219219 */
220220 escapeShellArgs = concatMapStringsSep " " escapeShellArg;
221221
222+ /* Turn a string into a Nix expression representing that string
223+
224+ Example:
225+ escapeNixString "hello\${}\n"
226+ => "\"hello\\\${}\\n\""
227+ */
228+ escapeNixString = s: escape ["$"] (builtins.toJSON s);
229+
222230 /* Obsolete - use replaceStrings instead. */
223231 replaceChars = builtins.replaceStrings or (
224232 del: new: s:
lib/types.nix+7
@@ -174,6 +174,13 @@ rec {
174174 merge = mergeOneOption;
175175 };
176176
177+ strMatching = pattern: mkOptionType {
178+ name = "strMatching ${escapeNixString pattern}";
179+ description = "string matching the pattern ${pattern}";
180+ check = x: str.check x && builtins.match pattern x != null;
181+ inherit (str) merge;
182+ };
183+
177184 # Merge multiple definitions by concatenating them (with the given
178185 # separator between the values).
179186 separatedString = sep: mkOptionType rec {
nixos/doc/manual/development/option-types.xml+6
@@ -110,6 +110,12 @@
110110 <listitem><para>A string. Multiple definitions are concatenated with a
111111 collon <literal>":"</literal>.</para></listitem>
112112 </varlistentry>
113+ <varlistentry>
114+ <term><varname>types.strMatching</varname></term>
115+ <listitem><para>A string matching a specific regular expression. Multiple
116+ definitions cannot be merged. The regular expression is processed using
117+ <literal>builtins.match</literal>.</para></listitem>
118+ </varlistentry>
113119</variablelist>
114120
115121 </section>
nixos/modules/services/networking/nat.nix+3−3
@@ -131,15 +131,15 @@ in
131131 type = with types; listOf (submodule {
132132 options = {
133133 sourcePort = mkOption {
134− type = types.int;
134+ type = types.either types.int (types.strMatching "[[:digit:]]+:[[:digit:]]+");
135135 example = 8080;
136− description = "Source port of the external interface";
136+ description = "Source port of the external interface; to specify a port range, use a string with a colon (e.g. \"60000:61000\")";
137137 };
138138
139139 destination = mkOption {
140140 type = types.str;
141141 example = "10.0.0.1:80";
142− description = "Forward connection to destination ip:port";
142+ description = "Forward connection to destination ip:port; to specify a port range, use ip:start-end";
143143 };
144144
145145 proto = mkOption {