{ pkgs, lib, config, ... }:
let
  inherit (lib) types mkOption mkEnableOption;
  cfg = config.mods.server.search;
  port = config.mods.server.nginx.ports.search;
in {
  options.mods.server.search = {
    enable = mkEnableOption {
      default = false;
      description = "enables search engine server";
    };
  };

  config = {
    services.searx = lib.mkIf cfg.enable {
      enable = true;

      settings = {
        server.port = port;
        server.bind_address = "0.0.0.0";
        server.secret_key = "temporary-before-sops";

        enabled_plugins = [ "Hostnames plugin" "Tracker URL remover" ];
        hostnames.remove = [ "(.*.)?facebook.com$" ];
        hostnames.replace = {
          # Self-hosted
          "(.*.)?reddit.com$" = "reddit.muon.host";
          # "(.*.)?youtube.com$" = "videos.muon.host"; # TODO not working

          # External
          "(.*.)?youtube.com$" = "invidious.nerdvpn.de";
          "(.*.)?imdb.com$" = "libremdb.iket.me";
          "(.*.)?imgur.com$" = "rimgo.privacyredirect.com";
          "(.*.)?twitch.com$" = "safetwitch.privacyredirect.com";
          "(.*.)?wikipedia.com$" = "wikiless.privacyredirect.com";
          "(.*.)?medium.com$" = "scribe.privacyredirect.com";
          "(.*.)?stackoverflow.com$" = "anonymousoverflow.privacyredirect.com";
          "(.*.)?github.com$" = "gothub.privacyredirect.com";
        };
      };
    };
  };
}
