lazygit: add shell integrations

This commit is contained in:
Chenxin Yan 2025-10-23 14:27:37 -04:00 committed by Austin Horstman
parent af6bb5ea8e
commit 24cad38b3f

View file

@ -5,7 +5,7 @@
...
}:
let
inherit (lib) mkIf;
inherit (lib) mkIf types;
cfg = config.programs.lazygit;
@ -49,6 +49,23 @@ in
for supported values.
'';
};
enableBashIntegration = lib.hm.shell.mkBashIntegrationOption { inherit config; };
enableFishIntegration = lib.hm.shell.mkFishIntegrationOption { inherit config; };
enableNushellIntegration = lib.hm.shell.mkNushellIntegrationOption { inherit config; };
enableZshIntegration = lib.hm.shell.mkZshIntegrationOption { inherit config; };
shellWrapperName = lib.mkOption {
type = types.str;
default = "lg";
example = "lg";
description = ''
Name of the shell wrapper to be called.
'';
};
};
config = mkIf cfg.enable {
@ -65,5 +82,49 @@ in
{
source = yamlFormat.generate "lazygit-config" cfg.settings;
};
programs =
let
bashIntegration = ''
${cfg.shellWrapperName}()
{
export LAZYGIT_NEW_DIR_FILE=~/.lazygit/newdir
lazygit "$@"
if [ -f $LAZYGIT_NEW_DIR_FILE ]; then
cd "$(cat $LAZYGIT_NEW_DIR_FILE)"
rm -f $LAZYGIT_NEW_DIR_FILE > /dev/null
fi
}
'';
fishIntegration = ''
set -x LAZYGIT_NEW_DIR_FILE ~/.lazygit/newdir
command lazygit $argv
if test -f $LAZYGIT_NEW_DIR_FILE
cd (cat $LAZYGIT_NEW_DIR_FILE)
rm -f $LAZYGIT_NEW_DIR_FILE
end
'';
nushellIntegration = ''
def --env ${cfg.shellWrapperName} [...args] {
$env.LAZYGIT_NEW_DIR_FILE = "~/.lazygit/newdir"
lazygit ...$args
if ($env.LAZYGIT_NEW_DIR_FILE | path exists) {
cd (open $env.LAZYGIT_NEW_DIR_FILE)
rm -f $env.LAZYGIT_NEW_DIR_FILE
}
}
'';
in
{
bash.initExtra = mkIf cfg.enableBashIntegration bashIntegration;
zsh.initContent = mkIf cfg.enableZshIntegration bashIntegration;
fish.functions.${cfg.shellWrapperName} = mkIf cfg.enableFishIntegration fishIntegration;
nushell.extraConfig = mkIf cfg.enableNushellIntegration nushellIntegration;
};
};
}