125 lines
3.0 KiB
Nix
125 lines
3.0 KiB
Nix
# 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 = {};
|
|
description = "Default port definitions for services.";
|
|
};
|
|
|
|
hostOverrides = mkOption {
|
|
type = types.attrsOf (types.attrsOf types.port);
|
|
default = {};
|
|
description = "Host-specific port overrides.";
|
|
};
|
|
|
|
currentHost = mkOption {
|
|
type = types.str;
|
|
default = config.networking.hostName;
|
|
description = "Hostname to use for port resolution.";
|
|
};
|
|
|
|
# Internal computed options
|
|
get = mkOption {
|
|
type = types.raw;
|
|
readOnly = true;
|
|
internal = true;
|
|
};
|
|
|
|
getForHost = mkOption {
|
|
type = types.raw;
|
|
readOnly = true;
|
|
internal = true;
|
|
};
|
|
|
|
all = mkOption {
|
|
type = types.attrsOf types.port;
|
|
readOnly = true;
|
|
internal = true;
|
|
};
|
|
|
|
allForHost = mkOption {
|
|
type = types.raw;
|
|
readOnly = true;
|
|
internal = true;
|
|
};
|
|
|
|
services = mkOption {
|
|
type = types.listOf types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
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;
|
|
};
|
|
}
|