13 , enableShared ? !stdenv.hostPlatform.isStatic
14 , enableStatic ? stdenv.hostPlatform.isStatic
15 , webUISupport ? false
21 # 1) change all these hashes
22 # 2) nix-build -A tree-sitter.updater.update-all-grammars
23 # 3) Set GITHUB_TOKEN env variable to avoid api rate limit (Use a Personal Access Token from https://github.com/settings/tokens It does not need any permissions)
24 # 4) run the ./result script that is output by that (it updates ./grammars)
26 sha256 = "sha256-278zU5CLNOwphGBUa4cGwjBqRJ87dhHMzFirZB09gYM=";
27 cargoSha256 = "sha256-0avy53pmR7CztDrL+5WAmlqpZwd/EA3Fh10hfPXyXZc=";
29 src = fetchFromGitHub {
30 owner = "tree-sitter";
34 fetchSubmodules = true;
37 update-all-grammars = callPackage ./update.nix { };
39 fetchGrammar = (v: fetchgit { inherit (v) url rev sha256 fetchSubmodules; });
42 runCommand "grammars" { } (''
44 '' + (lib.concatStrings (lib.mapAttrsToList
45 (name: grammar: "ln -s ${if grammar ? src then grammar.src else fetchGrammar grammar} $out/${name}\n")
46 (import ./grammars { inherit lib; }))));
48 buildGrammar = callPackage ./grammar.nix { };
52 build = name: grammar:
54 language = grammar.language or name;
56 src = grammar.src or (fetchGrammar grammar);
57 location = grammar.location or null;
58 generate = grammar.generate or false;
60 grammars' = import ./grammars { inherit lib; } // extraGrammars;
61 grammars = grammars' //
62 { tree-sitter-ocaml = grammars'.tree-sitter-ocaml // { location = "ocaml"; }; } //
63 { tree-sitter-ocaml-interface = grammars'.tree-sitter-ocaml // { location = "interface"; }; } //
64 { tree-sitter-org-nvim = grammars'.tree-sitter-org-nvim // { language = "org"; }; } //
65 { tree-sitter-typescript = grammars'.tree-sitter-typescript // { location = "typescript"; }; } //
66 { tree-sitter-tsx = grammars'.tree-sitter-typescript // { location = "tsx"; }; } //
67 { tree-sitter-typst = grammars'.tree-sitter-typst // { generate = true; }; } //
68 { tree-sitter-markdown = grammars'.tree-sitter-markdown // { location = "tree-sitter-markdown"; }; } //
69 { tree-sitter-markdown-inline = grammars'.tree-sitter-markdown // { language = "markdown_inline"; location = "tree-sitter-markdown-inline"; }; } //
70 { tree-sitter-wing = grammars'.tree-sitter-wing // { location = "libs/tree-sitter-wing"; generate = true; }; };
72 lib.mapAttrs build (grammars);
75 # pkgs.tree-sitter.withPlugins (p: [ p.tree-sitter-c p.tree-sitter-java ... ])
77 # or for all grammars:
78 # pkgs.tree-sitter.withPlugins (_: allGrammars)
79 # which is equivalent to
80 # pkgs.tree-sitter.withPlugins (p: builtins.attrValues p)
81 withPlugins = grammarFn:
83 grammars = grammarFn builtGrammars;
89 name = lib.strings.getName drv;
93 (lib.strings.replaceStrings [ "-" ] [ "_" ]
94 (lib.strings.removePrefix "tree-sitter-"
95 (lib.strings.removeSuffix "-grammar" name)))
97 path = "${drv}/parser";
102 allGrammars = builtins.attrValues builtGrammars;
105 rustPlatform.buildRustPackage {
106 pname = "tree-sitter";
107 inherit src version cargoSha256;
110 lib.optionals stdenv.isDarwin [ Security CoreServices ];
113 ++ lib.optionals webUISupport [ emscripten ];
115 postPatch = lib.optionalString (!webUISupport) ''
116 # remove web interface
117 sed -e '/pub mod playground/d' \
119 sed -e 's/playground,//' \
120 -e 's/playground::serve(¤t_dir.*$/println!("ERROR: web-ui is not available in this nixpkgs build; enable the webUISupport"); std::process::exit(1);/' \
124 # Compile web assembly with emscripten. The --debug flag prevents us from
125 # minifying the JavaScript; passing it allows us to side-step more Node
126 # JS dependencies for installation.
127 preBuild = lib.optionalString webUISupport ''
128 mkdir -p .emscriptencache
129 export EM_CACHE=$(pwd)/.emscriptencache
130 bash ./script/build-wasm --debug
134 PREFIX=$out make install
135 ${lib.optionalString (!enableShared) "rm $out/lib/*.so{,.*}"}
136 ${lib.optionalString (!enableStatic) "rm $out/lib/*.a"}
139 # test result: FAILED. 120 passed; 13 failed; 0 ignored; 0 measured; 0 filtered out
144 inherit update-all-grammars;
146 inherit grammars buildGrammar builtGrammars withPlugins allGrammars;
149 # make sure all grammars build
150 builtGrammars = lib.recurseIntoAttrs builtGrammars;
155 homepage = "https://github.com/tree-sitter/tree-sitter";
156 description = "A parser generator tool and an incremental parsing library";
158 Tree-sitter is a parser generator tool and an incremental parsing library.
159 It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited.
161 Tree-sitter aims to be:
163 * General enough to parse any programming language
164 * Fast enough to parse on every keystroke in a text editor
165 * Robust enough to provide useful results even in the presence of syntax errors
166 * Dependency-free so that the runtime library (which is written in pure C) can be embedded in any application
168 license = licenses.mit;
169 maintainers = with maintainers; [ Profpatsch ];