{ config, lib, pkgs, ... }:

let cfg = config.sydnix.deertopia.www;
in {
  options.sydnix.deertopia.www = {
    enable = lib.mkEnableOption "www.deertopia.net";
  };

  config = lib.mkIf cfg.enable {
    users.groups.www = {};

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

    systemd.tmpfiles.settings."10-www" =
      let
        f = user:
          let e = { inherit user; group = "www"; mode = "2755"; };
          in { z = e; v = e; };
      in {
        "/www" = f "root";
        "/www/~msyds" = f "msyds";
        "/www/~liv" = f "liv";
      };

    sydnix.impermanence.directories = [ "/www" ];

    sydnix.deertopia.nginx.vhosts."www" = {
      vhostName = "deertopia.net";
      directory = "/www";
      vhost = {
        forceSSL = true;
        enableACME = true;

        extraConfig = ''
          location /~msyds/ {
            index index.html;
          }
          location /~liv/ {
            index index.html;
          }
          location / {
            index index.html;
          }
        '';

        # locations."/" = {
        #   index = "index.html";
        # };
        # locations."/~msyds" = {
        #   index = "\\~msyds/index.html";
        # };
        # locations."/~liv" = {
        #   index = "\\~liv/index.html";
        # };
      };
    };
  };
}
