{
  mkTarget,
  config,
  lib,
  ...
}:
mkTarget {
  name = "wezterm";
  humanName = "WezTerm";
  extraOptions.luaBody = lib.mkOption {
    type = lib.types.lines;
    default = "";
    internal = true;
  };
  configElements = [
    (
      { colors }:
      with colors;
      {
        programs.wezterm.colorSchemes.stylix = {
          ansi = [
            base00
            base08
            base0B
            base0A
            base0D
            base0E
            base0C
            base05
          ];
          brights = [
            base03
            base08
            base0B
            base0A
            base0D
            base0E
            base0C
            base07
          ];
          background = base00;
          cursor_bg = base05;
          cursor_fg = base00;
          compose_cursor = base06;
          foreground = base05;
          scrollbar_thumb = base01;
          selection_bg = base05;
          selection_fg = base00;
          split = base03;
          visual_bell = base09;
          tab_bar = {
            background = base01;
            inactive_tab_edge = base01;
            active_tab = {
              bg_color = base00;
              fg_color = base05;
            };
            inactive_tab = {
              bg_color = base03;
              fg_color = base05;
            };
            inactive_tab_hover = {
              bg_color = base05;
              fg_color = base00;
            };
            new_tab = {
              bg_color = base03;
              fg_color = base05;
            };
            new_tab_hover = {
              bg_color = base05;
              fg_color = base00;
            };
          };
        };
        stylix.targets.wezterm.luaBody = ''
          color_scheme = "stylix",
          window_frame = {
              active_titlebar_bg = "${base03}",
              active_titlebar_fg = "${base05}",
              active_titlebar_border_bottom = "${base03}",
              border_left_color = "${base01}",
              border_right_color = "${base01}",
              border_bottom_color = "${base01}",
              border_top_color = "${base01}",
              button_bg = "${base01}",
              button_fg = "${base05}",
              button_hover_bg = "${base05}",
              button_hover_fg = "${base03}",
              inactive_titlebar_bg = "${base01}",
              inactive_titlebar_fg = "${base05}",
              inactive_titlebar_border_bottom = "${base03}",
          },
          colors = {
            tab_bar = {
              background = "${base01}",
              inactive_tab_edge = "${base01}",
              active_tab = {
                bg_color = "${base00}",
                fg_color = "${base05}",
              },
              inactive_tab = {
                bg_color = "${base03}",
                fg_color = "${base05}",
              },
              inactive_tab_hover = {
                bg_color = "${base05}",
                fg_color = "${base00}",
              },
              new_tab = {
                bg_color = "${base03}",
                fg_color = "${base05}",
              },
              new_tab_hover = {
                bg_color = "${base05}",
                fg_color = "${base00}",
              },
            },
          },
          command_palette_bg_color = "${base01}",
          command_palette_fg_color = "${base05}",
        '';
      }
    )
    (
      { fonts }:
      {
        stylix.targets.wezterm.luaBody = ''
          font = wezterm.font_with_fallback {
              "${fonts.monospace.name}",
              "${fonts.emoji.name}",
          },
          font_size = ${toString fonts.sizes.terminal},
          command_palette_font_size = ${toString fonts.sizes.popups},
        '';
      }
    )
    (
      { opacity }:
      {
        stylix.targets.wezterm.luaBody = "window_background_opacity = ${toString opacity.terminal},";
      }
    )
    (
      { cfg }:
      let
        indent =
          string: "    " + lib.concatStringsSep "\n    " (lib.splitString "\n" string);
      in
      {
        xdg.configFile."wezterm/wezterm.lua" =
          lib.mkIf (config.programs.wezterm.enable && cfg.luaBody != "")
            {
              text = lib.mkForce (
                lib.concatLines [
                  ''
                    -- Generated by Stylix
                    local wezterm = require("wezterm")
                    wezterm.add_to_config_reload_watch_list(wezterm.config_dir)
                    -- Allow working with both the current release and the nightly
                    local config = {}
                    if wezterm.config_builder then
                      config = wezterm.config_builder()
                    end
                    local stylix_base_config = {
                  ''
                  (indent cfg.luaBody)
                  ''
                    }
                    for key, value in pairs(stylix_base_config) do
                        config[key] = value
                    end
                    local function stylix_wrapped_config()
                        ${config.programs.wezterm.extraConfig}
                    end
                    local stylix_user_config = stylix_wrapped_config()
                    if stylix_user_config then
                        for key, value in pairs(stylix_user_config) do
                            config[key] = value
                        end
                    end
                    return config
                  ''
                ]
              );
            };
      }
    )
  ];
}
