How can I use standalone `tree-sitter parse` with existing language grammars?

I would like to test tree-sitter to parse python programs. I started by trying:

$ nix-shell -p tree-sitter --run "tree-sitter parse ast.py"
[...]
Warning: You have not configured any parser directories!
Please run `tree-sitter init-config` and edit the resulting
configuration file to indicate where we should look for
language grammars.

No language found

Seems that init-config would clutter my home. I hope there is a way to avoid that home-dependent plumbing in NixOS…

As far as I understand, we package common grammars, for example tree-sitter-grammars.tree-sitter-python.
Following nixpkgs/pkgs/development/tools/parsing/tree-sitter/default.nix at 50b3bd3fed0442bcbf7f58355e990da84af1749d · NixOS/nixpkgs · GitHub, I tried:

$ nix-shell -p "tree-sitter.withPlugins(ps: [ ps.tree-sitter-python ])" --run "tree-sitter parse atbdx2024/examples/ast.py"
[...]
/tmp/nix-shell-213097-0/rc: line 3: tree-sitter: command not found

Am I misunderstanding how withPlugins is supposed to work?

cc @Profpatsch

1 Like

I am having a similar issue, and I thought I knew the cause: From the Tree Sitter Repo’s docs:

In relevant part:

Currently, any folder within one of these parser directories whose name begins with tree-sitter- will be treated as a Tree-sitter grammar repository.

The plugins are in the nix store, so placing /nix/store/<some nix sha>-tree-sitter-<language>/parser doesn’t work because these directories don’t start with tree-sitter-. However, copying the contents of one of those directories to a directory of the ‘correct’ format also didn’t seem to work, so I’m pretty lost.

I’ve tried a variety of tweaks to the treesitter config, you can specify a location for one so in principle it should be possible to generate one in the nix store and refer to that, but that doesn’t appear to be what’s happening in the nix side of things.

I’m an utter novice with treesitter and barely a journeyman with Nix/Nixos, so I might be wrong on the theory, but I was definitely able to reproduce a similar result with a different TS Parser (rust, in my case).