# Announcing tree-sitter-nix

**URL:** https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483
**Category:** Announcements
**Created:** [March 22, 2019, 11:08am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483 "2019-03-22T11:08:11Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [March 22, 2019, 11:08am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/1 "2019-03-22T11:08:11Z")

</div>

I wrote a [tree-sitter](http://tree-sitter.github.io/tree-sitter/) grammar for Nix: [GitHub - nix-community/tree-sitter-nix: Nix grammar for tree-sitter [maintainer=@cstrahan]](https://github.com/cstrahan/tree-sitter-nix)

Down the road, we could do some fun stuff, like provide syntax highlighting in `nix repl` (Tree Sitter tries its best to come up with a sensible parse tree, even if there are errors).

---

<div class="post-metadata">

### Author: ![zarel](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zarel/32/857_2.png) [@zarel](https://discourse.nixos.org/u/zarel)
#### Post date: [March 22, 2019, 5:05pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/2 "2019-03-22T17:05:29Z")

</div>

I was thinking about it for a long time, is there a plan to have it included in Atom? Maybe including it in @wmertens extnsion?

---

<div class="post-metadata">

### Author: ![wmertens](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wmertens/32/53_2.png) [@wmertens](https://discourse.nixos.org/u/wmertens)
#### Post date: [March 22, 2019, 8:43pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/3 "2019-03-22T20:43:47Z")

</div>

I would love to use this instead of my Nix grammar. The Nix grammar can’t express the full language correctly because it’s only regular expressions.  
However, that would mean having tree-sitter support in editors, something that doesn’t exist yet it seems ☹

---

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [March 23, 2019, 8:02am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/4 "2019-03-23T08:02:18Z")

</div>

> However, that would mean having tree-sitter support in editors, something that doesn’t exist yet it seems ☹

Atom has support, but I don’t know about any other editors. There’s some [ongoing work](https://github.com/neovim/neovim/pull/9219) to support tree-sitter in Neovim, though.

> I was thinking about it for a long time, is there a plan to have it included in Atom?

Yeah, that’s something I’d like to do eventually.

---

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [March 23, 2019, 8:08am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/5 "2019-03-23T08:08:42Z")

</div>

I’ll note that I’ve pushed some changes that add a pretty decent test suite, fixes a couple bugs, and overhauls the shape of the resulting parse tree (it was pretty verbose before, but now it’s very clean).

I think at this point it should be very robust, and I don’t expect much churn. I’d love for some people to play around with it and report back. If no one can find some improvements / faults, I’ll cut a 1.0.0 release.

---

<div class="post-metadata">

### Author: ![danbst](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danbst/32/83_2.png) [@danbst](https://discourse.nixos.org/u/danbst)
#### Post date: [March 23, 2019, 8:31am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/6 "2019-03-23T08:31:23Z")

</div>

Does it support escaping of dollar in strings ‘’$ ?

---

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [March 23, 2019, 8:50am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/7 "2019-03-23T08:50:41Z")

</div>

Nice catch. I just pushed a fix for that in the custom scanner (I’d link to the commit, but it looks like we can’t link to github on this setup of Discourse 🤷‍♂️).

---

<div class="post-metadata">

### Author: ![tinix](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tinix/32/449_2.png) [@tinix](https://discourse.nixos.org/u/tinix)
#### Post date: [March 23, 2019, 8:56am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/8 "2019-03-23T08:56:34Z")

</div>

🤔 just ftr

logged out  
on the first post on this thread i see

> This post was flagged by the community and is temporarily hidden.  
> View hidden content.

logged in  
i see

> This is the first time cstrahan has posted — let’s welcome them to our community!

and

> This post was flagged by the community and is temporarily hidden.  
> View hidden content.

/ftr

😕

---

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [March 23, 2019, 8:58am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/9 "2019-03-23T08:58:41Z")

</div>

Oh yeah. That’s kinda weird. I saw that my first post, and a subsequent post here, are showing up as slightly grayed out. Bizarre.

---

<div class="post-metadata">

### Author: ![aanderse](https://discourse.nixos.org/user_avatar/discourse.nixos.org/aanderse/32/1420_2.png) [@aanderse](https://discourse.nixos.org/u/aanderse)
#### Post date: [March 23, 2019, 11:30am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/10 "2019-03-23T11:30:07Z")

</div>

I also saw these strange things… on another post too. Something fishy with discourse.

---

<div class="post-metadata">

### Author: ![7c6f434c](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/7/8e7dd6/32.png) [@7c6f434c](https://discourse.nixos.org/u/7c6f434c)
#### Post date: [March 23, 2019, 11:52am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/11 "2019-03-23T11:52:08Z")

</div>

Discourse just plainly hates URLs. It flags posts of new users with them, it loses them in the plain-text parts of emails…

---

<div class="post-metadata">

### Author: ![justinw](https://discourse.nixos.org/user_avatar/discourse.nixos.org/justinw/32/1554_2.png) [@justinw](https://discourse.nixos.org/u/justinw)
#### Post date: [March 30, 2019, 3:45pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/12 "2019-03-30T15:45:11Z")

</div>

I started putting together a formatter using this grammar here: [GitHub - justinwoo/format-nix: A simple formatter for Nix using tree-sitter-nix.](https://github.com/justinwoo/format-nix)

It’s a simple PureScript Node application using the node tree-sitter bindings.

---

<div class="post-metadata">

### Author: ![cstrahan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/cstrahan/32/1099_2.png) [@cstrahan](https://discourse.nixos.org/u/cstrahan)
#### Post date: [April 2, 2019, 4:27pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/13 "2019-04-02T16:27:47Z")

</div>

That’s awesome! Glad to see the grammar is already proving useful.

Thanks for your contributions and feedback, btw.

---

<div class="post-metadata">

### Author: ![doronbehar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/doronbehar/32/1387_2.png) [@doronbehar](https://discourse.nixos.org/u/doronbehar)
#### Post date: [June 15, 2019, 11:43am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/14 "2019-06-15T11:43:12Z")

</div>

Hey, while looking at the status of language server implementations for nix, and stumbling upon this project, I think it’d be worth mentioning for reference [GitHub - bash-lsp/bash-language-server: A language server for Bash](https://github.com/mads-hartmann/bash-language-server) which implements a very fast and rather performent language server for bash using a tree-sitter grammer as well. I assume the same could be leveraged for a language server implementation for nix as well.

Current LSP alternatives:

- [jD91mZM2 / nix-lsp · GitLab](https://gitlab.com/jD91mZM2/nix-lsp) : uses Rust unstable, last commit from 9th April 2019.
- [GitHub - domenkozar/hnix-lsp: Language Server Protocol for Nix](https://github.com/domenkozar/hnix-lsp) : uses haskell, last commit from 3th March 2019.

I’ll also note that using a nodejs based language server is easier for Editors to include. Atom’s and vscode’s configuration ecosystems are based on Javascript AFAIK and one of Vim’s popular LSP client plugins is [GitHub - neoclide/coc.nvim: Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.](https://github.com/neoclide/coc.nvim) which uses a configuration system very much like VSCode’s. For example it’s sh extension is easily installed with just `:CocInstall coc-sh`.

---

<div class="post-metadata">

### Author: ![teto](https://discourse.nixos.org/user_avatar/discourse.nixos.org/teto/32/182_2.png) [@teto](https://discourse.nixos.org/u/teto)
#### Post date: [June 27, 2019, 6:20am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/15 "2019-06-27T06:20:25Z")

</div>

I am attempting to package (at least for myself) the neovim version that supports tree-sitter (see [Tree-sitter step 1: vendor runtime lib + add lua API by bfredl · Pull Request #10124 · neovim/neovim · GitHub](https://github.com/neovim/neovim/pull/10124) and [[WIP] Tree-sitter integration by bfredl · Pull Request #9219 · neovim/neovim · GitHub](https://github.com/neovim/neovim/pull/9219/files)).  
There seems to be a package on npmjs but as [GitHub - tree-sitter/tree-sitter: An incremental parsing system for programming tools](https://github.com/tree-sitter/tree-sitter) looked cargo-based, I was expecting a rust package. I also would need [GitHub - tree-sitter/tree-sitter-c: C grammar for tree-sitter](https://github.com/tree-sitter/tree-sitter-c) . I wonder if you had packaged any of this for your tests ? or tell me which one I should use in nixpkgs.

---

<div class="post-metadata">

### Author: ![teto](https://discourse.nixos.org/user_avatar/discourse.nixos.org/teto/32/182_2.png) [@teto](https://discourse.nixos.org/u/teto)
#### Post date: [September 27, 2019, 5:36am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/16 "2019-09-27T05:36:08Z")

</div>

I would like to load your grammar into neovim. I’ve started following [Guide to writing your first Tree-sitter grammar · GitHub](https://gist.github.com/Aerijo/df27228d70c633e088b0591b8857eeef) and trial by error led me to this command line `nix run nixpkgs.tree-sitter nixpkgs:nodePackages.node-gyp nixpkgs:python27` but then it doesn’t find /bin/sh . Do you have some recipe on how to generate a module onx nixos from your grammar ? the CI configuration was not very information-heavy either xD

---

<div class="post-metadata">

### Author: ![milahu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/milahu/32/4006_2.png) [@milahu](https://discourse.nixos.org/u/milahu)
#### Post date: [November 18, 2021, 9:19am UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/17 "2021-11-18T09:19:37Z")

</div>

related

- [syntax-highlighter](https://github.com/EvgeniyPeshkov/syntax-highlighter) to use tree-sitter parsers in vscode/vscodium  
but currently, [no incremental parsing](https://github.com/EvgeniyPeshkov/syntax-highlighter/issues/46)
- [lezer-parser-nix](https://github.com/milahu/lezer-parser-nix) pure javascript nix parser for codemirror editor

---

<div class="post-metadata">

### Author: ![wmertens](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wmertens/32/53_2.png) [@wmertens](https://discourse.nixos.org/u/wmertens)
#### Post date: [November 24, 2021, 2:58pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/18 "2021-11-24T14:58:03Z")

</div>

Do you know how can we use tree-sitter-nix with syntax-highlighter?

---

<div class="post-metadata">

### Author: ![milahu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/milahu/32/4006_2.png) [@milahu](https://discourse.nixos.org/u/milahu)
#### Post date: [November 24, 2021, 7:31pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/19 "2021-11-24T19:31:36Z")

</div>

something like [add tree-sitter-nix · milahu/syntax-highlighter@617abbf · GitHub](https://github.com/milahu/syntax-highlighter/commit/617abbf4a8e44bbf749c0cae1732c5e1e4728c28)  
the “grammar” file maps node types to token categories  
todo: update package-lock.json, use npmlock2nix or nix-npm-buildpackage,  
use tree-sitter from nixpkgs instead of tree-sitter-cli from npm

---

<div class="post-metadata">

### Author: ![Congee](https://discourse.nixos.org/user_avatar/discourse.nixos.org/congee/32/4156_2.png) [@Congee](https://discourse.nixos.org/u/Congee)
#### Post date: [November 24, 2021, 10:24pm UTC](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483/20 "2021-11-24T22:24:57Z")

</div>

Assume you know [GitHub - nix-community/rnix-lsp: WIP Language Server for Nix! [maintainer=@aaronjanse]](https://github.com/nix-community/rnix-lsp) but just for others who don’t know it to check out 😁

[Next page](https://discourse.nixos.org/t/announcing-tree-sitter-nix/2483.md?page=2)
