Skip to content

Commit

Permalink
hooks: fix rome and nixfmt migrations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sandydoo committed Oct 12, 2024
1 parent eb74e0b commit e0aa320
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
62 changes: 57 additions & 5 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 1 addition & 3 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
@@ -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));
Expand Down

0 comments on commit e0aa320

Please sign in to comment.