{ config, lib, ... }: {
  nixpkgs.overlays = [(final: prev: {
    vimPlugins = prev.vimPlugins // {
      snacks-nvim = prev.vimPlugins.snacks-nvim.overrideAttrs (old: {
        patches = (old.patches or []) ++ [ ./main.patch ];
      });
    };
  })];

  my.nvim.snacks.setupArgs = {
    picker = {
      sources = {
        git_log = {
          # Normal mode as the default mode
          on_show = lib.generators.mkLuaInline ''function() vim.cmd.stopinsert() end'';
          win.input.keys = {
            P = lib.generators.mkLuaInline ''{ "git_push", desc = "Push" }'';
            p = lib.generators.mkLuaInline ''{ "git_pull_ff", desc = "Pull (ff-only)" }'';
          };
        };
      };
    };
  };

  my.nvim.snacks.extraLuaConfig = /*lua*/ ''
    do
      local author_width = 6
      local coauthor_width = 4
      local palette = { "String", "Function", "Identifier", "Type", "Statement", "Special" }

      local function author_colors()
        local normal = vim.api.nvim_get_hl(0, { name = "Normal", link = false })
        for i, name in ipairs(palette) do
          local hl = vim.api.nvim_get_hl(0, { name = name, link = false })
          vim.api.nvim_set_hl(0, "SnacksGitLogAuthor" .. i, {
            fg = hl.fg or normal.fg,
            ctermfg = hl.ctermfg or normal.ctermfg,
          })
        end
      end
      author_colors()
      vim.api.nvim_create_autocmd("ColorScheme", {
        group = vim.api.nvim_create_augroup("SnacksGitLogAuthors", { clear = true }),
        callback = author_colors,
      })

      local function author_hl(name)
        -- Hash the full text, so truncation and column placement never change its colour.
        local hash = tonumber(vim.fn.sha256(name):sub(1, 8), 16)
        return "SnacksGitLogAuthor" .. (hash % #palette + 1)
      end

      -- Snacks' truncating align counts characters, which can overflow with wide names.
      local function column(text, width)
        if vim.fn.strdisplaywidth(text) > width then
          text = vim.fn.printf("%.*S", width - 1, text) .. "…"
        end
        return vim.fn.printf("%-*S", width, text)
      end

      local function format(item, picker)
        local date = item.date
        if date:sub(1, 10) == os.date("%Y.%m.%d") then
          date = string.rep(" ", 11) .. date:sub(12)
        end
        local coauthors = table.concat(item.coauthors or {}, ", ")
        local ret = {
          { item.commit:sub(1, 8), item.pushed == true and "DiagnosticWarn"
            or item.pushed == false and "DiagnosticError" or "SnacksPickerGitCommit"
          },
          { " " },
          { Snacks.picker.util.align(date, 16), "SnacksPickerGitDate" },
          { " " },
          { column(item.author, author_width), author_hl(item.author) },
          { " " },
          { column(coauthors, coauthor_width), author_hl(coauthors) },
          { " " },
          { item.graph, author_hl(item.author) },
          { item.refs, "GreenSign" },
          { " " },
        }
        Snacks.picker.highlight.extend(ret, require("snacks.picker.format").commit_message(item, picker))
        return ret
      end

      for _, source in ipairs({ "git_log", "git_log_file", "git_log_line" }) do
        local opts = Snacks.config.picker.sources[source] or {}
        opts.cmd = "${config.programs.git.package}/bin/git"
        opts.format = format
        Snacks.config.picker.sources[source] = opts
      end
    end
  '';
}