Skip to content

bat: add shell integration for bat-extras packages#8966

Draft
aslibman wants to merge 1 commit intonix-community:masterfrom
aslibman:master
Draft

bat: add shell integration for bat-extras packages#8966
aslibman wants to merge 1 commit intonix-community:masterfrom
aslibman:master

Conversation

@aslibman
Copy link
Copy Markdown

Some bat-extras packages such as batman are used to replace specific shell functionality with bat. Adds shell init hooks for such packages.

Description

Add shell sourcing of bat-extras packages such at batman and batpipe so users do not need to manually add such sourcing to their shell configs.

Mostly just a copy of the logic from the equivalent nixpkgs module.

Checklist

  • Change is backwards compatible.

Technically, this change has the potential to inadvertently overrides users' $MANPAGER or $LESSOPEN if they installed either batman or batpipe and did not hook them into the shell. However, as these packages are usually installed to override specific shell functionality, I am hoping this change can have some leeway to avoid setting the shell integration defaults to false. Please let me know if this is unacceptable.

  • Code formatted with nix fmt or
    nix-shell -p treefmt nixfmt deadnix keep-sorted nixf-diagnose --run treefmt.

  • Code tested through nix run .#tests -- test-all or
    nix-shell --pure tests -A run.all.

  • Test cases updated/added. See example.

  • Commit messages are formatted like

    {component}: {description}
    
    {long description}
    

    See CONTRIBUTING for more information and recent commit messages for examples.

  • If this PR adds a new module

    • Added myself as module maintainer. See example.
    • Generate a news entry. See News
    • Basic tests added. See Tests
  • If this PR adds an exciting new feature or contains a breaking change.

    • Generate a news entry. See News

@home-manager-ci home-manager-ci Bot requested a review from khaneliman March 24, 2026 17:55
@aslibman aslibman marked this pull request as draft March 24, 2026 17:57
@aslibman aslibman closed this Mar 24, 2026
@aslibman aslibman reopened this Mar 24, 2026
@aslibman aslibman marked this pull request as ready for review March 24, 2026 18:39
Comment thread modules/programs/bat.nix Outdated
Comment on lines +33 to +41
optionalString (elem pkgs.bat-extras.batpipe cfg.extraPackages) (initScript {
program = pkgs.bat-extras.batpipe;
inherit shell;
})
+ optionalString (elem pkgs.bat-extras.batman cfg.extraPackages) (initScript {
program = pkgs.bat-extras.batman;
inherit shell;
flags = [ "--export-env" ];
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic won't work with custom/overlayed versions of the scripts...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with some logic to find the (potentially overlayed) pkg by pname. I'm still a nix noobie so if there's a better way lmk! 😄

Some bat-extras packages such as batman are used to replace specific shell functionality with bat. Adds shell init hooks for such packages.
Comment thread modules/programs/bat.nix
Comment on lines +18 to +40
initScript =
{
program,
shell,
flags ? [ ],
}:
if shell != "fish" then
''eval "$(${lib.getExe program} ${toString flags})"''
else
"${lib.getExe program} ${toString flags} | source";

shellInit =
shell:
optionalString (builtins.any (p: p.pname == "batpipe") cfg.extraPackages) (initScript {
program = lib.findFirst (p: p.pname == "batpipe") pkgs.bat-extras.batpipe cfg.extraPackages;
inherit shell;
})
+ optionalString (builtins.any (p: p.pname == "batman") cfg.extraPackages) (initScript {
program = lib.findFirst (p: p.pname == "batman") pkgs.bat-extras.batman cfg.extraPackages;
inherit shell;
flags = [ "--export-env" ];
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to programs let block.

Comment thread modules/programs/bat.nix
Comment on lines +234 to +237
programs.bash.initExtra = mkIf cfg.enableBashIntegration (shellInit "bash");
programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration (shellInit "fish");
programs.zsh.initExtra = mkIf cfg.enableZshIntegration (shellInit "zsh");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pack these programs into a single key. with a let block for the variables.

Comment thread modules/programs/bat.nix
Comment on lines +29 to +39
shellInit =
shell:
optionalString (builtins.any (p: p.pname == "batpipe") cfg.extraPackages) (initScript {
program = lib.findFirst (p: p.pname == "batpipe") pkgs.bat-extras.batpipe cfg.extraPackages;
inherit shell;
})
+ optionalString (builtins.any (p: p.pname == "batman") cfg.extraPackages) (initScript {
program = lib.findFirst (p: p.pname == "batman") pkgs.bat-extras.batman cfg.extraPackages;
inherit shell;
flags = [ "--export-env" ];
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little unintuitive and magical. I think we should add some sort of description explaining what the shell integration in this module does if we're just randomly adding shell sourcing for other packages related to bat.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, I also kinda felt that it's out of place to hook in the extras packages' internal details for sourcing into the bat top-level package, but it was the least friction path for me to get started off how nixpkgs already does it.

Maybe it's worth moving the sourcing logic into the bat extras packages themselves in the nixpkgs repo rather than in bat.nix here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to push some of the logic into nixpkgs itself, which would let me cleanup some of the 'magic' here.

Setting the PR to draft mode til the other PR lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants