From e0aa320384a8e47ffdd3ef76c43da83eb19d120f Mon Sep 17 00:00:00 2001 From: Sander Date: Sun, 13 Oct 2024 03:42:09 +0400 Subject: [PATCH] hooks: fix rome and nixfmt migrations I've decided to go with the "duplicate the thing and move on" migration approach. Here's why: 1. `hooks.nix` doesn't operate at the submodule level. Migrations need to be added to the hooks submodule in `pre-commit.nix`, which is a bit of a mess. 2. `mkAliasOptionModule` and `mkRenamedOptionModule` work (with the above caveat). However, the former makes it impossible AFAIKT to implement warnings because of the two-way aliasing, and the latter will always spew warnings when we eval `enable`. 3. Once we split up `hooks.nix` and have each hook in a separate module, I think we can revisit hook migrations. --- modules/hooks.nix | 62 ++++++++++++++++++++++++++++++++++++++---- modules/pre-commit.nix | 4 +-- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index d6bfb245..c1ecf65c 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -851,7 +851,7 @@ in }; }; nixfmt = mkOption { - description = "Deprecated nixfmt hook"; + description = "Deprecated nixfmt hook. Use nixfmt-classic or nixfmt-rfc-style instead."; visible = false; type = types.submodule { imports = [ hookModule ]; @@ -1372,6 +1372,37 @@ in }; }; }; + rome = mkOption { + description = "Deprecated rome hook. Use biome instead."; + visible = false; + type = types.submodule { + imports = [ hookModule ]; + options.settings = { + binPath = + mkOption { + type = types.nullOr types.path; + description = "`biome` binary path. E.g. if you want to use the `biome` in `node_modules`, use `./node_modules/.bin/biome`."; + default = null; + defaultText = "\${tools.biome}/bin/biome"; + }; + + write = + mkOption { + type = types.bool; + description = "Whether to edit files inplace."; + default = true; + }; + + configPath = mkOption { + type = types.str; + description = "Path to the configuration JSON file"; + # an empty string translates to use default configuration of the + # underlying biome binary (i.e biome.json if exists) + default = ""; + }; + }; + }; + }; rustfmt = mkOption { description = '' Additional rustfmt settings @@ -2986,9 +3017,14 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol builtins.toString script; files = "\\.nix$"; }; - # nixfmt was renamed to nixfmt-classic. - # The hook has been deprecated to free up the name for when the new RFC-style nixfmt becomes stable. - nixfmt = nixfmt-classic; + nixfmt = + { + name = "nixfmt-deprecated"; + description = "Deprecated Nix code prettifier. Use nixfmt-classic."; + package = tools.nixfmt; + entry = "${hooks.nixfmt.package}/bin/nixfmt ${lib.optionalString (hooks.nixfmt.settings.width != null) "--width=${toString hooks.nixfmt.settings.width}"}"; + files = "\\.nix$"; + }; nixfmt-classic = { name = "nixfmt-classic"; @@ -3329,7 +3365,23 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol "${hooks.ripsecrets.package}/bin/ripsecrets ${cmdArgs}"; types = [ "text" ]; }; - rome = biome; + rome = + { + name = "rome-deprecated"; + description = ""; + types_or = [ "javascript" "jsx" "ts" "tsx" "json" ]; + package = tools.biome; + entry = + let + binPath = migrateBinPathToPackage hooks.rome "/bin/biome"; + cmdArgs = + mkCmdArgs [ + [ (hooks.rome.settings.write) "--apply" ] + [ (hooks.rome.settings.configPath != "") "--config-path ${hooks.rome.settings.configPath}" ] + ]; + in + "${binPath} check ${cmdArgs}"; + }; ruff = { name = "ruff"; diff --git a/modules/pre-commit.nix b/modules/pre-commit.nix index 1a1701dd..60f4bd00 100644 --- a/modules/pre-commit.nix +++ b/modules/pre-commit.nix @@ -1,20 +1,18 @@ { config, lib, pkgs, hookModule, ... }: let inherit (lib) - attrNames boolToString concatStringsSep compare filterAttrs literalExample mapAttrsToList - mkIf mkOption types remove ; - inherit (pkgs) runCommand writeText git; + inherit (pkgs) runCommand git; cfg = config; install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks));