@@ -1427,10 +1427,11 @@ in
1427
1427
description = lib . mdDoc "When to use generate output." ;
1428
1428
default = "auto" ;
1429
1429
} ;
1430
+ # It is recommended to use `configPath`.
1430
1431
configuration =
1431
1432
mkOption {
1432
1433
type = types . str ;
1433
- description = lib . mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath ` gets ignored." ;
1434
+ description = lib . mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.config{File|Path} ` gets ignored." ;
1434
1435
default = "" ;
1435
1436
example = ''
1436
1437
[files]
@@ -1444,12 +1445,15 @@ in
1444
1445
'' ;
1445
1446
} ;
1446
1447
1448
+ # It is recommended to use a Nix path here as this way, the excludes
1449
+ # from the config file can be taken into account by pre-commit when
1450
+ # running `$ pre-commit run --all-files`.
1447
1451
configPath =
1448
1452
mkOption {
1449
- type = types . str ;
1450
- description = lib . mdDoc "Path to a custom config file." ;
1451
- default = "" ;
1452
- example = ".typos.toml" ;
1453
+ type = types . nullOr ( types . either types . str types . path ) ;
1454
+ description = lib . mdDoc "Path to a typos config file (recommended) or a string (deprecated) ." ;
1455
+ default = null ;
1456
+ example = "./. typos.toml" ;
1453
1457
} ;
1454
1458
1455
1459
diff =
@@ -3300,6 +3304,45 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
3300
3304
entry = "${ hooks . treefmt . package } /bin/treefmt --fail-on-change" ;
3301
3305
} ;
3302
3306
typos =
3307
+ let
3308
+ # Path to config file. May be in Nix store but can also be a relative
3309
+ # path to a system-dependent file.
3310
+ #
3311
+ # After various upstream discussions [0]), the best solution is to
3312
+ # provide the config file as Nix path but keep the string passing
3313
+ # as fall-back.
3314
+ #
3315
+ # [0]: https://github.com/cachix/pre-commit-hooks.nix/pull/387#issuecomment-1893600631
3316
+ pathToConfigFile =
3317
+ if hooks . typos . settings . configPath != null
3318
+ # Important: If passed as typeOf == "path", this is in Nix store
3319
+ # which we in fact encourage.
3320
+ then hooks . typos . settings . configPath
3321
+ else
3322
+ builtins . toFile "config.toml"
3323
+ # Concatenate config in config file with section for ignoring words
3324
+ # generated from list of words to ignore
3325
+ ( "${ hooks . typos . settings . configuration } " +
3326
+ lib . strings . optionalString ( hooks . typos . settings . ignored-words != [ ] ) "\n \[ default.extend-words\] " +
3327
+ lib . strings . concatMapStrings ( x : "\n ${ x } = \" ${ x } \" " ) hooks . typos . settings . ignored-words
3328
+ )
3329
+ ;
3330
+ # If the config file path is passed as Nix string and not as Nix
3331
+ # Path, we can't read it from Nix, unfortunately.
3332
+ excludesFromConfig =
3333
+ if builtins . typeOf hooks . typos . settings . configPath == "path" # passed directly or as Path
3334
+ then
3335
+ (
3336
+ let
3337
+ toml = builtins . fromTOML ( builtins . readFile pathToConfigFile ) ;
3338
+ in
3339
+ # The "files.extend-exclude" key comes from
3340
+ # https://github.com/crate-ci/typos/blob/master/docs/reference.md
3341
+ ( toml . files or { } ) . extend-exclude or [ ]
3342
+ )
3343
+ else
3344
+ [ ] ;
3345
+ in
3303
3346
{
3304
3347
name = "typos" ;
3305
3348
description = "Source code spell checker" ;
@@ -3314,8 +3357,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
3314
3357
( with hooks . typos . settings ; [
3315
3358
[ binary "--binary" ]
3316
3359
[ ( color != "auto" ) "--color ${ color } " ]
3317
- [ ( configuration != "" ) "--config ${ configFile } " ]
3318
- [ ( configPath != "" && configuration == "" ) " --config ${ configPath } " ]
3360
+ # Config file always exists (we generate one if not).
3361
+ [ true " --config ${ pathToConfigFile } " ]
3319
3362
[ diff "--diff" ]
3320
3363
[ ( exclude != "" ) "--exclude ${ exclude } --force-exclude" ]
3321
3364
[ ( format != "long" ) "--format ${ format } " ]
@@ -3331,6 +3374,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
3331
3374
in
3332
3375
"${ hooks . typos . package } /bin/typos ${ cmdArgs } " ;
3333
3376
types = [ "text" ] ;
3377
+ excludes = excludesFromConfig ;
3334
3378
} ;
3335
3379
typstfmt = {
3336
3380
name = "typstfmt" ;
0 commit comments