diff --git a/flake.lock b/flake.lock index 919115594d7..af3c89c49d7 100644 --- a/flake.lock +++ b/flake.lock @@ -16,25 +16,6 @@ "type": "github" } }, - "flake-parts": { - "inputs": { - "nixpkgs-lib": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1748821116, - "narHash": "sha256-F82+gS044J1APL0n4hH50GYdPRv/5JWm34oCJYmVKdE=", - "rev": "49f0870db23e8c1ca0b5259734a02cd9e1e371a1", - "revCount": 377, - "type": "tarball", - "url": "https://api.flakehub.com/f/pinned/hercules-ci/flake-parts/0.1.377%2Brev-49f0870db23e8c1ca0b5259734a02cd9e1e371a1/01972f28-554a-73f8-91f4-d488cc502f08/source.tar.gz" - }, - "original": { - "type": "tarball", - "url": "https://flakehub.com/f/hercules-ci/flake-parts/0.1" - } - }, "git-hooks-nix": { "inputs": { "flake-compat": "flake-compat", @@ -104,7 +85,6 @@ }, "root": { "inputs": { - "flake-parts": "flake-parts", "git-hooks-nix": "git-hooks-nix", "nixpkgs": "nixpkgs", "nixpkgs-23-11": "nixpkgs-23-11", diff --git a/flake.nix b/flake.nix index 967f8d8c397..f33216e482c 100644 --- a/flake.nix +++ b/flake.nix @@ -7,10 +7,8 @@ inputs.nixpkgs-23-11.url = "github:NixOS/nixpkgs/a62e6edd6d5e1fa0329b8653c801147986f8d446"; # dev tooling - inputs.flake-parts.url = "https://flakehub.com/f/hercules-ci/flake-parts/0.1"; inputs.git-hooks-nix.url = "https://flakehub.com/f/cachix/git-hooks.nix/0.1.941"; # work around https://github.com/NixOS/nix/issues/7730 - inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; inputs.git-hooks-nix.inputs.nixpkgs.follows = "nixpkgs"; # work around 7730 and https://github.com/NixOS/nix/issues/7807 inputs.git-hooks-nix.inputs.gitignore.follows = ""; @@ -75,17 +73,17 @@ forAllStdenvs = lib.genAttrs stdenvs; - # We don't apply flake-parts to the whole flake so that non-development attributes - # load without fetching any development inputs. - devFlake = inputs.flake-parts.lib.mkFlake { inherit inputs; } { - imports = [ ./maintainers/flake-module.nix ]; - systems = lib.subtractLists crossSystems systems; - perSystem = - { system, ... }: - { - _module.args.pkgs = nixpkgsFor.${system}.native; + # Pre-commit hooks configuration + preCommitHooksFor = + system: + let + pkgs = nixpkgsFor.${system}.native; + hookSettings = import ./packaging/pre-commit-hook-settings.nix { + inherit pkgs lib; + src = self; }; - }; + in + inputs.git-hooks-nix.lib.${system}.run hookSettings; # Memoize nixpkgs for different platforms for efficiency. nixpkgsFor = forAllSystems ( @@ -345,7 +343,9 @@ } )).componentTests ) - // devFlake.checks.${system} or { } + // { + pre-commit-check = preCommitHooksFor system; + } ); packages = forAllSystems ( @@ -494,7 +494,7 @@ devShells = let - makeShell = import ./packaging/dev-shell.nix { inherit lib devFlake; }; + makeShell = import ./packaging/dev-shell.nix { inherit lib preCommitHooksFor; }; prefixAttrs = prefix: lib.concatMapAttrs (k: v: { "${prefix}-${k}" = v; }); in forAllSystems ( diff --git a/maintainers/flake-module.nix b/maintainers/flake-module.nix deleted file mode 100644 index 8dcff9c63f0..00000000000 --- a/maintainers/flake-module.nix +++ /dev/null @@ -1,114 +0,0 @@ -{ - lib, - getSystem, - inputs, - ... -}: - -{ - imports = [ - inputs.git-hooks-nix.flakeModule - ]; - - perSystem = - { config, pkgs, ... }: - { - - # https://flake.parts/options/git-hooks-nix#options - pre-commit.settings = { - hooks = { - # Conflicts are usually found by other checks, but not those in docs, - # and potentially other places. - check-merge-conflicts.enable = true; - # built-in check-merge-conflicts seems ineffective against those produced by mergify backports - check-merge-conflicts-2 = { - enable = true; - entry = "${pkgs.writeScript "check-merge-conflicts" '' - #!${pkgs.runtimeShell} - conflicts=false - for file in "$@"; do - if grep --with-filename --line-number -E '^>>>>>>> ' -- "$file"; then - conflicts=true - fi - done - if $conflicts; then - echo "ERROR: found merge/patch conflicts in files" - exit 1 - fi - ''}"; - }; - meson-format = - let - meson = pkgs.meson.overrideAttrs { - doCheck = false; - doInstallCheck = false; - patches = [ - (pkgs.fetchpatch { - url = "https://github.com/mesonbuild/meson/commit/38d29b4dd19698d5cad7b599add2a69b243fd88a.patch"; - hash = "sha256-PgPBvGtCISKn1qQQhzBW5XfknUe91i5XGGBcaUK4yeE="; - }) - ]; - }; - in - { - enable = true; - files = "(meson.build|meson.options)$"; - entry = "${pkgs.writeScript "format-meson" '' - #!${pkgs.runtimeShell} - for file in "$@"; do - ${lib.getExe meson} format -ic ${../meson.format} "$file" - done - ''}"; - }; - nixfmt-rfc-style = { - enable = true; - 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 - package = pkgs.llvmPackages_latest.clang-tools; - excludes = [ - # We don't want to format test data - # ''tests/(?!nixos/).*\.nix'' - ''^src/[^/]*-tests/data/.*$'' - - # Don't format vendored code - ''^doc/manual/redirects\.js$'' - ''^doc/manual/theme/highlight\.js$'' - ]; - }; - shellcheck = { - enable = true; - }; - }; - }; - }; - - # We'll be pulling from this in the main flake - flake.getSystem = getSystem; -} diff --git a/packaging/dev-shell.nix b/packaging/dev-shell.nix index c4fc70511c1..3a338490743 100644 --- a/packaging/dev-shell.nix +++ b/packaging/dev-shell.nix @@ -1,6 +1,6 @@ { lib, - devFlake, + preCommitHooksFor, }: { pkgs }: @@ -11,7 +11,7 @@ pkgs.nixComponents2.nix-util.overrideAttrs ( let stdenv = pkgs.nixDependencies2.stdenv; buildCanExecuteHost = stdenv.buildPlatform.canExecute stdenv.hostPlatform; - modular = devFlake.getSystem stdenv.buildPlatform.system; + modular = preCommitHooksFor stdenv.buildPlatform.system; transformFlag = prefix: flag: assert builtins.isString flag; @@ -73,9 +73,7 @@ pkgs.nixComponents2.nix-util.overrideAttrs ( env = { # For `make format`, to work without installing pre-commit - _NIX_PRE_COMMIT_HOOKS_CONFIG = "${(pkgs.formats.yaml { }).generate "pre-commit-config.yaml" - modular.pre-commit.settings.rawConfig - }"; + _NIX_PRE_COMMIT_HOOKS_CONFIG = modular.shellHook; } // lib.optionalAttrs stdenv.hostPlatform.isLinux { CC_LD = "mold"; @@ -115,8 +113,7 @@ pkgs.nixComponents2.nix-util.overrideAttrs ( pkgs.buildPackages.gnused pkgs.buildPackages.shellcheck pkgs.buildPackages.changelog-d - modular.pre-commit.settings.package - (pkgs.writeScriptBin "pre-commit-hooks-install" modular.pre-commit.settings.installationScript) + pkgs.buildPackages.pre-commit pkgs.buildPackages.nixfmt-rfc-style pkgs.buildPackages.shellcheck pkgs.buildPackages.gdb diff --git a/packaging/pre-commit-hook-settings.nix b/packaging/pre-commit-hook-settings.nix new file mode 100644 index 00000000000..d123a4f02b3 --- /dev/null +++ b/packaging/pre-commit-hook-settings.nix @@ -0,0 +1,80 @@ +{ + pkgs, + lib, + src, +}: + +{ + inherit src; + + hooks = { + check-merge-conflicts.enable = true; + check-merge-conflicts-2 = { + enable = true; + entry = "${pkgs.writeScript "check-merge-conflicts" '' + #!${pkgs.runtimeShell} + conflicts=false + for file in "$@"; do + if grep --with-filename --line-number -E '^>>>>>>> ' -- "$file"; then + conflicts=true + fi + done + if $conflicts; then + echo "ERROR: found merge/patch conflicts in files" + exit 1 + fi + ''}"; + }; + meson-format = + let + meson = pkgs.meson.overrideAttrs { + doCheck = false; + doInstallCheck = false; + patches = [ + (pkgs.fetchpatch { + url = "https://github.com/mesonbuild/meson/commit/38d29b4dd19698d5cad7b599add2a69b243fd88a.patch"; + hash = "sha256-PgPBvGtCISKn1qQQhzBW5XfknUe91i5XGGBcaUK4yeE="; + }) + ]; + }; + in + { + enable = true; + files = "(meson.build|meson.options)$"; + entry = "${pkgs.writeScript "format-meson" '' + #!${pkgs.runtimeShell} + for file in "$@"; do + ${lib.getExe meson} format -ic ${../meson.format} "$file" + done + ''}"; + }; + nixfmt-rfc-style = { + enable = true; + excludes = [ + ''^tests/functional/lang/parse-.*\.nix$'' + ''^tests/functional/lang/eval-okay-curpos\.nix$'' + ''^tests/functional/lang/.*comment.*\.nix$'' + ''^tests/functional/lang/.*newline.*\.nix$'' + ''^tests/functional/lang/.*eol.*\.nix$'' + ''^tests/functional/shell.shebang\.nix$'' + ''^tests/functional/lang/eval-okay-ind-string\.nix$'' + ''^tests/functional/lang/eval-okay-deprecate-cursed-or\.nix$'' + ''^tests/functional/lang/eval-okay-attrs5\.nix$'' + ''^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; + package = pkgs.llvmPackages_latest.clang-tools; + excludes = [ + ''^src/[^/]*-tests/data/.*$'' + ''^doc/manual/redirects\.js$'' + ''^doc/manual/theme/highlight\.js$'' + ]; + }; + shellcheck.enable = true; + }; +}