{
  config,
  lib,
  pkgs,
  ...
}:

let
  git_wrapped =
    package: prog:
    pkgs.writeShellScriptBin "${prog}" ''
      export PATH="${
        lib.makeBinPath [
          pkgs.meld
        ]
      }:$PATH"
      exec ${package}/bin/${prog} "''${@}"
    '';
  git_hide = pkgs.writeShellScriptBin "git-hide" ''
    git add --intent-to-add "''${@}"
    git update-index --skip-worktree --assume-unchange "''${@}"
  '';
  git_unhide = pkgs.writeShellScriptBin "git-unhide" ''
    git update-index --no-skip-worktree --no-assume-unchange "''${@}"
    git reset "''${@}"
  '';
in
{
  imports = [ ];

  options.me = {
    git.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to install git.";
    };

    git.config = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      default = null;
      example = ./files/gitconfig_home;
      description = "A git config file.";
    };
  };

  config = lib.mkIf config.me.git.enable (
    lib.mkMerge [
      {
        environment.systemPackages = with pkgs; [
          my_git
          git_hide
          git_unhide
        ];
      }
      (lib.mkIf (config.me.git.config != null) {
        me.install.user.talexander.file = {
          ".gitconfig" = {
            source = config.me.git.config;
          };
        };
      })
      (lib.mkIf (config.me.graphical) {
        nixpkgs.overlays = [
          (final: prev: {
            my_git = (
              pkgs.symlinkJoin {
                name = final.git.name;
                version = final.git.version;
                paths =
                  (builtins.map (git_wrapped final.git) [
                    "git"
                  ])
                  ++ [
                    final.git
                  ];
                nativeBuildInputs = [ final.makeWrapper ];
              }
            );
          })
        ];
      })
      (lib.mkIf (!config.me.graphical) {
        nixpkgs.overlays = [
          (final: prev: {
            my_git = final.git;
          })
        ];
      })
    ]
  );
}
