The Uncompromising Nix Code Formatter

I designed Alejandra because I wanted a formatter to tell me how every piece of my code should look like, no exceptions

Speed, getting semantics perfectly correct, etc,
that is just what a good formatter should have,
this is not negotiable

This community is getting big
and we still do not have something like that.
Other communities like Rust, Go, Terraform, Python, they do have one.

There is clearly a problem to solve

Now, here are some technical facts though,
(I don’t care about beauty/style arguments)
to answer your question:

  1. Nixpkgs-fmt has bugs: Issues · nix-community/nixpkgs-fmt · GitHub

  2. Nixfmt has bugs: Escape Characters · Issue #81 · serokell/nixfmt · GitHub

  3. Alejandra was able to address certain bugs which I found in other formatters

See why:

  1. Format nixpkgs with nixfmt, run nixpkgs-review,
    and you’ll discover that many packages on Nixpkgs no longer build

    error: undefined variable 'PYTHON_EXECUTABLE'
    
        at /home/kamadorueda/.cache/nixpkgs-review/rev-f557203a0468aad924fcfd6ef3216d6497125df5-dirty/nixpkgs/pkgs/development/compilers/nextpnr/default.nix:57:22:
    
            56|     substituteInPlace ./ice40/CMakeLists.txt \
            57|       --replace '''${PYTHON_EXECUTABLE}' '${icestorm.pythonInterp}'
              |                      ^
            58|   '';
    
  2. Format Nixpkgs with nixpkgs-fmt, run nixpkgs-review,
    and you’ll discover that 37210 (almost all) packages on Nixpkgs needs
    rebuilding

Do you want a formatter breaking your builds?
I’m sure you don’t

Bugs multiplied by the 2.3 millions lines of Nix code that Nixpkgs have,
adds up a lot

Now, format Nixpkgs with Alejandra, you’ll get only 89 rebuilds!!!
all of them justified. No bugs.

Other facts:

  1. Nixpkgs-fmt is not extensive:

    It only formats a few elements on the grammar
    and leave others unformatted.

    Not formatting everything is a limitation of an architecture based on rules
    and not AST rebuilding:
    https://github.com/nix-community/nixpkgs-fmt/blob/5ae8532b82eb040ca6b21ae2d02d9e88f604e76a/src/rules.rs#L23

    Some people see “not formatting” as a feature of a formatter,
    I just simply can’t, that’s not the definition of a formatter.

    A formatter formats, right?

  2. Last release of nixfmt: Feb 10, 2020 (2 years ago)

  3. Where are the tests of nixfmt? Add a test suite · Issue #13 · serokell/nixfmt · GitHub

  4. It’s been more than ten minutes on a 16 cores machine,
    and nixfmt is still formatting Nixpkgs

In short, the important differences:

  • Correctness (bugs, broken builds)
  • Totality (a formatter must format everything)
  • Speed (dev time is expensive)

Style is subjective and I’ve been working lately on getting Alejandra into a format that is similar to Nixpkgs, in other words “beautiful” to the 51%: Issues · kamadorueda/alejandra · GitHub

14 Likes