port module simplified

This commit is contained in:
m3tam3re
2025-10-05 12:37:57 +02:00
parent 62a85af9bc
commit 735c22cf36
4 changed files with 42 additions and 421 deletions

View File

@@ -45,16 +45,13 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.m3ta.ports;
# Import the ports library
portsLib = import ../../lib/ports.nix {inherit lib;};
# Create port helpers from the configuration
portHelpers =
if cfg.enable
then
@@ -70,150 +67,58 @@ in {
definitions = mkOption {
type = types.attrsOf types.port;
default = {};
example = literalExpression ''
{
nginx = 80;
grafana = 3000;
prometheus = 9090;
ssh = 22;
}
'';
description = ''
Default port definitions for services.
These ports will be used unless overridden by host-specific settings.
'';
description = "Default port definitions for services.";
};
hostOverrides = mkOption {
type = types.attrsOf (types.attrsOf types.port);
default = {};
example = literalExpression ''
{
laptop = {
nginx = 8080;
ssh = 2222;
};
server = {
nginx = 443;
};
}
'';
description = ''
Host-specific port overrides.
Keys are hostnames, values are attribute sets of service-name to port mappings.
'';
description = "Host-specific port overrides.";
};
currentHost = mkOption {
type = types.nullOr types.str;
type = types.str;
default = config.networking.hostName;
defaultText = literalExpression "config.networking.hostName";
example = "laptop";
description = ''
The current hostname to use for port resolution.
Defaults to the system's hostname.
Set to null to disable host-specific overrides.
'';
description = "Hostname to use for port resolution.";
};
# Computed option - provides access to port helpers
# Internal computed options
get = mkOption {
type = types.functionTo types.port;
type = types.raw;
readOnly = true;
internal = true;
description = ''
Function to get a port for a service.
Automatically uses the current host for overrides.
'';
};
getForHost = mkOption {
type = types.functionTo (types.functionTo types.port);
type = types.raw;
readOnly = true;
internal = true;
description = ''
Function to get a port for a service on a specific host.
Usage: config.m3ta.ports.getForHost "hostname" "service"
'';
};
all = mkOption {
type = types.attrsOf types.port;
readOnly = true;
internal = true;
description = ''
All ports for the current host (defaults merged with overrides).
'';
};
allForHost = mkOption {
type = types.functionTo (types.attrsOf types.port);
type = types.raw;
readOnly = true;
internal = true;
description = ''
Function to get all ports for a specific host.
Usage: config.m3ta.ports.allForHost "hostname"
'';
};
services = mkOption {
type = types.listOf types.str;
readOnly = true;
internal = true;
description = ''
List of all defined service names.
'';
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = cfg.definitions != {};
message = "m3ta.ports.definitions must not be empty when m3ta.ports.enable is true";
}
];
m3ta.ports = {
# Function to get port for current host
get = service:
assert assertMsg (portHelpers != null) "Port helpers not initialized";
assert assertMsg (cfg.definitions ? ${service}) "Service '${service}' not defined in m3ta.ports.definitions";
portHelpers.getPort service cfg.currentHost;
# Function to get port for specific host
getForHost = host: service:
assert assertMsg (portHelpers != null) "Port helpers not initialized";
assert assertMsg (cfg.definitions ? ${service}) "Service '${service}' not defined in m3ta.ports.definitions";
portHelpers.getPort service host;
# All ports for current host
all =
if portHelpers != null
then portHelpers.getHostPorts cfg.currentHost
else {};
# Function to get all ports for specific host
allForHost = host:
assert assertMsg (portHelpers != null) "Port helpers not initialized";
portHelpers.getHostPorts host;
# List all services
services =
if portHelpers != null
then portHelpers.listServices
else [];
};
# Optional: Create a JSON file with all ports for easy inspection
environment.etc."m3ta/ports.json" = mkIf cfg.enable {
text = builtins.toJSON {
hostname = cfg.currentHost;
ports = cfg.all;
allDefinitions = cfg.definitions;
hostOverrides = cfg.hostOverrides;
};
mode = "0444";
};
m3ta.ports.get = service: portHelpers.getPort service cfg.currentHost;
m3ta.ports.getForHost = host: service: portHelpers.getPort service host;
m3ta.ports.all = portHelpers.getHostPorts cfg.currentHost;
m3ta.ports.allForHost = portHelpers.getHostPorts;
m3ta.ports.services = portHelpers.listServices;
};
}