This project's goal is to provide a nix code formatter that would be applied on nixpkgs. Ideally automatically with a tool like ofborg.
Try nixpkgs-fmt in your browser and submit code samples: https://nix-community.github.io/nixpkgs-fmt/
Use Rust because ofborg is written in rust. Rust also has a small chance of being included in nix upstream.
Use an incremental and rule-based formatter (vs pretty-printing). This allows to format partial code and leave more expressivity to the developer. For example double new-lines can be used when the developer wants a section of code to stand out. The important part is to avoid discussions on indent and brackets alignment.
Favour mergeability. nixpkgs is seeing a lot of traffic. Spread things out vertically to minimize the chances of merge conflicts.
$ nixpkgs-fmt --help 2>&1 || true
nixpkgs-fmt 0.1 Format Nix code USAGE: nixpkgs-fmt [FLAGS] [FILE]... FLAGS: -h, --help Prints help information -i, --in-place Overwrite FILE in place --parse Show syntax tree instead of reformatting -V, --version Prints version information ARGS: <FILE>... File to reformat
This project can also be installed as a pre-commit hook.
Add to your project's
- repo: https://github.com/nix-community/nixpkgs-fmt rev: master hooks: - id: nixpkgs-fmt
Make sure to have rust available in your environment.
Install Rust and Cargo or run
nix-shell to load the project dependencies.
Install pre-commit and run
pre-commit install to
setup the git hooks on the repository. This will allow to keep the code nicely
formatted over time.
cargo run to build and run the software.
$ cargo install cargo-fuzz $ mkdir -p ./fuzz/corpus/fmt $ cp test_data/**.nix ./fuzz/corpus/fmt $ rustup run nightly -- cargo fuzz run fmt
or with nix:
$ nix-shell --run "cargo fuzz run fmt"
fmtis the name of the target in
Fuzzer will run indefinitelly or until it finds a crash.
The crashing input is written to
crash- file, and it will be automatically tested by a unit-test.
- hnix - Haskell implementation of Nix including a parser. The parser is not comment-preserving.
- rnix - Rust Nix parser based on rowan
- nix-lsp - Nix language server using rnix
- tree-sitter-nix - Tree Sitter is a forgiving parser used by Atom for on-the-fly syntax highlighting and others. This is a implementation for Nix.
- format-nix - A nix formatter using tree-sitter-nix.
- nixfmt - A nix formatter written in Haskell.
- nix-format - Emacs-based Nix formatter
- canonix - Nix formatter prototype written in Haskell using tree-sitter-nix grammar.