Files
home-manager/modules/programs/diff-highlight.nix
Ihar Hrachyshka 3c7524c683 treewide: set git pager for specific commands, not for core.pager
Setting `core.pager` may result in issues like swallowed `git grep` results. 
To avoid this, apply pagers to specific commands where we expect `diff`-ish output only. 
Use a common helper to set pager for all modules that configure it.
2026-04-14 11:31:44 -05:00

111 lines
3.2 KiB
Nix

{
config,
options,
lib,
pkgs,
...
}:
let
cfg = config.programs.diff-highlight;
inherit (lib)
mkEnableOption
mkIf
mkOption
types
;
in
{
meta.maintainers = with lib.maintainers; [ khaneliman ];
imports = [
(lib.mkRenamedOptionModule
[ "programs" "git" "diff-highlight" "enable" ]
[ "programs" "diff-highlight" "enable" ]
)
(lib.mkRenamedOptionModule
[ "programs" "git" "diff-highlight" "pagerOpts" ]
[ "programs" "diff-highlight" "pagerOpts" ]
)
];
options.programs.diff-highlight = {
enable = mkEnableOption "" // {
description = ''
Enable the contrib {command}`diff-highlight` syntax highlighter.
See <https://github.com/git/git/blob/master/contrib/diff-highlight/README>,
'';
};
pagerOpts = mkOption {
type = types.listOf types.str;
default = [ ];
example = [
"--tabs=4"
"-RFX"
];
description = ''
Arguments to be passed to {command}`less`.
'';
};
enableGitIntegration = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable git integration for diff-highlight.
When enabled, diff-highlight will be configured as git's pager for
{command}`diff`, {command}`log`, and {command}`show`, and as git's diff
filter for interactive staging.
'';
};
};
config =
let
oldOption = lib.attrByPath [ "programs" "git" "diff-highlight" "enable" ] null options;
oldOptionEnabled =
oldOption != null && oldOption.isDefined && (builtins.length oldOption.files) > 0;
in
lib.mkMerge [
(mkIf cfg.enable {
assertions = [
{
assertion = !cfg.enableGitIntegration || config.programs.git.package != null;
message = ''
programs.diff-highlight.enableGitIntegration requires programs.git.package to be set.
Please set programs.git.package to a valid git package.
'';
}
];
warnings =
lib.optional
(
cfg.enableGitIntegration && options.programs.diff-highlight.enableGitIntegration.highestPrio == 1490
)
"`programs.diff-highlight.enableGitIntegration` automatic enablement is deprecated. Please explicitly set `programs.diff-highlight.enableGitIntegration = true`.";
# Auto-enable git integration if programs.git.diff-highlight.enable was set to true
programs.diff-highlight.enableGitIntegration = lib.mkIf oldOptionEnabled (lib.mkOverride 1490 true);
})
(mkIf (cfg.enable && cfg.enableGitIntegration && config.programs.git.package != null) {
programs.git = {
enable = lib.mkDefault true;
iniContent =
let
gitPackage = config.programs.git.package;
dhCommand = "${gitPackage}/share/git/contrib/diff-highlight/diff-highlight";
pagerCommand = "${dhCommand} | ${lib.getExe pkgs.less} ${lib.escapeShellArgs cfg.pagerOpts}";
in
lib.hm.git.diffPagerConfig pagerCommand
// {
interactive.diffFilter = dhCommand;
};
};
})
];
}