summaryrefslogtreecommitdiff
path: root/os/srv/nginx.nix
blob: a38b703c6ca2445126c3e70070518c9bd42eead2 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.os.srv.nginx;
in
{
  options.os.srv.nginx = {
    enable = lib.mkEnableOption "the NGINX reverse proxy service";

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Whether to open ports 80 and 443 in the firewall.";
    };
  };

  config = lib.mkMerge [
    {
      _module.args.securityTemplates.restrictToInternal = ''
        allow 127.0.0.1;
        allow ::1;

        allow ${config.os.core.network.lan.range};
        allow ${config.os.core.network.wg.range};
        allow ${config.os.core.network.hs.range};

        deny all;
      '';
    }

    (lib.mkIf cfg.enable {
      services.nginx = {
        enable = true;
        package = pkgs.nginx.override { openssl = pkgs.libressl; };

        recommendedProxySettings = true;
        recommendedTlsSettings = true;
        recommendedOptimisation = true;
        recommendedGzipSettings = true;
        virtualHosts = lib.mkMerge [
          {
            "_" = {
              default = true;
              rejectSSL = true;
              locations."/".return = "444";
            };
          }
          config.os.srv.monero.proxyConfig
        ];
      };

      security.acme = {
        acceptTerms = true;
        defaults.email = "adikro@disroot.org";
      };

      users.users.nginx.extraGroups = [ "acme" ];

      networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
        80
        443
      ];

      systemd.tmpfiles.rules = [
        "d /var/log/nginx 0750 nginx adm -"
      ];
    })
  ];
}