Home Explore Blog CI



nix

1st chunk of `src/nix/formatter-run.md`
73ee22ff37e1cc1783a355b21e41395104d4e5ce9c35c44900000001000002e0
R""(

# Description

`nix fmt` (an alias for `nix formatter run`) calls the formatter specified in the flake.

Flags can be forwarded to the formatter by using `--` followed by the flags.

Any arguments will be forwarded to the formatter. Typically these are the files to format.

The environment variable `PRJ_ROOT` (according to [prj-spec](https://github.com/numtide/prj-spec))
will be set to the absolute path to the directory containing the closest parent `flake.nix`
relative to the current directory.


# Example

To use the [official Nix formatter](https://github.com/NixOS/nixfmt):

```nix
# flake.nix
{
  outputs = { nixpkgs, self }: {
    formatter.x86_64-linux = nixpkgs.legacyPackages.${system}.nixfmt-tree;
  };
}
```

)""

Title: Nix FMT: Formatting Nix Files with Flakes
Summary
The `nix fmt` command, aliased to `nix formatter run`, uses the formatter defined in the flake to format Nix files. It allows forwarding flags and arguments to the formatter. The `PRJ_ROOT` environment variable is set to the root directory of the project based on the location of the `flake.nix` file. An example demonstrates how to configure a flake to use the official Nix formatter (nixfmt).