From 9c142b3ea03b0ab08829902087e3d592904c0a32 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Tue, 29 Apr 2025 10:07:18 +0200 Subject: [PATCH 1/2] move `nixfmt` instructions to FAQ all the interesting infromation is upstream, and there's no point in maintaining a permanently-outdated copy here. --- source/guides/faq.md | 7 ++++ source/guides/recipes/autoformatting.md | 56 ------------------------- source/guides/recipes/index.md | 1 - 3 files changed, 7 insertions(+), 57 deletions(-) delete mode 100644 source/guides/recipes/autoformatting.md diff --git a/source/guides/faq.md b/source/guides/faq.md index 7a9748240..7904a296d 100644 --- a/source/guides/faq.md +++ b/source/guides/faq.md @@ -2,6 +2,13 @@ ## Nix +### How to format Nix language code automatically? + +[`nixfmt`](https://github.com/NixOS/nixfmt) is the official formatter for {term}`Nix language` code. +Please refer to its source repository for installation instructions. + +`nixfmt` is [used to format all code](https://github.com/NixOS/nixpkgs/blob/master/ci/default.nix) in {term}`Nixpkgs`. + ### How to convert between paths and strings in the Nix language? See the Nix reference manual on [string interpolation](https://nix.dev/manual/nix/2.19/language/string-interpolation) and [operators on paths and strings](https://nix.dev/manual/nix/2.19/language/operators#string-concatenation) diff --git a/source/guides/recipes/autoformatting.md b/source/guides/recipes/autoformatting.md deleted file mode 100644 index 739a151c6..000000000 --- a/source/guides/recipes/autoformatting.md +++ /dev/null @@ -1,56 +0,0 @@ -(autoformatting)= -# Autoformatting - -[`nixfmt`](https://github.com/NixOS/nixfmt) is the official Nix autoformatter. -Official tooling currently does not use nixfmt out of the box. Subscribe to -[NixOS/nix PR #11252](https://github.com/NixOS/nix/pull/11252) for updates on that effort. - -Because `nixfmt` doesn't support formatting whole directory trees, you need -additional tooling such as `treefmt`. The `nixfmt-tree` package provides a -`treefmt` pre-configured to run `nixfmt` on all nix files in your project. Just -add it to your shell: - -```nix -mkShell { - packages = [ pkgs.nixfmt-tree ]; -} -``` - -Note: this assumes you're project is in a git repository, and you wish to treat -the entire repo as your project to be formatted. - -If you need to configure any [treefmt options], or enable formatting other -(non-nix) files, you can use `treefmt.withConfig`: - -[treefmt options]: https://treefmt.com/latest/getting-started/configure/#global-options - -```nix -pkgs.treefmt.withConfig { - runtimeInputs = [ - pkgs.nixfmt-rfc-style - pkgs.ruff - ]; - - settings = { - # Customize detection of the root of the project. - tree-root-file = "flake.nix"; - - # Configure nixfmt for .nix files. - formatter.nixfmt = { - command = "nixfmt"; - includes = [ "*.nix" ]; - }; - - # And for .py file. - formatter.ruff = { - command = "ruff"; - options = [ "format" ]; - includes = [ "*.py" ]; - }; - }; -} -``` - -This can get a little tedious. -[treefmt-nix](https://github.com/numtide/treefmt-nix) has a big library of -preconfigured formatters, and provides a `check` derivation you can use in CI. diff --git a/source/guides/recipes/index.md b/source/guides/recipes/index.md index 6ccf17955..1246ebe65 100644 --- a/source/guides/recipes/index.md +++ b/source/guides/recipes/index.md @@ -11,5 +11,4 @@ Managing remote sources <./dependency-management.md> Python development environment <./python-environment.md> post-build-hook.md continuous-integration-github-actions.md -autoformatting.md ``` From c157c30321047bdf1dde0844d801c5af7939ba6d Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Fri, 9 May 2025 22:28:28 +0200 Subject: [PATCH 2/2] add redirect --- _redirects | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_redirects b/_redirects index 50359d7cc..d5a31b953 100644 --- a/_redirects +++ b/_redirects @@ -40,4 +40,4 @@ /manual/nix/unstable/* https://hydra.nixos.org/job/nix/master/manual/latest/download/1/manual/:splat 200 /manual/nix/development/* https://hydra.nixos.org/job/nix/master/manual/latest/download/1/manual/:splat 200 /tutorials/nixos/continuous-integration-github-actions /guides/recipes/continuous-integration-github-actions 301 - +/guides/recipes/autoformatting /guides/faq#how-to-format-nix-language-code-automatically 301