Alejandra 💅
The Uncompromising Nix Code Formatter
Try it on your browser! here
Features
-
✔️ Fast
It's written in Rust and formats Nixpkgs in just a few seconds. 1
-
✔️ Powerful
We define a comprehensive style for all possible combinations of the Nix expression language.
-
✔️ Reliable
High coverage, battle tested.
From Nix's eyes, code is just the same. 2
-
✔️ Beautiful
Beauty is subjective, right?
We started from the wisdom of the crowd, which comes in big part from the 2.3 million lines of code of Nixpkgs. Then we applied the feedback of developers who have used Nix on a day to day basis for several years.
-
✔️ Transparent
You won't notice the formatter after a while.
Humans care about the content, machines about the style!
-
✔️ Native
We integrate with common code editors:
- Alejandra extension for Visual Studio Code
- doom-emacs configuration:
(set-formatter! 'alejandra "alejandra --quiet" :modes '(nix-mode))
Getting started
On the web editor
Please visit: kamadorueda.github.io/alejandra.
Prebuilt binaries
You can download a binary for your platform:
- aarch64-unknown-linux-musl
- armv6l-unknown-linux-musleabihf
- armv7l-unknown-linux-musleabihf
- i686-unknown-linux-musl
- x86_64-unknown-linux-musl
Make it executable ($ chmod +x
)
and run Alejandra with:
$ ./alejandra --help
or:
$ /path/to/alejandra --help
Nixpkgs
FromPlease visit: search.nixos.org/packages?query=alejandra.
Nix installation
-
Nix with Flakes:
$ nix profile install github:kamadorueda/alejandra/1.1.0
-
Nix stable:
Pick one depending on your platform:
$ nix-env -ivA aarch64-darwin -f https://github.com/kamadorueda/alejandra/tarball/1.1.0 $ nix-env -ivA aarch64-linux -f https://github.com/kamadorueda/alejandra/tarball/1.1.0 $ nix-env -ivA x86_64-darwin -f https://github.com/kamadorueda/alejandra/tarball/1.1.0 $ nix-env -ivA x86_64-linux -f https://github.com/kamadorueda/alejandra/tarball/1.1.0
Then run Alejandra with:
$ alejandra --help
NixOS installation
-
Nix with Flakes:
{ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; alejandra.url = "github:kamadorueda/alejandra/1.1.0"; alejandra.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = {alejandra, nixpkgs, ...}: { nixosConfigurations = { example = nixpkgs.lib.nixosSystem rec { # We support: aarch64-darwin, aarch64-linux, x86_64-darwin, x86_64-linux system = "x86_64-linux"; modules = [ { environment.systemPackages = [alejandra.defaultPackage.${system}]; } # Import your other modules here # ./path/to/my/module.nix # ... ]; }; }; }; }
-
Nix stable:
let alejandra = (import (builtins.fetchTarball { url = "https://github.com/kamadorueda/alejandra/tarball/1.1.0"; sha256 = "0000000000000000000000000000000000000000000000000000"; })) # Pick one from: aarch64-darwin, aarch64-linux, x86_64-darwin, x86_64-linux .x86_64-linux .outPath; in { environment.systemPackages = [alejandra]; }
Do I need to configure anything?
- No.
Discussion
Cool libraries
Alternatives
See why Alejandra was created and a comparison between alternatives here.
Versioning
We use semver to version Alejandra.
Our public API consists of:
- The formatting rules (a.k.a. the style).
- The CLI tool (
$ alejandra
), command line flags, positional arguments, exit codes, and stdout.
Changelog
Please see: CHANGELOG.md.
Footnotes
Footnotes
-
Running on a machine with:
- CPU: 8 physical x Intel(R) Core(TM) i7-10700K, 16 logical
- MHz: 3800.00
- BogoMips: 7599.80
- Cache Size: 16384 KB
Using:
# x86_64-unknown-linux-gnu $ time alejandra --threads $threads /path/to/nixpkgs
Results:
$threads Seconds 1 13.4 2 6.9 4 3.6 8 2.6 16 3.1 -
The methodology to claim this is:
-
Checkout Nixpkgs and run:
$ nix-env -qaP --json --drv-path > before
-
Now format with Alejandra and run:
$ nix-env -qaP --json --drv-path > after
As of 2022-02-21, there are 46 differences in a set of 36314 derivations because of things like this:
goDeps = ./deps.nix;
Since
./deps.nix
was also formatted you get a semantical difference.This is something that should be solved on Nixpkgs and not a bug in Alejandra. For example:
↩ -