(I do not have permissions to post in Announcements, so I am sharing this here. Moderators are welcome to move it.)
nix-manipulator is a Python library and CLI for parsing, transforming, and reconstructing Nix while preserving comments and layout, in line with RFC 166. It is built on Tree-sitter using the nix-community grammar.
Motivations
Nix is often described as “JSON on steroids”. I like this definition but did not find an easy way to update the values in a Nix file without doing string replacements or using regular expressions.
This got me curious about how nix-update proceeds… and it just uses string replacements.
So during SaltSprint 2025 this summer, I decided to tackle this problem and started using tree-sitter-nix to parse Nix code, transform the AST into opinionated and higher level structures and regenerating Nix code while preserving the original format.
This approach does not evaluate the Nix code, so definitions inside let ...
expressions or function calls are accessible.
Nix-manipulator is written in Python, with the goal to make this accessible from simple scripts, from a REPL or to be integrated into the existing Python tools.
I started writing a command-line interface named nima
to skip launching Python manually, which currently looks like:
nima set --file package.nix doCheck false
Now that I am back from holidays, I finally had time to publish an initial release, which you will find behind the links below.
Status and feedback
An initial release is available.
Not all Nix syntax is supported yet, but 73% of the files in nixpkgs
can already be manipulated.
I am looking for feedback on:
- Real-world edge cases and your use-cases
- API ergonomics for common edit patterns
- CLI subcommands and flags that would be most useful
Contributions are also welcome.
PyPI: nix-manipulator · PyPI