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

let cfg = config.sydnix.deertopia.quiver;
in {
  options.sydnix.deertopia.quiver = {
    enable = lib.mkEnableOption "Quiver, a commutative diagram editor";
    package = lib.mkPackageOption sydpkgs.packages.${system} "quiver" {};
  };

  config = lib.mkIf cfg.enable {
    sydnix.deertopia.nginx.vhosts."q".vhost =
      let
	package = cfg.package.overrideAttrs (finalAttrs: prevAttrs: {
	  patches = (prevAttrs.patches or []) ++ [
	    ./quiver/move-export-url.patch
	  ];
	});
      in {
        forceSSL = true;
        enableACME = true;
        locations."/" = {
          extraConfig = ''
            root ${package}/opt/quiver;
          '';
        };
      };
  };
}
