/ config / services / caddy.nix
caddy.nix
 1  {
 2    lib,
 3    config,
 4    ...
 5  }:
 6  
 7  let
 8    common = ''
 9      log
10      encode zstd gzip
11      tls ${config.env.DEVENV_STATE}/mkcert/_wildcard.localhost.pem ${config.env.DEVENV_STATE}/mkcert/_wildcard.localhost-key.pem
12    '';
13  in
14  {
15    certificates = [ "*.localhost" ];
16    hosts = lib.genAttrs (lib.attrNames config.services.caddy.virtualHosts) (_: "127.0.0.1");
17  
18    services.caddy = {
19      enable = true;
20      config = ''
21        {
22          log default {
23            level INFO
24            output stdout
25            format console
26          }
27        }
28      '';
29  
30      virtualHosts = lib.mkMerge [
31        (lib.mapAttrs'
32          (
33            name: upstream:
34            lib.nameValuePair "${name}.localhost" {
35              extraConfig = ''
36                ${common}
37                reverse_proxy ${toString upstream}
38              '';
39            }
40          )
41          (
42            {
43              mu = "10.0.0.218";
44            }
45            // lib.optionalAttrs config.services.prometheus.enable {
46              prometheus = ":${toString config.services.prometheus.port}";
47            }
48            // lib.optionalAttrs config.services.postgres.enable {
49              postgres = ":${toString config.services.postgres.port}";
50            }
51            // lib.optionalAttrs config.services.mailpit.enable {
52              mailpit = config.services.mailpit.uiListenAddress;
53            }
54            // lib.optionalAttrs config.services.sqld.enable {
55              sqld = ":${toString config.services.sqld.port}";
56            }
57            # // lib.optionalAttrs config.languages.rust.loco.enable {
58            #   api = "${toString config.languages.rust.loco.config.development.server.binding}:${toString config.languages.rust.loco.config.development.server.port}";
59            # }
60          )
61        )
62        {
63          "tui.localhost".extraConfig = ''
64            ${common}
65            file_server
66            root * ${config.git.root}/tui/dist
67          '';
68        }
69        {
70          "web.localhost".extraConfig = ''
71            ${common}
72            file_server
73            root * ${config.git.root}/target/dx/web/release/web/public
74          '';
75        }
76      ];
77    };
78  }