From fb3c68a676643e60e396daf4076684e12d157677 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 13 Jun 2025 15:02:58 +0530 Subject: Syncthing + service router refactor --- modules/network/default.nix | 16 +++++++++ modules/network/service-router.service.nix | 52 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 modules/network/service-router.service.nix (limited to 'modules/network') diff --git a/modules/network/default.nix b/modules/network/default.nix index ee3462c..6a40ef7 100644 --- a/modules/network/default.nix +++ b/modules/network/default.nix @@ -1,12 +1,28 @@ { lib, ... }: let settings = import ../../settings.nix { inherit lib; }; + ports = settings.network.ports; + host = settings.network.host; in { imports = [ ./ssh.nix + ./service-router.service.nix ]; + services.service-router = { + enable = true; + routes = { + "home.local" = { inherit host; port = ports.dashboard; }; + "sonarr.local" = { inherit host; port = ports.sonarr; }; + "radarr.local" = { inherit host; port = ports.radarr; }; + "prowlarr.local" = { inherit host; port = ports.prowlarr; }; + "jellyfin.local" = { inherit host; port = ports.jellyfin; }; + "syncthing.local" = { inherit host; port = ports.syncthing; }; + "lidarr.local" = { inherit host; port = ports.lidarr; }; + }; + }; + networking = { hostName = "bacchus"; diff --git a/modules/network/service-router.service.nix b/modules/network/service-router.service.nix new file mode 100644 index 0000000..9c4e6e2 --- /dev/null +++ b/modules/network/service-router.service.nix @@ -0,0 +1,52 @@ +{ config, lib, dns, ... }: +with lib; +let + cfg = config.services.service-router; + domainAZone = domain: record: { + A = [ record ]; + SOA = { + nameServer = "ns.${domain}."; + adminEmail = "dont@email.me"; + serial = 2019030800; + }; + NS = [ "ns.${domain}." ]; + }; +in { + options.services.service-router = { + enable = mkEnableOption "enable router"; + routes = mkOption { + type = types.attrsOf (types.submodule { options = { + port = mkOption { type = types.int; }; + host = mkOption { type = types.str; default = "127.0.0.1"; }; + protocol = mkOption { type = types.str; default = "http"; }; + nginx = mkOption { type = types.attrs; default = {}; }; + }; }); + default = {}; + }; + }; + + config = lib.mkIf cfg.enable { + services.nginx = { + enable = true; + recommendedOptimisation = true; + virtualHosts = lib.mapAttrs (_: val: { + locations."/" = if val.nginx == {} then { + proxyPass = "${val.protocol}://${val.host}:${toString val.port}"; + proxyWebsockets = true; + } else val.nginx; + }) cfg.routes; + }; + + services.nsd = { + enable = true; + interfaces = [ "0.0.0.0" ]; + zones = lib.mapAttrs (domain: val: { + data = dns.lib.toString domain (domainAZone domain val.host); + }) cfg.routes; + }; + networking.firewall.allowedTCPPorts = [ 53 ]; + networking.firewall.allowedUDPPorts = [ 53 ]; + + networking.hosts."127.0.0.1" = lib.mapAttrsToList (name: _: name) cfg.routes; + }; +} -- cgit v1.3.1