summary refs log tree commit diff
path: root/modules/foundation/www/default.nix
blob: 5030799a65fa814e3323c85b32e39f799b11d07d (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
  me,
  config,
  pkgs,
  lib,
  util,
  cloudflare-ips-v4,
  cloudflare-ips-v6,
  ...
}:

let
  inherit (lib)
    mergeAttrsList
    mkIf
    mkEnableOption
    mkOption
    concatMapStrings
    concatLines
    splitString
    ;
  inherit (config.age) secrets;

  cfg = config.foundation.www;

  rnrdUrl = if me.is.renard then "rnrd.eu" else "${me.name}.rnrd.eu";

  default-page-index = pkgs.substituteAll {
    src = ../../../assets/base.html;
    env.me = util.titleCase me.name;
  };

  default-page-index-w-goat = pkgs.concatText "base.html" [
    default-page-index
    ../../../assets/goat.html
  ];

  default-page = pkgs.linkFarm "www-base" {
    "index.html" = default-page-index-w-goat;
    "favicon.png" = ../../../assets/favicon.png;
  };

  certificate = domain: {
    ${domain} = {
      domain = "*.${domain}";
      extraDomainNames = [ domain ];

      dnsProvider = "cloudflare";
      credentialFiles = {
        CLOUDFLARE_DNS_API_TOKEN_FILE = secrets.cloudflare-dns.path;
      };
    };
  };

  defaultHost = domain: certificate: base: log: {
    default = true;
    serverName = domain;
    forceSSL = true;
    useACMEHost = certificate;

    root = base;
    extraConfig = ''
      access_log /var/log/nginx/${log}.access.log json_combined;
    '';
  };

in
{
  imports = [ ./tailnet.nix ];

  options.foundation.www = {
    enable = mkEnableOption "www server";
    public = mkEnableOption "public access through rnrd.eu url";

    defaultPage = mkOption {
      type = lib.types.package;
      default = default-page;
    };
  };

  config = mkIf cfg.enable {
    age.secrets = {
      cloudflare-dns.file = ../../../secrets/cloudflare-dns.age;
    };

    security.acme = {
      acceptTerms = true;
      # this sometimes causes issues with tailnet certificates,
      # but otherwise nginx does not want to launch with how i've configured it.
      # TODO if tailscale cert generation is failing again, investigate.
      preliminarySelfsigned = true;
      defaults = {
        email = "mel@rnrd.eu";
        # our certificates are really only used with Nginx
        group = config.services.nginx.group;
        reloadServices = [ "nginx.service" ];
      };

      # yes, we generate both certificates, even if they are not
      # used by every machine, but as long as it doesn't cause
      # any problems... :)
      certs = mergeAttrsList [
        (certificate "rnrd.eu")
        (certificate "rnrd.fyi")
      ];
    };

    services.nginx = {
      enable = true;
      recommendedGzipSettings = true;
      recommendedOptimisation = true;
      recommendedProxySettings = true;
      recommendedTlsSettings = true;

      statusPage = true;

      commonHttpConfig =
        let
          logs = ''
            log_format json_combined escape=json '{'
              '"time_local":"$time_local",'
              '"remote_addr":"$remote_addr",'
              '"remote_user":"$remote_user",'
              '"request":"$request",'
              '"status": "$status",'
              '"body_bytes_sent":"$body_bytes_sent",'
              '"request_length":"$request_length",'
              '"request_time":"$request_time",'
              '"http_referrer":"$http_referer",'
              '"http_user_agent":"$http_user_agent",'
              '"upstream_response_time":"$upstream_response_time",'
              '"upstream_addr":"$upstream_addr",'
              '"upstream_status":"$upstream_status",'
              '"cf_connecting_ip":"$http_cf_connecting_ip"'
            '}';
            access_log /var/log/nginx/access.log json_combined;
            error_log /var/log/nginx/error.log warn;
          '';

          cloudflareAddresses = builtins.filter (ip: ip != "") (
            splitString "\n" ''
              ${builtins.readFile cloudflare-ips-v4}
              ${builtins.readFile cloudflare-ips-v6}
            ''
          );

          realIpLine = ip: "set_real_ip_from ${ip};\n";

          cloudflare = ''
            ${concatMapStrings realIpLine cloudflareAddresses}

            real_ip_header CF-Connecting-IP;
          '';
        in
        concatLines [ logs cloudflare ];

      virtualHosts = {
        base = mkIf cfg.public (defaultHost rnrdUrl "rnrd.eu" cfg.defaultPage "base");
      };
    };
  };
}