zaphyra's git: nixfiles

zaphyra and void'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 
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 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
{
  name,
  povSelf,
  hostConfig,
  config,
  pkgs,
  lib,
  ...
}:

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;
    };
    domain = {
      type = types.str;
      default = "ctu.cx";
    };
    subdomain = {
      type = types.str;
      default = "fedi.home";
    };
  };

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

    sops.secrets = {
      "resticPasswords/gotosocial" = { };
    };

    systemd.tmpfiles.settings.gotosocial = {
      "/var/lib/gotosocial/storage".d = {
        user = config.modules.services.gotosocial.user;
        group = config.modules.services.gotosocial.group;
        mode = "750";
        age = "-";
      };
    };

    modules.services = {
      resticBackup.paths = {
        gotosocial = {
          enable = true;
          user = config.modules.services.gotosocial.user;
          passwordFile = config.sops.secrets."resticPasswords/gotosocial".path;
          sqliteDatabases = [
            (lib.mkIf (
              config.modules.services.gotosocial.settings.db-type == "sqlite"
            ) config.modules.services.gotosocial.settings.db-address)
          ];
          paths = [
            (lib.mkIf (
              config.modules.services.gotosocial.settings.storage-backend == "local"
            ) config.modules.services.gotosocial.settings.storage-local-base-path)
            "${config.modules.services.gotosocial.stateDir}/backup.json"
          ];
          runBeforeBackup = ''${pkgs.gotosocial}/bin/gotosocial --config-path /etc/gotosocial.yaml admin export --path ${config.modules.services.gotosocial.stateDir}/backup.json'';
        };
      };
      gotosocial = {
        enable = true;
        group = config.services.nginx.group;
        settings = {
          protocol = lib.mkDefault "https";

          bind-address = lib.mkDefault "[::1]";
          port = lib.mkDefault 8085;

          trusted-proxies = lib.mkDefault [
            "::1/128"
            "172.17.0.0/24"
          ];

          db-type = lib.mkDefault "sqlite";
          db-address = lib.mkDefault "${config.modules.services.gotosocial.stateDir}/db.sqlite";

          storage-backend = lib.mkDefault "local";
          storage-local-base-path = "${config.modules.services.gotosocial.stateDir}/storage";

          host = "${cfg.subdomain}.${cfg.domain}";
          account-domain = cfg.domain;

          landing-page-user = "katja";

          accounts-allow-custom-css = true;
          accounts-registration-open = false;

          instance-expose-peers = true;
          instance-expose-suspended = true;
          instance-expose-suspended-web = true;

          instance-languages = [
            "de"
            "en-us"
          ];

          media-local-max-size = "50MiB";
          media-remote-max-size = "50MiB";

          media-remote-cache-days = 3;
          media-cleanup-from = "01:00";
        };
      };
    };

    services.nginx = {
      appendHttpConfig = ''
        proxy_cache_path /var/cache/nginx keys_zone=gotosocial_ap_public_responses:10m inactive=1w;
      '';
      virtualHosts =
        {
          "${config.modules.services.gotosocial.settings.host}" = {
            useACMEHost = lib.mkDefault "${config.networking.fqdn}";
            forceSSL = lib.mkDefault true;
            kTLS = lib.mkDefault true;
            locations = {
              "/" = {
                proxyPass = "http://${toString config.modules.services.gotosocial.settings.bind-address}:${toString config.modules.services.gotosocial.settings.port}";
                proxyWebsockets = true;
              };

              "~ /.well-known/(webfinger|host-meta)$" = {
                proxyPass = "http://${toString config.modules.services.gotosocial.settings.bind-address}:${toString config.modules.services.gotosocial.settings.port}";
                extraConfig = ''
                  proxy_cache gotosocial_ap_public_responses;
                  proxy_cache_background_update on;
                  proxy_cache_key $scheme://$host$uri$is_args$query_string;
                  proxy_cache_valid 200 10m;
                  proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_429;
                  proxy_cache_lock on;
                  add_header X-Cache-Status $upstream_cache_status;
                '';
              };

              "~ ^\/users\/(?:[a-z0-9_\.]+)\/main-key$" = {
                proxyPass = "http://${toString config.modules.services.gotosocial.settings.bind-address}:${toString config.modules.services.gotosocial.settings.port}";
                extraConfig = ''
                  proxy_cache gotosocial_ap_public_responses;
                  proxy_cache_background_update on;
                  proxy_cache_key $scheme://$host$uri;
                  proxy_cache_valid 200 604800s;
                  proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_429;
                  proxy_cache_lock on;

                  add_header X-Cache-Status $upstream_cache_status;
                '';
              };

              "/assets/".extraConfig = ''
                alias ${config.modules.services.gotosocial.package}/share/web/assets/;
                autoindex off;
                expires max;
                add_header Cache-Control "public, immutable";
              '';
            };
          };
        }
        // (
          if
            (
              config.modules.services.gotosocial.settings.account-domain
              != config.modules.services.gotosocial.settings.host
            )
          then
            {
              "${config.modules.services.gotosocial.settings.account-domain}" = {
                locations = {
                  "= /.well-known/host-meta".extraConfig =
                    "return 301 https://${config.modules.services.gotosocial.settings.host}$request_uri;";
                  "= /.well-known/webfinger".extraConfig =
                    "return 301 https://${config.modules.services.gotosocial.settings.host}$request_uri;";
                  "= /.well-known/nodeinfo".extraConfig =
                    "return 301 https://${config.modules.services.gotosocial.settings.host}$request_uri;";
                };
              };
            }
          else
            { }
        );
    };
  };

}