{
  lib,
  config,
  pkgs,
  ...
}: let
  cfg = config.marleyos.programs.tmux;
in {
  options.marleyos.programs.tmux.enable = lib.mkEnableOption "tmux";

  config = lib.mkIf cfg.enable {
    stylix.targets.tmux.enable = false;

    programs.tmux = {
      enable = true;

      terminal = "tmux-256color";

      escapeTime = 0;

      # Start window & pane numbering at 1.
      baseIndex = 1;

      # Vi mode.
      keyMode = "vi";
      customPaneNavigationAndResize = true;
      mouse = true;

      # Auto-spawn a new session when attaching if none exist.
      newSession = true;

      # Key used with Ctrl to make the prefix.
      shortcut = "a";

      plugins = with pkgs.tmuxPlugins; [
        {
          plugin = resurrect;
        }

        {
          plugin = continuum;
          extraConfig =
            # tmux
            ''
              set -g @continuum-restore 'on'
            '';
        }

        {
          plugin = tilish;
          extraConfig =
            # tmux
            ''
              # Don't enforce the layout.
              set -g @tilish-enforce 'none'

              set -g @tilish-project "$HOME/hackin"
              set -g @tilish-navigator 'on'
            '';
        }

        {
          plugin = vim-tmux-navigator;
        }

        yank

        {
          plugin = jump;
          extraConfig =
            # tmux
            ''
              set -g @jump-key 's'
            '';
        }

        {
          plugin = fingers;
          extraConfig =
            # tmux
            ''
              set -g @fingers-jump-key 'f'
            '';
        }

        {
          plugin = tmux-floax;
          extraConfig =
            # tmux
            ''
              set -g @floax-bind 'i'
              set -g @floax-text-color 'white'
            '';
        }
      ];

      extraConfig = with config.lib.stylix.colors.withHashtag;
      # tmux
        ''
          set -g @rose_pine_directory 'on'

          # Set repeat timeout so keys can be repeated without the prefix.
          set -g repeat-time 1000

          # Auto-renumber windows when one is deleted.
          set -g renumber-windows 'on'

          set -ag terminal-overrides ",*:RGB"

          ### Theme ###
          # From https://github.com/rose-pine/tmux.

          set -g status 'on'
          set -g status-position 'top'
          set -g status-style "fg=${base0B},bg=${base00}"

          set -g status-left-length "200"
          set -g status-right-length "200"

          set -g message-style "fg=${base03},bg=${base00}"
          set -g message-command-style "fg=${base00},bg=${base09}"

          set -g pane-border-style "fg=${base07}"
          set -g pane-active-border-style "fg=${base09}"
          set -g display-panes-active-colour "${base05}"
          set -g display-panes-colour "${base09}"

          setw -g window-status-style "fg=${base0D},bg=${base00}"
          setw -g window-status-activity-style "fg=${base00},bg=${base0A}"
          setw -g window-status-current-style "fg=${base09},bg=${base00}"

          set -g @sep ' '
          set -g @lsep '  '
          set -g @rsep '  '

          set -g status-left " #[fg=#{?client_prefix,${base08},${base05}}] #[fg=${base05}]#S  #[fg=${base04}] #[fg=${base0A}]#W#{@sep}"
          set -g status-right "#{@sep}#[fg=${base05}]#H#[fg=${base04}]#{@rsep}#[fg=${base04}]󰒋 #[fg=${base0C}]%b %d %y#[fg=${base04}]#{@rsep}#[fg=${base04}]󰃰 #{@sep}#[fg=${base04}] #[fg=${base0A}]#{b:pane_current_path} "

          setw -g window-status-separator "  "
          setw -g clock-mode-colour "${base08}"
          setw -g mode-style "fg=${base09}"

          ### Keybindings ###

          # Easy reload config.
          bind r source-file ${config.xdg.configHome}/tmux/tmux.conf \; display-message "Reloaded config"

          # Better split commands.
          bind -N 'vsplit' |  split-window -h -c "#{pane_current_path}"
          bind -N 'vsplit' \\ split-window -h -c "#{pane_current_path}"
          bind -N 'hsplit' -  split-window -v -c "#{pane_current_path}"
          unbind '"'
          unbind %

          # Vi-like resizing.
          bind -r -N 'Resize pane (left)'  M-h resize-pane -L 5
          bind -r -N 'Resize pane (down)'  M-j resize-pane -D 5
          bind -r -N 'Resize pane (up)'    M-k resize-pane -U 5
          bind -r -N 'Resize pane (right)' M-l resize-pane -R 5

          # Even out panes.
          bind -N 'Evenly distribute panes' = select-layout -E

          # Swap panes.
          bind -r -N 'Swap current pane with the next'     H swap-pane -U
          bind -r -N 'Swap current pane with the previous' L swap-pane -D

          # Window switching.
          bind -N 'Previous window' Left previous-window
          bind -N 'Next window'     Right next-window

          # Vi copy mode.
          unbind [
          bind -N 'Enter normal (copy) mode' Escape copy-mode

          bind -T copy-mode-vi ? command-prompt -p '?' 'send -X search-backward %1'
          bind -T copy-mode-vi / command-prompt -p '/' 'send -X search-forward %1'
          bind -T copy-mode-vi q send -X cancel
          bind -T copy-mode-vi Escape if-shell -F '#{selection_present}' 'send -X clear-selection' 'send -X cancel'
        '';
    };

    home.shellAbbrs = {
      tmain = "tmux new -s main -A";
      tmobile = "tmux new -s mobile -A";
    };
  };
}
