bat: add shell integration for bat-extras packages#8966
bat: add shell integration for bat-extras packages#8966aslibman wants to merge 1 commit intonix-community:masterfrom
Conversation
| 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" ]; | ||
| }); |
There was a problem hiding this comment.
This logic won't work with custom/overlayed versions of the scripts...
There was a problem hiding this comment.
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.
| 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" ]; | ||
| }); | ||
|
|
There was a problem hiding this comment.
Move to programs let block.
| programs.bash.initExtra = mkIf cfg.enableBashIntegration (shellInit "bash"); | ||
| programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration (shellInit "fish"); | ||
| programs.zsh.initExtra = mkIf cfg.enableZshIntegration (shellInit "zsh"); | ||
|
|
There was a problem hiding this comment.
Pack these programs into a single key. with a let block for the variables.
| 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" ]; | ||
| }); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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
batmanandbatpipeso 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
Technically, this change has the potential to inadvertently overrides users' $MANPAGER or $LESSOPEN if they installed either
batmanorbatpipeand 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 fmtornix-shell -p treefmt nixfmt deadnix keep-sorted nixf-diagnose --run treefmt.Code tested through
nix run .#tests -- test-allornix-shell --pure tests -A run.all.Test cases updated/added. See example.
Commit messages are formatted like
See CONTRIBUTING for more information and recent commit messages for examples.
If this PR adds a new module
If this PR adds an exciting new feature or contains a breaking change.