From d489575942518f7e20a0c9584003070e2a558a5c Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Tue, 4 Feb 2025 15:50:58 +0100 Subject: [PATCH] bat-extras: heavily refactor --- .../misc/bat-extras/buildBatExtrasPkg.nix | 77 +++++++ pkgs/tools/misc/bat-extras/default.nix | 208 +----------------- .../tools/misc/bat-extras/modules/batdiff.nix | 18 ++ .../tools/misc/bat-extras/modules/batgrep.nix | 15 ++ pkgs/tools/misc/bat-extras/modules/batman.nix | 11 + .../tools/misc/bat-extras/modules/batpipe.nix | 9 + .../misc/bat-extras/modules/batwatch.nix | 17 ++ pkgs/tools/misc/bat-extras/modules/core.nix | 86 ++++++++ .../misc/bat-extras/modules/prettybat.nix | 22 ++ .../{ => patches}/disable-theme-tests.patch | 0 pkgs/top-level/all-packages.nix | 2 +- 11 files changed, 266 insertions(+), 199 deletions(-) create mode 100644 pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/batdiff.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/batgrep.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/batman.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/batpipe.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/batwatch.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/core.nix create mode 100644 pkgs/tools/misc/bat-extras/modules/prettybat.nix rename pkgs/tools/misc/bat-extras/{ => patches}/disable-theme-tests.patch (100%) diff --git a/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix b/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix new file mode 100644 index 000000000000..adc621108c58 --- /dev/null +++ b/pkgs/tools/misc/bat-extras/buildBatExtrasPkg.nix @@ -0,0 +1,77 @@ +{ + lib, + stdenv, + core, + + bash, + bat, + fish, + getconf, + makeWrapper, + zsh, +}: +let + cleanArgs = lib.flip builtins.removeAttrs [ + "dependencies" + "meta" + ]; +in +{ + name, + dependencies, + meta ? { }, + ... +}@args: +stdenv.mkDerivation ( + finalAttrs: + cleanArgs args + // { + inherit (core) version; + + src = core; + + nativeBuildInputs = [ makeWrapper ]; + # Make the dependencies available to the tests. + buildInputs = dependencies; + + # Patch shebangs now because our tests rely on them + postPatch = '' + patchShebangs --host bin/${name} + ''; + + dontConfigure = true; + dontBuild = true; # we've already built + + doCheck = true; + nativeCheckInputs = [ + bat + bash + fish + zsh + ] ++ (lib.optionals stdenv.hostPlatform.isDarwin [ getconf ]); + checkPhase = '' + runHook preCheck + bash ./test.sh --compiled --suite ${name} + runHook postCheck + ''; + + installPhase = + '' + runHook preInstall + mkdir -p $out/bin + cp -p bin/${name} $out/bin/${name} + '' + + lib.optionalString (dependencies != [ ]) '' + wrapProgram $out/bin/${name} \ + --prefix PATH : ${lib.makeBinPath dependencies} + '' + + '' + runHook postInstall + ''; + + # We already patched + dontPatchShebangs = true; + + meta = core.meta // { mainProgram = name; } // meta; + } +) diff --git a/pkgs/tools/misc/bat-extras/default.nix b/pkgs/tools/misc/bat-extras/default.nix index 323b1dc21cc7..e10913b37852 100644 --- a/pkgs/tools/misc/bat-extras/default.nix +++ b/pkgs/tools/misc/bat-extras/default.nix @@ -1,200 +1,12 @@ -{ - lib, - stdenv, - fetchFromGitHub, - makeWrapper, - bat, - # batdiff, batgrep, and batwatch - coreutils, - getconf, - less, - # tests - bash, - zsh, - fish, - # batgrep - ripgrep, - # prettybat - withShFmt ? shfmt != null, - shfmt ? null, - withPrettier ? nodePackages ? prettier, - nodePackages ? null, - withClangTools ? clang-tools != null, - clang-tools ? null, - withRustFmt ? rustfmt != null, - rustfmt ? null, - # batwatch - withEntr ? entr != null, - entr ? null, - # batdiff - gitMinimal, - withDelta ? delta != null, - delta ? null, - # batman - util-linux, -}: +{ }: +self: { + buildBatExtrasPkg = self.callPackage ./buildBatExtrasPkg.nix { }; -let - # Core derivation that all the others are based on. - # This includes the complete source so the per-script derivations can run the tests. - core = stdenv.mkDerivation rec { - pname = "bat-extras"; - version = "2024.07.10"; - - src = fetchFromGitHub { - owner = "eth-p"; - repo = "bat-extras"; - rev = "v${version}"; - hash = "sha256-6IRAKSy5f/WcQZBcJKVSweTjHLznzdxhsyx074bXnUQ="; - fetchSubmodules = true; - }; - - # bat needs to be in the PATH during building so EXECUTABLE_BAT picks it up - nativeBuildInputs = [ bat ]; - - dontConfigure = true; - - patches = [ ./disable-theme-tests.patch ]; - - postPatch = '' - patchShebangs --build test.sh test/shimexec .test-framework/bin/best.sh - ''; - - buildPhase = '' - runHook preBuild - bash ./build.sh --minify=none --no-verify - runHook postBuild - ''; - - # Run the library tests as they don't have external dependencies - doCheck = true; - nativeCheckInputs = [ - bash - fish - zsh - ] ++ (lib.optionals stdenv.hostPlatform.isDarwin [ getconf ]); - checkPhase = '' - runHook preCheck - # test list repeats suites. Unique them - declare -A test_suites - while read -r action arg _; do - [[ "$action" == "test_suite" && "$arg" == lib_* ]] && - test_suites+=(["$arg"]=1) - done <<<"$(./test.sh --compiled --list --porcelain)" - (( ''${#test_suites[@]} != 0 )) || { - echo "Couldn't find any library test suites" - exit 1 - } - ./test.sh --compiled $(printf -- "--suite %q\n" "''${!test_suites[@]}") - runHook postCheck - ''; - - installPhase = '' - runHook preInstall - cp -a . $out - runHook postInstall - ''; - - # A few random files have shebangs. Don't patch them, they don't make it into the final output. - # The per-script derivations will go ahead and patch the files they actually install. - dontPatchShebangs = true; - - meta = with lib; { - description = "Bash scripts that integrate bat with various command line tools"; - homepage = "https://github.com/eth-p/bat-extras"; - license = with licenses; [ mit ]; - maintainers = with maintainers; [ bbigras ]; - platforms = platforms.all; - }; - }; - script = - name: # the name of the script - dependencies: # the tools we need to prefix onto PATH - stdenv.mkDerivation { - pname = name; - inherit (core) version; - - src = core; - - nativeBuildInputs = [ makeWrapper ]; - # Make the dependencies available to the tests. - buildInputs = dependencies; - - # Patch shebangs now because our tests rely on them - postPatch = '' - patchShebangs --host bin/${name} - ''; - - dontConfigure = true; - dontBuild = true; # we've already built - - doCheck = true; - nativeCheckInputs = [ - bat - bash - fish - zsh - ] ++ (lib.optionals stdenv.hostPlatform.isDarwin [ getconf ]); - checkPhase = '' - runHook preCheck - bash ./test.sh --compiled --suite ${name} - runHook postCheck - ''; - - installPhase = - '' - runHook preInstall - mkdir -p $out/bin - cp -p bin/${name} $out/bin/${name} - '' - + lib.optionalString (dependencies != [ ]) '' - wrapProgram $out/bin/${name} \ - --prefix PATH : ${lib.makeBinPath dependencies} - '' - + '' - runHook postInstall - ''; - - # We already patched - dontPatchShebangs = true; - - meta = core.meta // { - mainProgram = name; - }; - }; - optionalDep = - cond: dep: - assert cond -> dep != null; - lib.optional cond dep; -in -{ - batdiff = script "batdiff" ( - [ - less - coreutils - gitMinimal - ] - ++ optionalDep withDelta delta - ); - batgrep = script "batgrep" [ - less - coreutils - ripgrep - ]; - batman = script "batman" (lib.optionals stdenv.hostPlatform.isLinux [ util-linux ]); - batpipe = script "batpipe" [ less ]; - batwatch = script "batwatch" ( - [ - less - coreutils - ] - ++ optionalDep withEntr entr - ); - prettybat = script "prettybat" ( - [ ] - ++ optionalDep withShFmt shfmt - ++ optionalDep withPrettier nodePackages.prettier - ++ optionalDep withClangTools clang-tools - ++ optionalDep withRustFmt rustfmt - ); + core = self.callPackage ./modules/core.nix { }; + batdiff = self.callPackage ./modules/batdiff.nix { }; + batgrep = self.callPackage ./modules/batgrep.nix { }; + batman = self.callPackage ./modules/batman.nix { }; + batpipe = self.callPackage ./modules/batpipe.nix { }; + batwatch = self.callPackage ./modules/batwatch.nix { }; + prettybat = self.callPackage ./modules/prettybat.nix { }; } diff --git a/pkgs/tools/misc/bat-extras/modules/batdiff.nix b/pkgs/tools/misc/bat-extras/modules/batdiff.nix new file mode 100644 index 000000000000..57110cc4111f --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/batdiff.nix @@ -0,0 +1,18 @@ +{ + lib, + buildBatExtrasPkg, + less, + coreutils, + gitMinimal, + delta, + withDelta ? true, +}: +buildBatExtrasPkg { + name = "batdiff"; + dependencies = [ + less + coreutils + gitMinimal + ] ++ lib.optional withDelta delta; + meta.description = "Diff a file against the current git index, or display the diff between two files"; +} diff --git a/pkgs/tools/misc/bat-extras/modules/batgrep.nix b/pkgs/tools/misc/bat-extras/modules/batgrep.nix new file mode 100644 index 000000000000..8ce898761c5b --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/batgrep.nix @@ -0,0 +1,15 @@ +{ + buildBatExtrasPkg, + less, + coreutils, + ripgrep, +}: +buildBatExtrasPkg { + name = "batgrep"; + dependencies = [ + less + coreutils + ripgrep + ]; + meta.description = "Quickly search through and highlight files using ripgrep"; +} diff --git a/pkgs/tools/misc/bat-extras/modules/batman.nix b/pkgs/tools/misc/bat-extras/modules/batman.nix new file mode 100644 index 000000000000..a08067f7df5e --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/batman.nix @@ -0,0 +1,11 @@ +{ + lib, + stdenv, + buildBatExtrasPkg, + util-linux, +}: +buildBatExtrasPkg { + name = "batman"; + dependencies = lib.optional stdenv.targetPlatform.isLinux util-linux; + 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 new file mode 100644 index 000000000000..aa145c82db23 --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/batpipe.nix @@ -0,0 +1,9 @@ +{ + buildBatExtrasPkg, + less, +}: +buildBatExtrasPkg { + name = "batpipe"; + dependencies = [ less ]; + meta.description = "less (and soon bat) preprocessor for viewing more types of files in the terminal"; +} diff --git a/pkgs/tools/misc/bat-extras/modules/batwatch.nix b/pkgs/tools/misc/bat-extras/modules/batwatch.nix new file mode 100644 index 000000000000..0293629dddc2 --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/batwatch.nix @@ -0,0 +1,17 @@ +{ + lib, + buildBatExtrasPkg, + less, + coreutils, + entr, + + withEntr ? true, +}: +buildBatExtrasPkg { + name = "batwatch"; + dependencies = [ + less + coreutils + ] ++ lib.optional withEntr entr; + meta.description = "Watch for changes in one or more files, and print them with bat"; +} diff --git a/pkgs/tools/misc/bat-extras/modules/core.nix b/pkgs/tools/misc/bat-extras/modules/core.nix new file mode 100644 index 000000000000..6bb09788c948 --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/core.nix @@ -0,0 +1,86 @@ +{ + lib, + fetchFromGitHub, + stdenv, + bash, + bat, + fish, + getconf, + nix-update-script, + zsh, +}: +stdenv.mkDerivation rec { + pname = "bat-extras"; + version = "2024.08.24"; + + src = fetchFromGitHub { + owner = "eth-p"; + repo = "bat-extras"; + tag = "v${version}"; + hash = "sha256-xkND/w6UNC58dC8WrsifwjqU9ZI4yUUq+ZljkhhUNT8="; + fetchSubmodules = true; + }; + + # bat needs to be in the PATH during building so EXECUTABLE_BAT picks it up + nativeBuildInputs = [ bat ]; + + dontConfigure = true; + + patches = [ ../patches/disable-theme-tests.patch ]; + + postPatch = '' + patchShebangs --build test.sh test/shimexec .test-framework/bin/best.sh + ''; + + buildPhase = '' + runHook preBuild + bash ./build.sh --minify=none --no-verify + runHook postBuild + ''; + + # Run the library tests as they don't have external dependencies + doCheck = true; + nativeCheckInputs = [ + bash + fish + zsh + ] ++ (lib.optionals stdenv.hostPlatform.isDarwin [ getconf ]); + checkPhase = '' + runHook preCheck + # test list repeats suites. Unique them + declare -A test_suites + while read -r action arg _; do + [[ "$action" == "test_suite" && "$arg" == lib_* ]] && + test_suites+=(["$arg"]=1) + done <<<"$(./test.sh --compiled --list --porcelain)" + (( ''${#test_suites[@]} != 0 )) || { + echo "Couldn't find any library test suites" + exit 1 + } + ./test.sh --compiled $(printf -- "--suite %q\n" "''${!test_suites[@]}") + runHook postCheck + ''; + + installPhase = '' + runHook preInstall + cp -a . $out + runHook postInstall + ''; + + # A few random files have shebangs. Don't patch them, they don't make it into the final output. + # The per-script derivations will go ahead and patch the files they actually install. + dontPatchShebangs = true; + + passthru.updateScript = nix-update-script { }; + + meta = { + description = "Bash scripts that integrate bat with various command line tools"; + homepage = "https://github.com/eth-p/bat-extras"; + license = with lib.licenses; [ mit ]; + maintainers = with lib.maintainers; [ + bbigras + perchun + ]; + platforms = lib.platforms.all; + }; +} diff --git a/pkgs/tools/misc/bat-extras/modules/prettybat.nix b/pkgs/tools/misc/bat-extras/modules/prettybat.nix new file mode 100644 index 000000000000..618f6ac10a00 --- /dev/null +++ b/pkgs/tools/misc/bat-extras/modules/prettybat.nix @@ -0,0 +1,22 @@ +{ + lib, + buildBatExtrasPkg, + shfmt, + nodePackages, + clang-tools, + rustfmt, + + withShFmt ? true, + withPrettier ? true, + withClangTools ? true, + withRustFmt ? true, +}: +buildBatExtrasPkg { + name = "prettybat"; + dependencies = + lib.optional withShFmt shfmt + ++ lib.optional withPrettier nodePackages.prettier + ++ lib.optional withClangTools clang-tools + ++ lib.optional withRustFmt rustfmt; + meta.description = "Pretty-print source code and highlight it with bat"; +} diff --git a/pkgs/tools/misc/bat-extras/disable-theme-tests.patch b/pkgs/tools/misc/bat-extras/patches/disable-theme-tests.patch similarity index 100% rename from pkgs/tools/misc/bat-extras/disable-theme-tests.patch rename to pkgs/tools/misc/bat-extras/patches/disable-theme-tests.patch diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 30f86fc3ec11..0a39fe7db907 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -1941,7 +1941,7 @@ with pkgs; babelfish = callPackage ../shells/fish/babelfish.nix { }; - bat-extras = recurseIntoAttrs (callPackages ../tools/misc/bat-extras { }); + bat-extras = recurseIntoAttrs (lib.makeScope newScope (callPackage ../tools/misc/bat-extras { })); beauty-line-icon-theme = callPackage ../data/icons/beauty-line-icon-theme { inherit (plasma5Packages) breeze-icons;