From 143ea58a7d7ffe2975856959019218ce0afa4165 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Sun, 28 Jun 2026 14:11:17 -0500 Subject: [PATCH] tree-sitter: restore extraGrammars The grammar source refactor removed the hook overlays used to add grammars before builtGrammars is computed. Add it back so custom grammar sources populate tree-sitter.grammars, tree-sitter.builtGrammars, tree-sitter-grammars, and withPlugins from one override. --- .../by-name/tr/tree-sitter/grammars/README.md | 25 +++++++++++++++++ pkgs/by-name/tr/tree-sitter/package.nix | 27 ++++++++++--------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pkgs/by-name/tr/tree-sitter/grammars/README.md b/pkgs/by-name/tr/tree-sitter/grammars/README.md index 9445f68494cb0..974d4f44a89c7 100644 --- a/pkgs/by-name/tr/tree-sitter/grammars/README.md +++ b/pkgs/by-name/tr/tree-sitter/grammars/README.md @@ -46,6 +46,31 @@ Attempt to build the new grammar: `nix-build -A tree-sitter-grammars.tree-sitter This will fail due to the invalid hash. Review the downloaded source, then update the source definition with the printed source `hash`. +## Extending Grammars + +Overlays may extend the grammar set without modifying nixpkgs by overriding `tree-sitter` with `extraGrammars`. +Extra grammars use the expanded `pkgs.tree-sitter.grammars` form: each key is the final `tree-sitter-*` attribute name, and each value provides `language`, `version`, and `src`. +When added through an overlay, extra grammars are also available through `pkgs.tree-sitter.builtGrammars`, `pkgs.tree-sitter-grammars`, and `pkgs.tree-sitter.withPlugins`. + +```nix +final: prev: { + tree-sitter = prev.tree-sitter.override { + extraGrammars = { + tree-sitter-foolang = { + language = "foolang"; + version = "0.42.0"; + src = final.fetchFromGitHub { + owner = "example"; + repo = "tree-sitter-foolang"; + tag = "v0.42.0"; + hash = ""; + }; + }; + }; + }; +} +``` + ## Pinning Grammar Sources To pin to a specific ref, append this to the source `url` to override the default version tag. diff --git a/pkgs/by-name/tr/tree-sitter/package.nix b/pkgs/by-name/tr/tree-sitter/package.nix index cc4522b35f217..46a245121f0dc 100644 --- a/pkgs/by-name/tr/tree-sitter/package.nix +++ b/pkgs/by-name/tr/tree-sitter/package.nix @@ -25,6 +25,7 @@ enableStatic ? stdenv.hostPlatform.isStatic, wasmSupport ? false, webUISupport ? false, + extraGrammars ? { }, }: let @@ -52,18 +53,20 @@ let Use pkgs.tree-sitter.grammars. to access. */ - grammars = import ./grammars { - inherit - lib - nix-update-script - fetchFromGitHub - fetchFromGitLab - fetchFromSourcehut - fetchFromCodeberg - fetchpatch - stdenv - ; - }; + grammars = + import ./grammars { + inherit + lib + nix-update-script + fetchFromGitHub + fetchFromGitLab + fetchFromSourcehut + fetchFromCodeberg + fetchpatch + stdenv + ; + } + // extraGrammars; /** Attrset of compiled grammars.