From 457b222b7a1c9a62445803409e2a740695f45f91 Mon Sep 17 00:00:00 2001 From: Benedikt Rips Date: Sat, 12 Apr 2025 23:59:32 +0200 Subject: [PATCH 1/3] refactor(typos): structured attrs configuration --- modules/hooks.nix | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 18fb4d1d..63591748 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -4,6 +4,8 @@ let cfg = config; inherit (lib) flatten mapAttrs mapAttrsToList mkDefault mkOption mkRemovedOptionModule mkRenamedOptionModule types; + toml = pkgs.formats.toml { }; + cargoManifestPathArg = lib.optionalString (settings.rust.cargoManifestPath != null) @@ -33,6 +35,9 @@ in (mkRemovedOptionModule [ "settings" "yamllint" "relaxed" ] '' This option has been removed. Use `hooks.yamllint.settings.preset = "relaxed"`. '') + (mkRemovedOptionModule [ "typos" "settings" "configuration" ] '' + This option has been removed. Use `hooks.typos.settings.config`, which is a structured attrs, instead. + '') ] # Manually rename options that had a package or a config option ++ flatten (mapAttrsToList (name: map (o: mkRenamedOptionModule [ "settings" name o ] [ "hooks" name "settings" o ])) { @@ -1789,33 +1794,30 @@ in description = "Whether to search binary files."; default = false; }; + color = mkOption { type = types.enum [ "auto" "always" "never" ]; description = "When to use generate output."; default = "auto"; }; - configuration = - mkOption { - type = types.str; - description = "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath` gets ignored."; - default = ""; - example = '' - [files] - ignore-dot = true - - [default] - binary = false - [type.py] - extend-glob = [] - ''; + config = + mkOption { + type = toml.type; + description = "Configuration as in https://github.com/crate-ci/typos/blob/master/docs/reference.md."; + default = { }; + example = { + files.ignore-dot = true; + default.binary = false; + type.py.extend-glob = [ ]; + }; }; configPath = mkOption { type = types.str; - description = "Path to a custom config file."; + description = "Path to a custom config file. Ignored if `typos.settings.config` is set."; default = ""; example = ".typos.toml"; }; @@ -3988,16 +3990,15 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm package = tools.typos; entry = let - # Concatenate config in config file with section for ignoring words generated from list of words to ignore - configuration = "${hooks.typos.settings.configuration}" + lib.strings.optionalString (hooks.typos.settings.ignored-words != [ ]) "\n\[default.extend-words\]" + lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") hooks.typos.settings.ignored-words; - configFile = builtins.toFile "typos-config.toml" configuration; + inherit (hooks.typos.settings) config; + configFile = toml.generate "typos-config.toml" config; cmdArgs = mkCmdArgs (with hooks.typos.settings; [ [ binary "--binary" ] [ (color != "auto") "--color ${color}" ] - [ (configuration != "") "--config ${configFile}" ] - [ (configPath != "" && configuration == "") "--config ${configPath}" ] + [ (config != { }) "--config ${configFile}" ] + [ (configPath != "" && config == { }) "--config ${configPath}" ] [ diff "--diff" ] [ (exclude != "") "--exclude ${exclude} --force-exclude" ] [ (format != "long") "--format ${format}" ] @@ -4012,6 +4013,9 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm ]); in "${hooks.typos.package}/bin/typos ${cmdArgs}"; + settings.config.default.extend-words = lib.mkIf + (hooks.typos.settings.ignored-words != [ ]) + (lib.genAttrs hooks.typos.settings.ignored-words lib.id); types = [ "text" ]; }; typstfmt = { From 19f7aebd54754379523c8ea83f9e1b870335eec4 Mon Sep 17 00:00:00 2001 From: Benedikt Rips Date: Sun, 13 Apr 2025 07:41:30 +0200 Subject: [PATCH 2/3] feat(typos): set `--force-exclude` by default --- modules/hooks.nix | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 63591748..897ed372 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -1837,6 +1837,14 @@ in example = "*.nix"; }; + force-exclude = + mkOption { + type = types.bool; + description = "Respect excluded files even for paths passed explicitly."; + default = true; + example = false; + }; + format = mkOption { type = types.enum [ "silent" "brief" "long" "json" ]; @@ -4000,7 +4008,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm [ (config != { }) "--config ${configFile}" ] [ (configPath != "" && config == { }) "--config ${configPath}" ] [ diff "--diff" ] - [ (exclude != "") "--exclude ${exclude} --force-exclude" ] + [ (exclude != "") "--exclude ${exclude}" ] + [ force-exclude " --force-exclude" ] [ (format != "long") "--format ${format}" ] [ hidden "--hidden" ] [ (locale != "en") "--locale ${locale}" ] From b635a9e742fc54bb73d2a2867282dbdfffb90d57 Mon Sep 17 00:00:00 2001 From: Benedikt Rips Date: Sun, 13 Apr 2025 08:33:26 +0200 Subject: [PATCH 3/3] feat(typos): multiple exclude globs --- modules/hooks.nix | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 897ed372..320124b9 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -1831,10 +1831,10 @@ in exclude = mkOption { - type = types.str; - description = "Ignore files and directories matching the glob."; - default = ""; - example = "*.nix"; + type = with types; coercedTo str (s: [ s ]) (listOf str); + description = "Ignore files and directories matching one of the globs."; + default = [ ]; + example = [ "*.nix" ]; }; force-exclude = @@ -3998,8 +3998,9 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm package = tools.typos; entry = let - inherit (hooks.typos.settings) config; + inherit (hooks.typos.settings) config exclude; configFile = toml.generate "typos-config.toml" config; + excludeFlags = lib.map (glob: "--exclude ${glob}") exclude; cmdArgs = mkCmdArgs (with hooks.typos.settings; [ @@ -4008,7 +4009,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm [ (config != { }) "--config ${configFile}" ] [ (configPath != "" && config == { }) "--config ${configPath}" ] [ diff "--diff" ] - [ (exclude != "") "--exclude ${exclude}" ] + ([ (exclude != [ ]) ] ++ excludeFlags) [ force-exclude " --force-exclude" ] [ (format != "long") "--format ${format}" ] [ hidden "--hidden" ]