From c61c7418d066011591080873cab68e85d981d974 Mon Sep 17 00:00:00 2001 From: Alex Libman <5430928+aslibman@users.noreply.github.com> Date: Sat, 28 Mar 2026 18:22:13 -0400 Subject: [PATCH] bat: genericize shell init for bat-extras packages Moves shell init logic specific to bat-extras packages into the extras packages themselves. The bat package itself is now only reponsible for registering the bat-extras shell inits. Also uses finalAttrs.finalPackage to correctly handle overlays on bat-extra packages. --- nixos/modules/programs/bat.nix | 55 ++++++------------- .../misc/bat-extras/buildBatExtrasPkg.nix | 36 ++++++++++++ pkgs/tools/misc/bat-extras/modules/batman.nix | 3 + .../tools/misc/bat-extras/modules/batpipe.nix | 3 + 4 files changed, 58 insertions(+), 39 deletions(-) diff --git a/nixos/modules/programs/bat.nix b/nixos/modules/programs/bat.nix index 9d220cf7aa1c..d3fe0096c13d 100644 --- a/nixos/modules/programs/bat.nix +++ b/nixos/modules/programs/bat.nix @@ -5,9 +5,9 @@ ... }: let - inherit (builtins) isList elem; + inherit (builtins) isList; inherit (lib) - getExe + concatMapStrings literalExpression maintainers mapAttrs' @@ -36,33 +36,6 @@ let boolToString value else toString value; - - initScript = - { - program, - shell, - flags ? [ ], - }: - if (shell != "fish") then - '' - eval "$(${getExe program} ${toString flags})" - '' - else - '' - ${getExe program} ${toString flags} | source - ''; - - shellInit = - shell: - 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" ]; - }); in { options.programs.bat = { @@ -112,17 +85,21 @@ in ); }; - programs = { - bash = mkIf (!config.programs.fish.enable) { - interactiveShellInit = shellInit "bash"; + programs = + let + shellInit = shell: concatMapStrings (pkg: pkg.shellInit shell) cfg.extraPackages; + in + { + bash = mkIf (!config.programs.fish.enable) { + interactiveShellInit = shellInit "bash"; + }; + fish = mkIf config.programs.fish.enable { + interactiveShellInit = shellInit "fish"; + }; + zsh = mkIf (!config.programs.fish.enable && config.programs.zsh.enable) { + interactiveShellInit = shellInit "zsh"; + }; }; - fish = mkIf config.programs.fish.enable { - interactiveShellInit = shellInit "fish"; - }; - zsh = mkIf (!config.programs.fish.enable && config.programs.zsh.enable) { - interactiveShellInit = shellInit "zsh"; - }; - }; }; meta.maintainers = with maintainers; [ sigmasquadron ]; } diff --git a/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix b/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix index e4aaeedbd12b..9779438c460f 100644 --- a/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix +++ b/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix @@ -16,12 +16,18 @@ let "name" "dependencies" "meta" + "shellInit" ]; in { name, dependencies, meta ? { }, + # Config for the `shellInit` passthru (a `shell -> string` + # function returning the shell-specific init snippet for the bat-extras + # package). Specified as an attrset with a 'flags' string list argument. + # If null, there is no shell init for the package. + shellInit ? null, ... }@args: stdenv.mkDerivation ( @@ -76,6 +82,36 @@ stdenv.mkDerivation ( # We have already patched dontPatchShebangs = true; + passthru = + let + initScript = + { + program, + shell, + flags ? [ ], + }: + if (shell != "fish") then + '' + eval "$(${lib.getExe program} ${toString flags})" + '' + else + '' + ${lib.getExe program} ${toString flags} | source + ''; + in + { + shellInit = + shell: + if shellInit == null then + "" + else + initScript { + program = finalAttrs.finalPackage; + inherit shell; + flags = shellInit.flags or [ ]; + }; + }; + meta = core.meta // { mainProgram = name; } // meta; } ) diff --git a/pkgs/tools/misc/bat-extras/modules/batman.nix b/pkgs/tools/misc/bat-extras/modules/batman.nix index a08067f7df5e..bf518657c6a9 100644 --- a/pkgs/tools/misc/bat-extras/modules/batman.nix +++ b/pkgs/tools/misc/bat-extras/modules/batman.nix @@ -7,5 +7,8 @@ buildBatExtrasPkg { name = "batman"; dependencies = lib.optional stdenv.targetPlatform.isLinux util-linux; + shellInit = { + flags = [ "--export-env" ]; + }; meta.description = "Read system manual pages (man) using bat as the manual page formatter"; } diff --git a/pkgs/tools/misc/bat-extras/modules/batpipe.nix b/pkgs/tools/misc/bat-extras/modules/batpipe.nix index c8c9a754923d..c9cfdc7253a9 100644 --- a/pkgs/tools/misc/bat-extras/modules/batpipe.nix +++ b/pkgs/tools/misc/bat-extras/modules/batpipe.nix @@ -11,6 +11,9 @@ buildBatExtrasPkg { less procps ]; + shellInit = { + flags = [ ]; + }; patches = [ ../patches/batpipe-skip-outdated-test.patch