mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-03-14 18:18:59 +00:00
nixos/state: add ports sub-option
This commit is contained in:
parent
83d78c0cb8
commit
917cd7e01d
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
imports = [
|
imports = [
|
||||||
./extra-arguments.nix
|
./extra-arguments.nix
|
||||||
./state.nix
|
./state
|
||||||
./suites/archiving.nix
|
./suites/archiving.nix
|
||||||
./suites/browsers.nix
|
./suites/browsers.nix
|
||||||
./suites/desktop.nix
|
./suites/desktop.nix
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
{ lib, ... }:
|
{ lib, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
|
imports = [
|
||||||
|
./ports.nix
|
||||||
|
];
|
||||||
|
|
||||||
options.state = lib.mkOption {
|
options.state = lib.mkOption {
|
||||||
type = lib.types.submodule {
|
type = lib.types.submodule {
|
||||||
freeFormType = with lib.types; attrsOf anything;
|
freeformType = with lib.types; attrsOf anything;
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
description = ''
|
description = ''
|
108
modules/nixos/_private/state/ports.nix
Normal file
108
modules/nixos/_private/state/ports.nix
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
let
|
||||||
|
cfg = config.state;
|
||||||
|
|
||||||
|
supportedProtocols = [ "tcp" "udp" ];
|
||||||
|
|
||||||
|
portRangeType = {
|
||||||
|
options = {
|
||||||
|
from = lib.mkOption {
|
||||||
|
type = lib.types.port;
|
||||||
|
description = ''
|
||||||
|
The start of the range of TCP/UDP ports to be taken over.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
to = lib.mkOption {
|
||||||
|
type = lib.types.port;
|
||||||
|
description = ''
|
||||||
|
The end of the range of TCP/UDP ports to be taken over.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
portModule = { lib, ... }: {
|
||||||
|
options = {
|
||||||
|
protocols = lib.mkOption {
|
||||||
|
type = with lib.types; listOf (enum supportedProtocols);
|
||||||
|
description = ''
|
||||||
|
Indicates the type of protocol of the service.
|
||||||
|
'';
|
||||||
|
default = [ "tcp" "udp" ];
|
||||||
|
example = [ "tcp" ];
|
||||||
|
};
|
||||||
|
|
||||||
|
value = lib.mkOption {
|
||||||
|
type = with lib.types; either port (submodule portRangeType);
|
||||||
|
description = ''
|
||||||
|
The port number itself.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
openFirewall = lib.mkEnableOption "opening the ports to firewall";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options.state =
|
||||||
|
let
|
||||||
|
portsModule = { lib, ... }: {
|
||||||
|
options = {
|
||||||
|
ports = lib.mkOption {
|
||||||
|
type = with lib.types; attrsOf (submodule portModule);
|
||||||
|
description = ''
|
||||||
|
A set of ports indicating what goes where in the NixOS system.
|
||||||
|
'';
|
||||||
|
example = lib.literalExpression ''
|
||||||
|
rec {
|
||||||
|
gonic = {
|
||||||
|
value = 5757;
|
||||||
|
protocols = [ "tcp" ];
|
||||||
|
openFirewall = true;
|
||||||
|
};
|
||||||
|
uxplay = {
|
||||||
|
value = 7864;
|
||||||
|
openFirewall = true;
|
||||||
|
};
|
||||||
|
uxplayClients.value = {
|
||||||
|
from = uxplay.value + 1;
|
||||||
|
to = uxplay.value + 20;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in lib.mkOption {
|
||||||
|
type = lib.types.submodule portsModule;
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf (cfg.ports != { }) {
|
||||||
|
networking.firewall =
|
||||||
|
let
|
||||||
|
allPortsToBeOpened = lib.filterAttrs (_: v: v.openFirewall) cfg.ports;
|
||||||
|
hasProtocol = protocol: v: lib.elem protocol v.protocols;
|
||||||
|
mkFirewallEntry = protocol: v:
|
||||||
|
let
|
||||||
|
inherit (v) value;
|
||||||
|
in
|
||||||
|
if lib.isAttrs value then {
|
||||||
|
${if protocol == "tcp"
|
||||||
|
then "allowedTCPPortRanges"
|
||||||
|
else "allowedUDPPortRanges"} = [ value ];
|
||||||
|
} else {
|
||||||
|
${if protocol == "tcp"
|
||||||
|
then "allowedTCPPorts"
|
||||||
|
else "allowedUDPPorts"} = [ value ];
|
||||||
|
};
|
||||||
|
|
||||||
|
mkFirewallEntryModule = _: v:
|
||||||
|
lib.optionalAttrs (hasProtocol "udp" v) (mkFirewallEntry "udp" v)
|
||||||
|
// lib.optionalAttrs (hasProtocol "tcp" v) (mkFirewallEntry "tcp" v);
|
||||||
|
in
|
||||||
|
lib.mkMerge
|
||||||
|
(lib.mapAttrsToList mkFirewallEntryModule allPortsToBeOpened);
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user