zaphyra's git: nixfiles

zaphyra's nixfiles

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 
{
  povSelf,
  name,
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib) types;
  cfg = lib.getAttrFromPath povSelf config;
  cfgWebsites = lib.getAttrFromPath (lib.remove name povSelf) config;

in
{

  options = {
    enable = {
      type = types.bool;
      default = false;
    };
    subdomain = {
      type = types.str;
      default = "continuwuity";
    };
    domain = {
      type = types.str;
      default = "zaphyra.eu";
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = cfgWebsites."zaphyra.eu".enable == true;
        message = "The option 'modules.websites.\"zaphyra.eu\"' must be enabled in order to use this module.";
      }
    ];

    dns.zones = {
      "${cfg.domain}".subdomains."${cfg.subdomain}".CNAME = [
        "${config.networking.fqdn}."
      ];
    };

    modules.filesystem.impermanence.system.dirs = [
      "/var/lib/private/continuwuity"
    ];

    services.matrix-continuwuity = {
      enable = true;
      settings = {
        global = {
          address = [ "::1" ];
          trusted_servers = [
            "matrix.org"
            "tchncs.de"
          ];
          server_name = cfg.domain;
          allow_registration = false;
          yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse = false;
        };
      };
    };

    services.nginx = {
      enable = true;
      virtualHosts =
        let
          matrixServerConfig = {
            "m.server" = "${cfg.subdomain}.${cfg.domain}:443";
          };
          matrixClientConfig = {
            "m.homeserver".base_url = "https://${cfg.subdomain}.${cfg.domain}/";
          };
        in
        {
          "${config.services.matrix-continuwuity.settings.global.server_name}" = {
            locations = {
              "= /.well-known/matrix/server".extraConfig = ''
                add_header Content-Type application/json;
                add_header "Access-Control-Allow-Origin" "*";
                add_header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS";
                add_header "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization";
                return 200 '${builtins.toJSON matrixServerConfig}';
              '';
              "= /.well-known/matrix/client".extraConfig = ''
                add_header Content-Type application/json;
                add_header "Access-Control-Allow-Origin" "*";
                add_header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS";
                add_header "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization";
                return 200 '${builtins.toJSON matrixClientConfig}';
              '';
            };
          };
          "${cfg.subdomain}.${cfg.domain}" = {
            useACMEHost = "${config.networking.fqdn}";
            forceSSL = true;
            kTLS = true;
            locations = {
              "/_matrix" = {
                proxyPass = "http://[::1]:${toString config.services.matrix-continuwuity.settings.global.port}";
                proxyWebsockets = true;
              };
              "/".root = pkgs.cinny.override {
                conf = {
                  defaultHomeserver = 0;
                  homeserverList = [ cfg.domain ];
                  hashRouter.enabled = true;
                  allowCustomHomesevrers = false;
                };
              };
            };
          };
        };
    };
  };

}