Files
nixpkgs/modules/nixos/ports.nix

125 lines
3.0 KiB
Nix
Raw Normal View History

2025-10-05 09:19:29 +02:00
# NixOS Module for Port Management
#
# This module provides centralized port management across your NixOS systems.
# Define ports once and use them consistently across all services, with
# support for host-specific overrides.
#
# Usage in your NixOS configuration:
#
# # In your flake.nix or configuration.nix:
# imports = [ inputs.m3ta-nixpkgs.nixosModules.default ];
#
# m3ta.ports = {
# enable = true;
#
# # Define your default ports
# definitions = {
# nginx = 80;
# grafana = 3000;
# prometheus = 9090;
# homepage = 8080;
# ssh = 22;
# };
#
# # Define host-specific overrides
# hostOverrides = {
# laptop = {
# nginx = 8080; # Use non-privileged port on laptop
# ssh = 2222;
# };
# server = {
# homepage = 3001;
# };
# };
#
# # Optionally set the current hostname for automatic port resolution
# currentHost = config.networking.hostName;
# };
#
# # Use ports in your configuration:
# services.nginx.defaultHTTPListenPort = config.m3ta.ports.get "nginx";
# services.grafana.settings.server.http_port = config.m3ta.ports.get "grafana";
#
# # Or access all ports for the current host:
# environment.etc."my-ports.json".text = builtins.toJSON config.m3ta.ports.all;
{
config,
lib,
...
}:
with lib; let
cfg = config.m3ta.ports;
portsLib = import ../../lib/ports.nix {inherit lib;};
portHelpers =
if cfg.enable
then
portsLib.mkPortHelpers {
ports = cfg.definitions;
hostPorts = cfg.hostOverrides;
}
else null;
in {
options.m3ta.ports = {
enable = mkEnableOption "centralized port management";
definitions = mkOption {
type = types.attrsOf types.port;
default = {};
2025-10-05 12:37:57 +02:00
description = "Default port definitions for services.";
2025-10-05 09:19:29 +02:00
};
hostOverrides = mkOption {
type = types.attrsOf (types.attrsOf types.port);
default = {};
2025-10-05 12:37:57 +02:00
description = "Host-specific port overrides.";
2025-10-05 09:19:29 +02:00
};
currentHost = mkOption {
2025-10-05 12:37:57 +02:00
type = types.str;
2025-10-05 09:19:29 +02:00
default = config.networking.hostName;
2025-10-05 12:37:57 +02:00
description = "Hostname to use for port resolution.";
2025-10-05 09:19:29 +02:00
};
2025-10-05 12:37:57 +02:00
# Internal computed options
2025-10-05 09:19:29 +02:00
get = mkOption {
2025-10-05 12:37:57 +02:00
type = types.raw;
2025-10-05 09:19:29 +02:00
readOnly = true;
internal = true;
};
getForHost = mkOption {
2025-10-05 12:37:57 +02:00
type = types.raw;
2025-10-05 09:19:29 +02:00
readOnly = true;
internal = true;
};
all = mkOption {
type = types.attrsOf types.port;
readOnly = true;
internal = true;
};
allForHost = mkOption {
2025-10-05 12:37:57 +02:00
type = types.raw;
2025-10-05 09:19:29 +02:00
readOnly = true;
internal = true;
};
services = mkOption {
type = types.listOf types.str;
readOnly = true;
internal = true;
};
};
config = mkIf cfg.enable {
2025-10-05 12:37:57 +02:00
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;
2025-10-05 09:19:29 +02:00
};
}