diff --git a/modules/flake/checks/port-collector.nix b/modules/flake/checks/port-collector.nix index 31cebf41..eb829151 100644 --- a/modules/flake/checks/port-collector.nix +++ b/modules/flake/checks/port-collector.nix @@ -1,4 +1,6 @@ -# collect all the ports even if they are disabled, we want to avoid any conflicts +# collect every port a garden service declares on every nixos host, even if +# the service is disabled there, so a port can be moved between hosts freely. +# a service's port is its `port` option plus any option ending in `Port` { lib, @@ -6,20 +8,48 @@ self, }: let - inherit (lib.attrsets) attrNames; - inherit (lib.lists) filter length groupBy; - inherit (lib.strings) concatMapStringsSep concatStringsSep; + inherit (lib.attrsets) + attrNames + attrValues + filterAttrs + mapAttrsToList + ; + inherit (lib.lists) + concatLists + concatMap + filter + groupBy + length + ; + inherit (lib.strings) concatStringsSep hasSuffix; - services = self.nixosConfigurations.amaterasu.config.garden.services; + isPort = name: value: (name == "port" || hasSuffix "Port" name) && builtins.isInt value; - # build {"3000" = ["pds" "kittr"]; "0" = [ ... ]; ...} in a single pass. - portsToServices = groupBy (name: toString (services.${name}.port or 0)) (attrNames services); + # [ { port = "3000"; owner = "pds.port"; } ... ] + portsOf = + services: + concatMap ( + service: + mapAttrsToList (option: port: { + port = toString port; + owner = "${service}.${option}"; + }) (filterAttrs isPort services.${service}) + ) (attrNames services); - # "0" is the catch all for services that don't bind a port; we will drop it - # before checking for collisions. - collisions = filter (srvList: length srvList > 1) ( - builtins.attrValues (removeAttrs portsToServices [ "0" ]) - ); + collisionsOn = + host: nixos: + map (owners: "${host}: ${concatStringsSep " and " owners}") ( + filter (owners: length owners > 1) ( + attrValues ( + builtins.mapAttrs (_: map (p: p.owner)) (groupBy (p: p.port) (portsOf nixos.config.garden.services)) + ) + ) + ); + + # the installer doesn't import the garden modules + hosts = filterAttrs (_: nixos: nixos.config ? garden) self.nixosConfigurations; + + collisions = concatLists (mapAttrsToList collisionsOn hosts); in runCommandLocal "port-collector" { @@ -27,9 +57,7 @@ runCommandLocal "port-collector" } '' if [ ${toString (collisions != [ ])} ]; then - echo "Port collisions detected between services ${ - concatMapStringsSep ", " (s: concatStringsSep " and " s) collisions - }" + echo "Port collisions detected between ${concatStringsSep ", " collisions}" exit 1 fi diff --git a/modules/nixos/services/forgejo.nix b/modules/nixos/services/forgejo.nix index db015d75..621403fe 100644 --- a/modules/nixos/services/forgejo.nix +++ b/modules/nixos/services/forgejo.nix @@ -112,7 +112,11 @@ in cache = { ENABLED = true; ADAPTER = "redis"; - HOST = "redis://:forgejo@localhost:6371"; + HOST = + let + inherit (config.garden.services) redis; + in + "redis://:forgejo@${redis.host}:${toString redis.port}"; }; oauth2_client = { diff --git a/modules/nixos/services/immich.nix b/modules/nixos/services/immich.nix index 8f9b01f4..ac42ff76 100644 --- a/modules/nixos/services/immich.nix +++ b/modules/nixos/services/immich.nix @@ -10,13 +10,23 @@ let rdomain = config.networking.domain; inherit (lib.modules) mkIf; + inherit (lib.options) mkOption; inherit (self.lib) mkServiceOption mkSecret; in { - options.garden.services.immich = mkServiceOption "immich" { - port = 3007; - domain = "photos.${rdomain}"; - }; + options.garden.services.immich = + mkServiceOption "immich" { + port = 3007; + domain = "photos.${rdomain}"; + } + // { + machineLearningPort = mkOption { + type = lib.types.port; + default = 3003; + readOnly = true; + description = "The port immich's machine learning listens on, which nixpkgs fixes"; + }; + }; config = mkIf cfg.enable { networking.firewall.allowedTCPPorts = [ cfg.port ]; @@ -187,7 +197,7 @@ in modelName = "PP-OCRv6_medium"; }; - urls = [ "http://127.0.0.1:3003" ]; + urls = [ "http://127.0.0.1:${toString cfg.machineLearningPort}" ]; }; map = { diff --git a/modules/nixos/services/kanidm.nix b/modules/nixos/services/kanidm.nix index 6d27efab..cdb87d97 100644 --- a/modules/nixos/services/kanidm.nix +++ b/modules/nixos/services/kanidm.nix @@ -10,6 +10,7 @@ }: let inherit (lib.modules) mkIf; + inherit (lib.options) mkOption; inherit (self.lib) mkServiceOption mkSecret; rdomain = config.networking.domain; @@ -20,10 +21,18 @@ let cfg' = config.garden.services; in { - options.garden.services.kanidm = mkServiceOption "kanidm" { - port = 3010; - domain = "sso.${rdomain}"; - }; + options.garden.services.kanidm = + mkServiceOption "kanidm" { + port = 3010; + domain = "sso.${rdomain}"; + } + // { + ldapPort = mkOption { + type = lib.types.port; + default = 3636; + description = "The port for kanidm's ldap interface"; + }; + }; config = mkIf cfg.enable { garden.services = { @@ -88,7 +97,7 @@ in inherit (cfg) domain; origin = "https://${cfg.domain}"; bindaddress = "${cfg.host}:${toString cfg.port}"; - ldapbindaddress = "${cfg.host}:3636"; + ldapbindaddress = "${cfg.host}:${toString cfg.ldapPort}"; tls_chain = "${certDir}/fullchain.pem"; tls_key = "${certDir}/key.pem"; }; diff --git a/modules/nixos/services/qbittorent.nix b/modules/nixos/services/qbittorent.nix index 9e1406dd..4b856168 100644 --- a/modules/nixos/services/qbittorent.nix +++ b/modules/nixos/services/qbittorent.nix @@ -6,13 +6,28 @@ }: let inherit (lib.modules) mkIf; + inherit (lib.options) mkOption; + inherit (lib.types) port; inherit (self.lib) mkServiceOption mkSecret; cfg = config.garden.services.qbittorrent; inherit (config.garden.services) arr; in { - options.garden.services.qbittorrent = mkServiceOption "qbittorrent" { port = 3019; }; + # port is qui's, which fronts qbittorrent's own webui + options.garden.services.qbittorrent = mkServiceOption "qbittorrent" { port = 3019; } // { + webuiPort = mkOption { + type = port; + default = 4019; + description = "The port for qbittorrent's own webui, which qui talks to"; + }; + + torrentingPort = mkOption { + type = port; + default = 43125; + description = "The port peers reach qbittorrent on"; + }; + }; config = mkIf cfg.enable { # peers have to reach the torrenting port from anywhere @@ -40,8 +55,7 @@ in enable = true; group = arr.mediaGroup; - webuiPort = 4019; - torrentingPort = 43125; + inherit (cfg) webuiPort torrentingPort; serverConfig = { LegalNotice.Accepted = true; diff --git a/modules/nixos/services/redis.nix b/modules/nixos/services/redis.nix index 300e442f..803c13f3 100644 --- a/modules/nixos/services/redis.nix +++ b/modules/nixos/services/redis.nix @@ -12,7 +12,8 @@ let cfg = config.garden.services; in { - options.garden.services.redis = mkServiceOption "redis" { }; + # only forgejo's server for now, so it gets the port + options.garden.services.redis = mkServiceOption "redis" { port = 6371; }; config = mkIf cfg.redis.enable { services.redis = { @@ -23,7 +24,8 @@ in forgejo = { enable = true; user = "forgejo"; - port = 6371; + inherit (cfg.redis) port; + bind = cfg.redis.host; databases = 16; logLevel = "debug"; requirePass = "forgejo";