The Uncompromising Nix Code Formatter

I just wanted to say that this is really awesome, thank you for your hard work on this formatter, looking forward to switching to it once the package hits stable (or I decide nixfmt annoys me enough I should grab the flake) :slight_smile:

5 Likes

This looks like fun, i will give it a whirl, because i need something to keep nix code under controlā€¦

However, i donā€™t think any software has no bugs, maybe ā€˜few bugsā€™ or no-known bugsā€¦ iā€™ve never seen a program that as complex as this without bugsā€¦

unless, itā€™s magic!

1 Like

Challenge accepted :rocket:

3 Likes

Thank you!

I just added a few statically linked pre-built binaries (less than 2MB each), so it is as easier as possible to get started

No need to grab any flake!

4 Likes

hello and thank you for your very cool project

I have installed both the package in my nixos machine as well as the vscode extension
Everything seems to be working

I only see a bug for now.
i first applied alejandra in all of my nix files then commit / push
when i update a file this is what i see now

Iā€™m happy for this!

Yes, this was a recent change, we processed the feedback of Nix experts over time and finally decided to favor the one you see to the right of your image

More about the rationale behind this decision can be found here:

We are cleaning up things before going for a 1.0 release

2 Likes

Is it intentional that same-line comments get flushed onto the next line?
The result becomes quite confusing to read:

-      (import ./cfg.nix) { allowEnv = true; }; # if no config is given allow env
+    else (import ./cfg.nix) { allowEnv = true; };
+  # if no config is given allow env

https://github.com/kamadorueda/alejandra/issues/32

Claiming this has no bugs has been a wise strategy because it has mobilized many people to prove it wrong.

41 Likes

Hi! As someone who worked on nixfmt, Iā€™d like to defend it for a bit.

Correctness: alejandra is far from perfect, try to format e.g. ''foo''\''bar'' and notice the changed semantics. These escapes are hard :wink:

Speed: nixfmt is designed to format all of nixpkgs in under half a minute, on a medium-powered laptop from 2016. Weā€™ll try to reproduce your ten-minute case, but please tell us how you tested this.

This turns your comparison into (emoji table):

            | Correctness | Totality | Speed
------------+-------------+----------+----------
nixpkgs-fmt | āŒ design   | āŒ design | āœ…
nixfmt      | āŒ bugs     | āœ…        | ??
alejandra   | āŒ bugs     | āœ…        | āœ…
3 Likes

Good catch! I confirm

What are your thoughts on the fact that Alejandra only causes 46 rebuilds in a set of 36000 derivations (Nixpkgs), vs that nixfmt actually breaks the build, vs that nixpkgs-fmt causes near 100% rebuilds ?

I agree no formatter is correct from the theoretical point of view (itā€™s impossible) but Alejandra is correct from a practical (2.3 millions lines of code of Nixpkgs) point of view

1 Like

Hello everyone,

I recently realized that my messaging
around the discourse post for Alejandra
where I compared different Nix formatting tools
can make people feel personally attacked

I want to apologize for this

The work on Alejandra is only possible because the previous projects,
I have personally used such formatters at work for many years.
Thank you

Truth be told,
I was excited about the tool I just created
and got carried away with so much enthusiasm that I forgot that
we are not just engineers,
but also humans with feelings,
and most importantly,
a community around a common goal.
My only wish is that Nix will have the best possible formatter

I will remove or edit the posts or announcements where I have made an inappropriate comparison

And I also ask for the understanding and forgiveness of the people involved

@zimbatm @yorickvP @andersk @zhenya @Lucus

24 Likes

I think were going to be implementing this formatter, not because the others are crap or badly designed or coded, just because we like it.

Is there anyway it can be ruled based, how much effort would it be to make it rule based, can we send you $$$ to make that feature happen?

Hey @kamadorueda, donā€™t worry about it.

How I see it, nixpkgs-fmt is 99% there but itā€™s always the last tiny percentage that takes a lot of energy. Personally, I am a bit burned out on the project, and so far nobody stepped up to take over the nixpkgs-fmt maintenance.

If itā€™s easier for you to rewrite rather than contribute, thatā€™s fine by me. Sometimes it takes less energy to write things from scratch. You own the whole design, rather than slowly moving a project to the desired state. Plus youā€™re re-using the rnix-parser so that counts as a win to me :slight_smile:

I donā€™t know what you wrote but I would assume that it was coming from a place of being enthusiastic. I donā€™t know for others but Iā€™d rather you keep your enthusiasm and not worry about roughing a few feathers. Keep up the good work!

21 Likes

Could you please help me understand what the expected outcome/user experience would be?

If it improves the project, letā€™s do it!

Hey, iā€™m sorry you feel burned out on the project, it happensā€¦ Me being a cybernetic robot from the future, a majority people donā€™t understand even robots from skynet, have energy levels.

6 Likes

Iā€™m adding a text-user interface:

asciicast

In this PR:
https://github.com/kamadorueda/alejandra/pull/164

Would be very happy if you help me test it in your own terminal or operative system before merging :heart:

4 Likes

Thanks @kamadorueda for the nice words.
I think alejandraā€™s current rules-based formatting is more inviting to collaborators and more maintainable, and that going for Rust will easy adoption.

We spent a while looking for bugs and found a few, which Iā€™ll put on the github issue tracker. Sadly, they are mostly rnix-parser related, and I think that project will need some contributions to make alejandra the bug-free formatter it aspires to be :wink: .

Another thing thatā€™s very useful to do (and that we did in nixfmt development) is looking at formatted nixpkgs diffs, prioritizing the files that have different outputs on nix-instantiate --parse.

3 Likes

Thank you! any type of engagement with the project is highly appreciated.
Besides, itā€™s helping us improve things and thatā€™s awesome!

I frequently ask myself if it would be more valuable (as community effort) to just rewrite that component of Nix to allow other programs to see the code as Nix would see it. Instead of creating a non-official library that just tries to mimic the Nix parsing behaviour (and will by definition differ from the official one)

5 Likes

I think a good idea would be an increased modularization of Nix as a whole (this would also make it easier for partial reimplementations of Nix, so that components can be swapped out); some of this is also a goal of https://riir-nix.github.io/, which I initiated with some enthusiasm, but some adjacent projects proved to be more difficult than expected (e.g. GitHub - YZITE/nix2js: [not yet usable] transpile Nix to Javascript using Rust, also uses rnix-parser, and I also reported some bugsā€¦, and fixed one or so). My proposal would be to provide a shared interface, such that the lexer-parser part (itā€™s complex, because of the string handling in Nix, it needs to toggle between lexing and parsing) of nix can be swapped out with some wrapper built on top of rnix-parser.

It would then be possible to build a fuzzer which makes sure that the implementations handle all valid input the same way, or such.

1 Like