@@ -1115,10 +1115,24 @@ in
1115
1115
extend-glob = []
1116
1116
'' ;
1117
1117
} ;
1118
+ # Recommended way to specifcy a config, as this way, exludes can be
1119
+ # taken into account by pre-commit when running
1120
+ # `$ pre-commit run --all-files`.
1121
+ # We provide configFile and configPath for API compatibility with
1122
+ # previous versions and coherence with other hooks defined here.
1123
+ configFile =
1124
+ mkOption {
1125
+ type = types . path ;
1126
+ description = lib . mdDoc "Path to a typos config file (in Nix store)." ;
1127
+ default = null ;
1128
+ example = "./.typos.toml" ;
1129
+ } ;
1130
+ # It is recommended to use configFile instead, as it provides more
1131
+ # flexibility and a more correct/expected behaviour.
1118
1132
configPath =
1119
1133
mkOption {
1120
1134
type = types . str ;
1121
- description = lib . mdDoc "Path to a custom config file." ;
1135
+ description = lib . mdDoc "Path to a typos config file (not in Nix store) ." ;
1122
1136
default = "" ;
1123
1137
example = ".typos.toml" ;
1124
1138
} ;
@@ -2456,21 +2470,68 @@ in
2456
2470
entry = "${ settings . treefmt . package } /bin/treefmt --fail-on-change" ;
2457
2471
} ;
2458
2472
typos =
2473
+ let
2474
+ # Since we don't set "pass_filenames = false" for typos (see
2475
+ # upstream discussions [0]), we must ensure that pre-commit never
2476
+ # passes the files specified as excludes in `.typos.toml` to
2477
+ # `typos` as argument, if possible. Otherwise,
2478
+ # `$ pre-commit run typos --all-files` has another behaviour than
2479
+ # `$ typos .`, which is confusing and wrong.
2480
+ #
2481
+ # To not break anything, and to be compliant with the existing
2482
+ # API, we encourage users to provide the `.typos.toml` as Nix
2483
+ # path, so the excludes can be read via evaluation time.
2484
+ #
2485
+ # The `configAsFile` variable is the path where the configuration
2486
+ # file can be found by the `typos` utility when it executes in the
2487
+ # user environment. Not necessarily in Nix store.
2488
+ #
2489
+ # [0]: https://github.com/cachix/pre-commit-hooks.nix/pull/387#issuecomment-1893600631
2490
+ configAsFile =
2491
+ # Highest precedence.
2492
+ if settings . typos . configFile != null
2493
+ then ( toString settings . typos . configFile )
2494
+ # Secondary precedence.
2495
+ else if settings . typos . configPath != ""
2496
+ then settings . typos . configPath
2497
+ # Lowest precedence.
2498
+ else
2499
+ builtins . toFile "config.toml"
2500
+ # Concatenate config in config file with section for ignoring words
2501
+ # generated from list of words to ignore
2502
+ ( "${ settings . typos . config } " +
2503
+ lib . strings . optionalString ( settings . typos . ignored-words != [ ] ) "\n \[ default.extend-words\] " +
2504
+ lib . strings . concatMapStrings ( x : "\n ${ x } = \" ${ x } \" " ) settings . typos . ignored-words
2505
+ )
2506
+ ;
2507
+ # If the config file path is passed as Nix string and not as Nix
2508
+ # Path, we can't read it from Nix, unfortunately.
2509
+ excludesFromConfig =
2510
+ if settings . typos . configPath == "" # passed directly or as Path
2511
+ then
2512
+ (
2513
+ let
2514
+ toml = builtins . fromTOML ( builtins . readFile configAsFile ) ;
2515
+ in
2516
+ # The "files.extend-exclude" key comes from
2517
+ # https://github.com/crate-ci/typos/blob/master/docs/reference.md
2518
+ ( toml . files or { } ) . extend-exclude or [ ]
2519
+ )
2520
+ else
2521
+ [ ] ;
2522
+ in
2459
2523
{
2460
2524
name = "typos" ;
2461
2525
description = "Source code spell checker" ;
2462
2526
entry =
2463
2527
let
2464
- # Concatenate config in config file with section for ignoring words generated from list of words to ignore
2465
- config = "${ settings . typos . config } " + lib . strings . optionalString ( settings . typos . ignored-words != [ ] ) "\n \[ default.extend-words\] " + lib . strings . concatMapStrings ( x : "\n ${ x } = \" ${ x } \" " ) settings . typos . ignored-words ;
2466
- configFile = builtins . toFile "typos-config.toml" config ;
2467
2528
cmdArgs =
2468
2529
mkCmdArgs
2469
2530
( with settings . typos ; [
2531
+ # Config file always exists.
2532
+ [ true "--config ${ configAsFile } " ]
2470
2533
[ binary "--binary" ]
2471
2534
[ ( color != "auto" ) "--color ${ color } " ]
2472
- [ ( config != "" ) "--config ${ configFile } " ]
2473
- [ ( configPath != "" && config == "" ) "--config ${ configPath } " ]
2474
2535
[ diff "--diff" ]
2475
2536
[ ( exclude != "" ) "--exclude ${ exclude } --force-exclude" ]
2476
2537
[ ( format != "long" ) "--format ${ format } " ]
2486
2547
in
2487
2548
"${ tools . typos } /bin/typos ${ cmdArgs } " ;
2488
2549
types = [ "text" ] ;
2550
+ excludes = excludesFromConfig ;
2489
2551
} ;
2490
2552
typstfmt = {
2491
2553
name = "typstfmt" ;
0 commit comments