{
  config,
  lib,
  pkgs,
  ...
}: {
  imports = [
    ../../home/packages/wl-clipboard
    ../../home/sessionVariables
    ../../xdg
    ../firefox
    ../mpv
    ../xplr
  ];

  options.dotfiles.inputs.home-manager.programs.qutebrowser.enable =
    lib.dotfiles.mkEnableOption;

  config =
    lib.mkIf
    config.dotfiles.inputs.home-manager.programs.qutebrowser.enable
    (
      lib.mkMerge [
        {
          dotfiles.inputs.home-manager = {
            home = {
              packages.wl-clipboard.enable = true;

              sessionVariables = {
                BROWSER.enable = true;
                EDITOR.enable = true;
                TERMINAL.enable = true;
                enable = true;
              };
            };

            programs = {
              firefox.enable = true;
              mpv.enable = true;
              xplr.enable = true;
            };
          };

          home.activation =
            lib.dotfiles.home.activation.systemRequirement
            "dotfiles.inputs.home-manager.programs.qutebrowser"
            "programs.dconf.enable = true;"
            "https://nix-community.github.io/home-manager/options.xhtml#opt-services.easyeffects.enable";

          programs.qutebrowser = {
            aliases.x = "quit --save";
            enable = true;

            keyBindings.normal = let
              hint = "hint links";
              hintUrl = "'{hint-url}'";

              mpv = {
                audio = "--player-operation-mode=pseudo-gui --vid=no";
                finalPackage = lib.getExe config.programs.mpv.finalPackage;
                loop = "--loop-playlist";
                shuffle = "--shuffle";
              };

              spawn = "spawn --detach --verbose";
              url = "'{url}'";
            in
              lib.concatMapAttrs
              (
                name: value: {
                  "<Space>${name}" = lib.concatStringsSep " " value;
                }
              )
              {
                "f" = [
                  spawn
                  (lib.getExe config.programs.firefox.finalPackage)
                  url
                ];

                "mA" = [spawn mpv.finalPackage mpv.audio url];
                "mV" = [spawn mpv.finalPackage url];
                "ma" = [hint spawn mpv.finalPackage mpv.audio hintUrl];

                "mlA" = [
                  spawn
                  mpv.finalPackage
                  mpv.loop
                  mpv.audio
                  mpv.shuffle
                  url
                ];

                "mlV" = [spawn mpv.finalPackage mpv.loop url];

                "mla" = [
                  hint
                  spawn
                  mpv.finalPackage
                  mpv.loop
                  mpv.audio
                  mpv.shuffle
                  hintUrl
                ];

                "mlv" = [hint spawn mpv.finalPackage mpv.loop hintUrl];
                "mv" = [hint spawn mpv.finalPackage hintUrl];
              };

            settings = let
              xplr = lib.getExe config.programs.xplr.package;
            in {
              colors.webpage = {
                darkmode.enabled = true;
                preferred_color_scheme = "dark";
              };

              content = {
                autoplay = false;
                cookies.store = false;
              };

              editor.command = [
                config.home.sessionVariables.TERMINAL
                "-e"
                pkgs.runtimeShell
                "-c"
                (lib.concatStringsSep " " [
                  config.home.sessionVariables.EDITOR
                  "-c 'call cursor({line}, {column0})'"
                  "--"
                  "{file}"
                  "&&"
                  "nohup"
                  (lib.getExe' pkgs.wl-clipboard "wl-copy")
                  "< {file}"

                  # Redirect all terminal output to /dev/null to avoid nohup.out
                  # file creations:
                  #
                  #     If standard error is a terminal, redirect it to standard
                  #     output.  To save output to FILE, use 'nohup COMMAND >
                  #     FILE'.
                  #
                  #     -- nohup(1)
                  ">/dev/null"

                  "2>&1"
                ])
              ];

              fileselect = {
                folder.command = [
                  config.home.sessionVariables.TERMINAL
                  "-e"
                  pkgs.runtimeShell
                  "-c"
                  "${xplr} --print-pwd-as-result --read-only >{}"
                ];

                handler = "external";

                multiple_files.command = [
                  config.home.sessionVariables.TERMINAL
                  "-e"
                  pkgs.runtimeShell
                  "-c"
                  "${xplr} --read-only >{}"
                ];

                single_file.command = [
                  config.home.sessionVariables.TERMINAL
                  "-e"
                  pkgs.runtimeShell
                  "-c"
                  "${xplr} --read-only >{}"
                ];
              };

              input.forward_unbound_keys = "none";
              keyhint.delay = 0;
              messages.timeout = 5000;

              statusbar.widgets = [
                "keypress"
                "search_match"
                "url"
                "scroll"
                "history"
                "progress"
              ];

              tabs.last_close = "default-page";

              # To avoid excessive memory reallocations, the individual zoom
              # levels are independently computed on the CPU, instead of
              # implementing an accumulative multiplication.
              zoom.levels =
                map
                (level: "${toString (lib.dotfiles.pow 1.5 level)}%")
                (lib.range 8 17);
            };
          };
        }

        (
          lib.mkIf
          (config.dotfiles.inputs.home-manager.programs.firefox.enable or false)
          {
            dotfiles.inputs.home-manager.xdg.mimeApps.enable = true;

            xdg.mimeApps.defaultApplications = let
              qutebrowser = "org.qutebrowser.qutebrowser.desktop";
            in {
              "application/xhtml+xml" = [qutebrowser];
              "text/html" = [qutebrowser];
              "text/xml" = [qutebrowser];
              "x-scheme-handler/http" = [qutebrowser];
              "x-scheme-handler/https" = [qutebrowser];
            };
          }
        )
      ]
    );
}
