aboutsummaryrefslogtreecommitdiff
path: root/modules/service-router.module.nix
blob: e1da4fba83b5a7c108fea04d65889ac298f58ad6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{ config, lib, ... }:
with lib;
let
  cfg = config.services.service-router;
  hostname = name: "${name}.local";
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"; };
      }; });
      default = {};
    };
  };

  config = lib.mkIf cfg.enable {
    services.nginx = {
      enable = true;
      virtualHosts = lib.mapAttrs' (name: val: {
        name = hostname name;
        value = {
          locations."/" = {
            proxyPass = "${val.protocol}://${val.host}:${toString val.port}";
          };
        };
      }) cfg.routes;
    };

    networking.hosts."127.0.0.1" = lib.mapAttrsToList (name: _: hostname name) cfg.routes;
  };
}