From ba6425a7d0dad99f4fd03e17db3d4c29df62dd70 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 24 Jan 2025 13:16:36 +0100 Subject: [PATCH] dev: Configure nixfmt (rfc style) --- doc/manual/source/development/building.md | 29 +++++++++++++ flake.lock | 52 +++++++++++++++++++++++ flake.nix | 3 +- maintainers/flake-module.nix | 30 ++++++++++++- packaging/dev-shell.nix | 3 +- 5 files changed, 114 insertions(+), 3 deletions(-) diff --git a/doc/manual/source/development/building.md b/doc/manual/source/development/building.md index 9a8ed8346..a60543f4d 100644 --- a/doc/manual/source/development/building.md +++ b/doc/manual/source/development/building.md @@ -278,6 +278,8 @@ You may run the formatters as a one-off using: ./maintainers/format.sh ``` +### Pre-commit hooks + If you'd like to run the formatters before every commit, install the hooks: ``` @@ -292,3 +294,30 @@ If it fails, run `git add --patch` to approve the suggestions _and commit again_ To refresh pre-commit hook's config file, do the following: 1. Exit the development shell and start it again by running `nix develop`. 2. If you also use the pre-commit hook, also run `pre-commit-hooks-install` again. + +### VSCode + +Insert the following json into your `.vscode/settings.json` file to configure `nixfmt`. +This will be picked up by the _Format Document_ command, `"editor.formatOnSave"`, etc. + +```json +{ + "nix.formatterPath": "nixfmt", + "nix.serverSettings": { + "nixd": { + "formatting": { + "command": [ + "nixfmt" + ], + }, + }, + "nil": { + "formatting": { + "command": [ + "nixfmt" + ], + }, + }, + }, +} +``` diff --git a/flake.lock b/flake.lock index ce484a67a..e45f0fb98 100644 --- a/flake.lock +++ b/flake.lock @@ -36,6 +36,24 @@ "type": "github" } }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "git-hooks-nix": { "inputs": { "flake-compat": [], @@ -61,6 +79,24 @@ "type": "github" } }, + "nixfmt": { + "inputs": { + "flake-utils": "flake-utils" + }, + "locked": { + "lastModified": 1736283758, + "narHash": "sha256-hrKhUp2V2fk/dvzTTHFqvtOg000G1e+jyIam+D4XqhA=", + "owner": "NixOS", + "repo": "nixfmt", + "rev": "8d4bd690c247004d90d8554f0b746b1231fe2436", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixfmt", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1734359947, @@ -114,10 +150,26 @@ "flake-compat": "flake-compat", "flake-parts": "flake-parts", "git-hooks-nix": "git-hooks-nix", + "nixfmt": "nixfmt", "nixpkgs": "nixpkgs", "nixpkgs-23-11": "nixpkgs-23-11", "nixpkgs-regression": "nixpkgs-regression" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 64391efa4..c6df63c5e 100644 --- a/flake.nix +++ b/flake.nix @@ -17,6 +17,7 @@ # work around 7730 and https://github.com/NixOS/nix/issues/7807 inputs.git-hooks-nix.inputs.flake-compat.follows = ""; inputs.git-hooks-nix.inputs.gitignore.follows = ""; + inputs.nixfmt.url = "github:NixOS/nixfmt"; outputs = inputs@{ self, nixpkgs, nixpkgs-regression, ... }: @@ -301,7 +302,7 @@ }); devShells = let - makeShell = import ./packaging/dev-shell.nix { inherit lib devFlake; }; + makeShell = import ./packaging/dev-shell.nix { inherit inputs lib devFlake; }; prefixAttrs = prefix: lib.concatMapAttrs (k: v: { "${prefix}-${k}" = v; }); in forAllSystems (system: diff --git a/maintainers/flake-module.nix b/maintainers/flake-module.nix index fcf370b71..93fd3675e 100644 --- a/maintainers/flake-module.nix +++ b/maintainers/flake-module.nix @@ -31,6 +31,35 @@ touch $out ''}"; }; + nixfmt-rfc-style = { + enable = true; + package = inputs.nixfmt.packages.${pkgs.hostPlatform.system}.default; + excludes = [ + # Invalid + ''^tests/functional/lang/parse-.*\.nix$'' + + # Formatting-sensitive + ''^tests/functional/lang/eval-okay-curpos\.nix$'' + ''^tests/functional/lang/.*comment.*\.nix$'' + ''^tests/functional/lang/.*newline.*\.nix$'' + ''^tests/functional/lang/.*eol.*\.nix$'' + + # Syntax tests + ''^tests/functional/shell.shebang\.nix$'' + ''^tests/functional/lang/eval-okay-ind-string\.nix$'' + + # Not supported by nixfmt + ''^tests/functional/lang/eval-okay-deprecate-cursed-or\.nix$'' + ''^tests/functional/lang/eval-okay-attrs5\.nix$'' + + # More syntax tests + # These tests, or parts of them, should have been parse-* test cases. + ''^tests/functional/lang/eval-fail-eol-2\.nix$'' + ''^tests/functional/lang/eval-fail-path-slash\.nix$'' + ''^tests/functional/lang/eval-fail-toJSON-non-utf-8\.nix$'' + ''^tests/functional/lang/eval-fail-set\.nix$'' + ]; + }; clang-format = { enable = true; # https://github.com/cachix/git-hooks.nix/pull/532 @@ -660,7 +689,6 @@ ''^src/libutil-tests/data/git/check-data\.sh$'' ]; }; - # TODO: nixfmt, https://github.com/NixOS/nixfmt/issues/153 }; }; }; diff --git a/packaging/dev-shell.nix b/packaging/dev-shell.nix index 30ac518d5..b35a48f65 100644 --- a/packaging/dev-shell.nix +++ b/packaging/dev-shell.nix @@ -1,4 +1,4 @@ -{ lib, devFlake }: +{ lib, inputs, devFlake }: { pkgs }: @@ -108,6 +108,7 @@ in { modular.pre-commit.settings.package (pkgs.writeScriptBin "pre-commit-hooks-install" modular.pre-commit.settings.installationScript) + inputs.nixfmt.packages.${pkgs.hostPlatform.system}.default ] # TODO: Remove the darwin check once # https://github.com/NixOS/nixpkgs/pull/291814 is available