Skip to content

Commit

Permalink
README: Add installation and integration docs
Browse files Browse the repository at this point in the history
Closes #214
  • Loading branch information
dasJ committed Aug 29, 2024
1 parent 5dc9b10 commit 965c5c5
Showing 1 changed file with 111 additions and 11 deletions.
122 changes: 111 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,119 @@ The established standard Nix formatting differs considerably from the original o

For more details, see the [RFC implementation tracking issue](https://github.com/NixOS/nixfmt/issues/153).

## Installation
## Installation And Usage Instructions

- `nixfmt` is in nixpkgs master as of 2019-09-04:
### nixpkgs/NixOS

nix-env -iA nixpkgs.nixfmt

- To get the most recent version, install from master:

nix-env -f https://github.com/NixOS/nixfmt/archive/master.tar.gz -i

- Nix with flakes

nix profile install github:NixOS/nixfmt
`nixfmt` was used as the basis for the official Nix formatter with a standardized formatting.
The new formatting differs considerably from the original one.
This is why two packages are in nixpkgs: `nixfmt-classic` with the non-standardized formatting and `nixfmt-rfc-style`.

So installing this `nixfmt` is as simple as adding to the system packages:

```nix
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.nixfmt-rfc-style ];
}
```

### From the repository

It's also possible to install `nixfmt` directly from the repository using `nix-env`.
This allows the newest changes to be used, which may not be equal to the `nixfmt` version used to format nixpkgs, so this should be done with care!
Also, updates are not done automatically (as with the system packages).

```
nix-env -f https://github.com/NixOS/nixfmt/archive/master.tar.gz -i
```

### nix fmt

[nix fmt](https://nix.dev/manual/nix/latest/command-ref/new-cli/nix3-fmt) (which is a flakes-only feature) can be configured by adding the following to `flake.nix` (assuming a `nixpkgs` input exists):
```
{
outputs = { nixpkgs, self }: {
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt-rfc-style;
};
}
```

### treefmt

[treefmt](https://github.com/numtide/treefmt) can be used to format repositories consisting of different languages with one command.
A possible configuration for `nixfmt` in `.treefmt.toml` looks like this:
```toml
[formatter.nixfmt-rfc-style]
command = "nixfmt"
includes = ["*.nix"]
```

This only works when `nixfmt-rfc-style` is installed (see above for installation instructions).

`treefmt` can be integrated into text editors and CI to ensure consistent formatting for all filetypes.

### treefmt-nix

[treefmt-nix](https://github.com/numtide/treefmt-nix) automatically configures the correct packages and formatters using a Nix configuration and has native support for `nixfmt`:

```nix
# ...
treefmt-nix.mkWrapper nixpkgs {
# ... other options ...
programs.nixfmt-rfc-style.enable = true;
}
```

More information about configuration can be found in [the official README](https://github.com/numtide/treefmt-nix?tab=readme-ov-file#integration-into-nix).

### neovim + nixd

```lua
local nvim_lsp = require("lspconfig")
nvim_lsp.nixd.setup({
settings = {
nixd = {
formatting = {
command = { "nixfmt" },
},
},
},
})
```

This only works when `nixfmt-rfc-style` is installed (see above for installation instructions).

### neovim + nil

```lua
local nvim_lsp = require("lspconfig")
nvim_lsp.nil_ls.setup({
settings = {
['nil'] = {
formatting = {
command = { "nixfmt" },
},
},
},
})
```

This only works when `nixfmt-rfc-style` is installed (see above for installation instructions).

### neovim + none-ls

```lua
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.formatting.nixfmt,
},
})
```

This only works when `nixfmt-rfc-style` is installed (see above for installation instructions).

## Development

Expand Down

0 comments on commit 965c5c5

Please sign in to comment.