Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nat port forwarding ranges #32212

Merged
merged 3 commits into from Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/strings.nix
Expand Up @@ -219,6 +219,14 @@ rec {
*/
escapeShellArgs = concatMapStringsSep " " escapeShellArg;

/* Turn a string into a Nix expression representing that string

Example:
escapeNixString "hello\${}\n"
=> "\"hello\\\${}\\n\""
*/
escapeNixString = s: escape ["$"] (builtins.toJSON s);

/* Obsolete - use replaceStrings instead. */
replaceChars = builtins.replaceStrings or (
del: new: s:
Expand Down
7 changes: 7 additions & 0 deletions lib/types.nix
Expand Up @@ -174,6 +174,13 @@ rec {
merge = mergeOneOption;
};

strMatching = pattern: mkOptionType {
name = "strMatching ${escapeNixString pattern}";
description = "string matching the pattern ${pattern}";
check = x: str.check x && builtins.match pattern x != null;
inherit (str) merge;
};

# Merge multiple definitions by concatenating them (with the given
# separator between the values).
separatedString = sep: mkOptionType rec {
Expand Down
6 changes: 6 additions & 0 deletions nixos/doc/manual/development/option-types.xml
Expand Up @@ -110,6 +110,12 @@
<listitem><para>A string. Multiple definitions are concatenated with a
collon <literal>":"</literal>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>types.strMatching</varname></term>
<listitem><para>A string matching a specific regular expression. Multiple
definitions cannot be merged. The regular expression is processed using
<literal>builtins.match</literal>.</para></listitem>
</varlistentry>
</variablelist>

</section>
Expand Down
6 changes: 3 additions & 3 deletions nixos/modules/services/networking/nat.nix
Expand Up @@ -125,15 +125,15 @@ in
type = with types; listOf (submodule {
options = {
sourcePort = mkOption {
type = types.int;
type = types.either types.int (types.strMatching "[[:digit:]]+:[[:digit:]]+");
example = 8080;
description = "Source port of the external interface";
description = "Source port of the external interface; to specify a port range, use a string with a colon (e.g. \"60000:61000\")";
Copy link
Member

@edolstra edolstra Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in favor of this. It's better to add an option sourcePortRange (e.g. sourcePortRange = { start = 123; end = 456; }). I mean, why specify something as a string that needs to be parsed when you can use a structured data type that doesn't need parsing?

};

destination = mkOption {
type = types.str;
example = "10.0.0.1:80";
description = "Forward connection to destination ip:port";
description = "Forward connection to destination ip:port; to specify a port range, use ip:start-end";
};

proto = mkOption {
Expand Down