From 911f7269ac583284f413672b83867cf401f21e5d Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 18 Jun 2022 01:57:06 +0200 Subject: [PATCH] modules/port-magic: full module rework --- modules/port-magic/default.nix | 55 +++--------------------- modules/port-magic/link.nix | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 modules/port-magic/link.nix diff --git a/modules/port-magic/default.nix b/modules/port-magic/default.nix index 8a0d963..f867d5d 100644 --- a/modules/port-magic/default.nix +++ b/modules/port-magic/default.nix @@ -1,56 +1,11 @@ { config, lib, ... }: -with builtins; with lib; -let - cfg = config.reservedPorts; - - portNames = config.reservePortsFor; - - portHash = flip pipe [ - (hashString "md5") - (substring 0 7) - (hash: (fromTOML "v=0x${hash}").v) - (flip mod cfg.amount) - (add cfg.start) - ]; - - ports = genAttrs portNames portHash; - - portsEnd = cfg.start + cfg.amount; -in { - options = { - reservedPorts = { - amount = mkOption { - type = types.int; - default = 10000; - description = "Amount of ports to reserve at most."; - }; - start = mkOption { - type = types.int; - default = 30000; - description = "Starting point for reserved ports."; - }; - }; - reservePortsFor = mkOption { - type = types.listOf types.str; - default = []; - description = "List of application names for which to automatically reserve ports."; - }; - ports = mkOption { - type = types.attrsOf (types.ints.between cfg.start portsEnd); - default = {}; - description = "Named network ports."; - }; - portsStr = mkOption { - readOnly = true; - type = types.attrsOf types.str; - description = "Named network ports, as strings."; - }; - }; - config = lib.mkIf (config.reservePortsFor != []) { - inherit ports; - portsStr = mapAttrs (_: toString) ports; +{ + options.links = mkOption { + type = types.attrsOf (types.submodule ./link.nix); + description = "Port Magic links."; + default = {}; }; } diff --git a/modules/port-magic/link.nix b/modules/port-magic/link.nix new file mode 100644 index 0000000..43ca9bc --- /dev/null +++ b/modules/port-magic/link.nix @@ -0,0 +1,76 @@ +{ config, lib, name, ... }: + +with builtins; +with lib; + +let + cfg = config; + + portHash = flip pipe [ + (hashString "md5") + (substring 0 7) + (hash: (fromTOML "v=0x${hash}").v) + (flip mod cfg.reservedPorts.amount) + (add cfg.reservedPorts.start) + ]; +in + +{ + options = { + ipv4 = mkOption { + type = types.str; + default = "127.0.0.1"; + description = "The IPv4 address."; + }; + hostname = mkOption { + type = types.str; + description = "The hostname."; + }; + + port = mkOption { + type = types.int; + description = "The TCP or UDP port."; + }; + portStr = mkOption { + type = types.str; + description = "The TCP or UDP port, as a string."; + }; + reservedPorts = { + amount = mkOption { + type = types.int; + default = 10000; + description = "Amount of ports to reserve at most."; + }; + start = mkOption { + type = types.int; + default = 30000; + description = "Starting point for reserved ports."; + }; + }; + + protocol = mkOption { + type = types.str; + description = "The protocol in URL scheme name format."; + }; + path = mkOption { + type = types.nullOr types.path; + default = null; + description = "The resource path."; + }; + url = mkOption { + type = types.str; + description = "The URL."; + }; + tuple = mkOption { + type = types.str; + description = "The hostname:port tuple."; + }; + }; + config = mkIf true { + hostname = mkDefault cfg.ipv4; + port = mkDefault (portHash name); + portStr = toString cfg.port; + tuple = "${cfg.hostname}:${cfg.portStr}"; + url = "${cfg.protocol}://${cfg.hostname}:${cfg.portStr}${if cfg.path == null then "" else cfg.path}"; + }; +}