{
  config,
  inputs,
  lib,
  pkgs,
  ...
}: {
  options.dotfiles.inputs.home-manager.programs.xplr.enable =
    lib.dotfiles.mkEnableOption;

  config = lib.mkIf config.dotfiles.inputs.home-manager.programs.xplr.enable {
    home.packages = let
      # TODO: Patch missing upstream plugin dependencies.
      dependencies.inputs.dragon-xplr = with pkgs; [curl xdragon];
    in
      dependencies.inputs.dragon-xplr;

    programs = {
      fish.functions.x = let
        xplr = lib.getExe config.programs.xplr.package;
      in {
        body = ''
          set --function pwd "$(${xplr} --print-pwd-as-result $argv)"

          if test -n $pwd
            cd -- $pwd
          end
        '';

        description = ''
          Open ${xplr} and change the working directory to the last visited one
        '';
      };

      xplr = {
        enable = true;

        extraConfig =
          lib.concatMapStringsSep
          "\n"
          (module: "require('${module}').setup()")
          (builtins.attrNames config.programs.xplr.plugins);

        plugins = {
          dragon = inputs.dragon-xplr;
          tri-pane = inputs.tri-pane-xplr;
        };
      };
    };
  };
}
