Skip to content
Nix code formatter for nixpkgs [maintainer=@zimbatm]
Rust Nix HTML Shell
Branch: master
Clone or download
Latest commit 28b0def Aug 23, 2019

README.md

nixpkgs-fmt

Build Status built with nix

STATUS: alpha

This project's goal is to provide a nix code formatter that would be applied on nixpkgs. Ideally automatically with a tool like ofborg.

Demo

Try nixpkgs-fmt in your browser and submit code samples: https://nix-community.github.io/nixpkgs-fmt/

Design decisions

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.

Usage

$ 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

pre-commit hook

This project can also be installed as a pre-commit hook.

Add to your project's .pre-commit-config.yaml:

-   repo: https://github.com/nix-community/nixpkgs-fmt
    rev: master
    hooks:
    -   id: nixpkgs-fmt

Make sure to have rust available in your environment.

Then run pre-commit install-hooks

Development

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.

Then use cargo run to build and run the software.

Running Fuzzer

$ 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"
  • fmt is the name of the target in ./fuzz/Cargo.toml

Fuzzer will run indefinitelly or until it finds a crash. The crashing input is written to fuzz/artifacts directory. Commit this crash- file, and it will be automatically tested by a unit-test.

Documentation

Related projects

  • 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-fmt
  • nix-format - Emacs-based Nix formatter
  • nix-beautify
  • nix-linter
  • canonix - Nix formatter prototype written in Haskell using tree-sitter-nix grammar.

Discussions

You can’t perform that action at this time.