An Opinionated Formatter for Nix

Serokell
2 min readMay 12, 2019

--

Hi folks! Today we are happy to announce an alpha release of nixfmt, our formatter for Nix code. The work is still in progress, but we can't help sharing what we've done by now, as we consider the project truly important for the community and us as the Nix users. It is released under the MPL 2.0, and contributions are welcome.

At Serokell, we write quite a bit of Nix code, and the lack of tooling surrounding the language has always bothered us. No good formatter existed, and even editor indentation plugins were often lacking. So we set out to change that by ourselves.

We started writing nixfmt, a tool for formatting Nix code in a consistent way. In the project, we're using popular libraries like megaparsec and text, and we use a custom pretty printer originally based on the prettyprinter library.

Currently, nixfmt parses and formats all of nixpkgs in about 20 seconds on a dual-core CPU and individual files like all-packages.nix in less than a second. The project is available at https://github.com/serokell/nixfmt. To give it a try, clone it and run nix-build. Then run ./result/bin/nixfmt -w80 some.nix files.nix. For more details, see the README.md .

The tool is not entirely done yet, but the formatting it applies is very consistent and improves overall readability although unattractive in many places. We’re working hard to make it produce good results by the end of the month. In the first place, we aim to reach a nixfmt that generates somewhat pretty code for almost everything. At that point, it will be considered widely usable. Then we’d like to work along with the NixOS community people to bring the project up to standard for an officially accepted code formatter.

If you want to see how it works, we deployed it at https://nixfmt.serokell.io. We kindly invite you to help with the final stretch. Feel free to contribute!

--

--

Serokell
Serokell

Written by Serokell

Serokell is a software development company focused on building innovative solutions for complex problems. Come visit us at serokell.io!

No responses yet