aboutsummaryrefslogtreecommitdiff
path: root/nixos/serverModules/nginx.nix
blob: 9e249d9ff50ca01f75a73090a780fb44842b6d73 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{ pkgs, config, lib, ... }:
with lib;
let
  vhostOptions = import (pkgs.path + "/nixos/modules/services/web-servers/nginx/vhost-options.nix");
  locationOptions = import (pkgs.path + "/nixos/modules/services/web-servers/nginx/location-options.nix");
  nginxOptions = import (pkgs.path + "/nixos/modules/services/web-servers/nginx/default.nix");
  cfg = config.nginx;
in
{
  options.nginx = {
    enable = mkEnableOption "Enable nginx";

    domain = mkOption {
      type = types.str;
      description = ''
        Domain to use with subdomains
      '';
    };

    recommendedProxySettings = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Enables global recommended proxy settings
      '';
    };

    subdomains = mkOption {
      type = types.attrsOf (types.submodule (locationOptions { inherit config lib; }));
      description = ''
        Subdomains with nginx virtualHosts configuration
      '';
    };

    extraVirtualHosts = mkOption {
      type = types.attrsOf (types.submodule (vhostOptions { inherit config lib; }));
      default = {};
    };

    home = {
      virtualHosts = mkOption {
        type = types.attrsOf (types.submodule (vhostOptions { inherit config lib; }));
        default = {};
        description = ''
          Virtual hosts from another nginx configuration, that will be used to decrypt ssl and forward traffic to another server.
          Make sure that the connection between the two is secure.
        '';
      };

      subdomains = mkOption {
        type = types.attrsOf (types.submodule (locationOptions { inherit config lib; }));
        default = {};
        description = ''
          Subdomains from another nginx configuration, that will be used to decrypt ssl and forward traffic to another server.
          Make sure that the connection between the two is secure.
        '';
      };

      domain = mkOption {
        type = types.str;
        default = cfg.domain;
        description = ''
          Home domain, if no domain provided, the current will be used;
        '';
      };
      
      url = mkOption {
        type = types.str;
        default = "";
        description = ''
          Url that requests would be passed to; 
        '';
      };
    };

    acme = {
      enable = mkEnableOption "enable acme certs";
      email = mkOption {
        type = types.str;
        default = "notspl3g+acme@duck.com";
      };
    };

    extraConfig = mkOption {
      type = types.attrsOf (types.submodule nginxOptions);
      default = {};
      description = ''
        Extra nginx config.
      '';
    };
  };
  
  config = mkIf cfg.enable {
    security.acme = mkIf cfg.acme.enable {
      acceptTerms = true;
      defaults.email = cfg.acme.email;
    };

    users.groups.nginx = mkIf cfg.acme.enable {};
    users.users.nginx = mkIf cfg.acme.enable {
      group = "nginx";
      extraGroups = [ "acme" ];
      isSystemUser = true;
    };
    services.nginx =
      let
        ssl = {
          forceSSL = cfg.acme.enable;
          enableACME = cfg.acme.enable;
        };

        makeVhosts = (domain: subdomains:
          lib.concatMapAttrs
            (name: value: { ${name + "." + domain} = { locations."/" = value; } // ssl; })
            subdomains);

        homeRoutes = (homeVirtualHosts: homeUrl:
          builtins.mapAttrs
            (name: value: {
              locations."/" = value.locations."/" // {
                proxyPass = homeUrl;
                recommendedProxySettings = true;
              };
            } // ssl)
            homeVirtualHosts);

        vhosts = (makeVhosts cfg.domain cfg.subdomains);
        homeVhosts = (homeRoutes ((makeVhosts (cfg.home.domain) cfg.home.subdomains) // cfg.home.virtualHosts) cfg.home.url);
      in {
        enable = true;
        recommendedProxySettings = cfg.recommendedProxySettings;
        
        virtualHosts =  vhosts // homeVhosts // cfg.extraVirtualHosts;
      } // cfg.extraConfig;
  };
}