diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-06-14 16:40:48 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-06-14 16:40:48 +0530 |
| commit | 42df69164dbc74e5ddb54f5a7a01fd66260fef57 (patch) | |
| tree | bc8b19c39df9edf16ca6827005b3d5e1ef564764 /services/service-router.service.nix | |
| parent | 58519ea7b12e8ad58c0f97d9636d30572a789b41 (diff) | |
| download | homeserver-nixos-config-42df69164dbc74e5ddb54f5a7a01fd66260fef57.tar.gz homeserver-nixos-config-42df69164dbc74e5ddb54f5a7a01fd66260fef57.zip | |
Switch to coredns + refactor dns hosts mapping into service + more refactor
Diffstat (limited to 'services/service-router.service.nix')
| -rw-r--r-- | services/service-router.service.nix | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/services/service-router.service.nix b/services/service-router.service.nix new file mode 100644 index 0000000..e52280f --- /dev/null +++ b/services/service-router.service.nix @@ -0,0 +1,47 @@ +{ config, lib, ... }: +with lib; +let + cfg = config.services.service-router; +in { + imports = [ + ./bacchus-dns.service.nix + ]; + + 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"; }; + basePath = mkOption { type = types.str; default = ""; }; + extraNginxOptions = mkOption { type = types.attrs; default = {}; }; + }; }); + default = {}; + }; + }; + + config = lib.mkIf cfg.enable { + services.nginx = { + enable = true; + recommendedOptimisation = true; + virtualHosts = lib.mapAttrs (_: val: + let + opts = if hasAttr "extraNginxOptions" val then val.extraNginxOptions else {}; + in { + locations."/" = { + proxyPass = + "${val.protocol}://${val.host}:${toString val.port}${val.basePath}"; + proxyWebsockets = true; + } // opts; + } + ) cfg.routes; + }; + + # Hostname mapping + services.bacchus-dns = { + enable = true; + hosts = mapAttrs (_: val: val.host) cfg.routes; + }; + }; +} |
