Announcing tree-sitter-nix

I wrote a tree-sitter grammar for Nix: 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).

9 Likes

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?

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 :frowning:

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

Atom has support, but I don’t know about any other editors. There’s some ongoing work 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.

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.

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

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 :man_shrugging:).

:thinking: 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

:confused:

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.

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

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

I started putting together a formatter using this grammar here: https://github.com/justinwoo/format-nix

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

5 Likes

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

Thanks for your contributions and feedback, btw.

1 Like

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 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:

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 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.