@@ -1383,10 +1383,10 @@ in
1383
1383
hooks.rustfmt.packageOverrides.rustfmt = pkgs.rustfmt;
1384
1384
```
1385
1385
'' ;
1386
- type = types . submodule
1387
- ( { config , ... } : {
1388
- imports = [ hookModule ] ;
1389
- options . packageOverrides = {
1386
+ type = types . submodule ( { config , ... } : {
1387
+ imports = [ hookModule ] ;
1388
+ options = {
1389
+ packageOverrides = {
1390
1390
cargo = mkOption {
1391
1391
type = types . package ;
1392
1392
description = "The cargo package to use." ;
@@ -1396,12 +1396,84 @@ in
1396
1396
description = "The rustfmt package to use." ;
1397
1397
} ;
1398
1398
} ;
1399
-
1400
- config . extraPackages = [
1401
- config . packageOverrides . cargo
1402
- config . packageOverrides . rustfmt
1403
- ] ;
1404
- } ) ;
1399
+ settings =
1400
+ let
1401
+ nameType = types . strMatching "[][*?!0-9A-Za-z_-]+" ;
1402
+ in
1403
+ {
1404
+ all = mkOption {
1405
+ type = types . bool ;
1406
+ description = "Format all packages, and also their local path-based dependencies" ;
1407
+ default = true ;
1408
+ } ;
1409
+ check = mkOption {
1410
+ type = types . bool ;
1411
+ description = "Run rustfmt in check mode" ;
1412
+ default = false ;
1413
+ } ;
1414
+ color = mkOption {
1415
+ type = types . enum [ "auto" "always" "never" ] ;
1416
+ description = "Coloring the output" ;
1417
+ default = "always" ;
1418
+ } ;
1419
+ config = mkOption {
1420
+ type = types . attrs ;
1421
+ description = "Override configuration values" ;
1422
+ default = { } ;
1423
+ apply = config :
1424
+ let
1425
+ config' = lib . mapAttrsToList
1426
+ ( key : value : "${ key } =${ toString value } " )
1427
+ config ;
1428
+ in
1429
+ if config != { }
1430
+ then
1431
+ ( builtins . concatStringsSep "," config' )
1432
+ else
1433
+ null ;
1434
+ } ;
1435
+ config-path = mkOption {
1436
+ type = types . nullOr types . str ;
1437
+ description = "Path to rustfmt.toml config file" ;
1438
+ default = null ;
1439
+ } ;
1440
+ emit = mkOption {
1441
+ type = types . nullOr ( types . enum [ "files" "stdout" ] ) ;
1442
+ description = "What data to emit and how" ;
1443
+ default = null ;
1444
+ } ;
1445
+ files-with-diff = mkOption {
1446
+ type = types . bool ;
1447
+ description = "" ;
1448
+ default = hooks . rustfmt . settings . message-format == "short" ;
1449
+ } ;
1450
+ manifest-path = mkOption {
1451
+ type = types . nullOr types . str ;
1452
+ description = "Path to Cargo.toml" ;
1453
+ default = settings . rust . cargoManifestPath ;
1454
+ } ;
1455
+ message-format = mkOption {
1456
+ type = types . nullOr ( types . enum [ "human" "short" ] ) ;
1457
+ description = "The output format of diagnostic messages" ;
1458
+ default = null ;
1459
+ } ;
1460
+ package = mkOption {
1461
+ type = types . listOf nameType ;
1462
+ description = "Package(s) to check" ;
1463
+ default = [ ] ;
1464
+ } ;
1465
+ verbose = mkOption {
1466
+ type = types . bool ;
1467
+ description = "Use verbose output" ;
1468
+ default = false ;
1469
+ } ;
1470
+ } ;
1471
+ } ;
1472
+ config . extraPackages = [
1473
+ config . packageOverrides . cargo
1474
+ config . packageOverrides . rustfmt
1475
+ ] ;
1476
+ } ) ;
1405
1477
} ;
1406
1478
shfmt = mkOption {
1407
1479
description = "shfmt hook" ;
@@ -3276,23 +3348,36 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
3276
3348
} ;
3277
3349
rustfmt =
3278
3350
let
3351
+ mkAdditionalArgs = args : lib . optionalString ( args != "" ) " -- ${ args } " ;
3352
+
3279
3353
inherit ( hooks . rustfmt ) packageOverrides ;
3280
3354
wrapper = pkgs . symlinkJoin {
3281
3355
name = "rustfmt-wrapped" ;
3282
3356
paths = [ packageOverrides . rustfmt ] ;
3283
3357
nativeBuildInputs = [ pkgs . makeWrapper ] ;
3284
3358
postBuild = ''
3285
3359
wrapProgram $out/bin/cargo-fmt \
3286
- --prefix PATH : ${ lib . makeBinPath [ packageOverrides . cargo packageOverrides . rustfmt ] }
3360
+ --prefix PATH : ${ lib . makeBinPath ( builtins . attrValues packageOverrides ) }
3287
3361
'' ;
3288
3362
} ;
3289
3363
in
3290
3364
{
3291
3365
name = "rustfmt" ;
3292
3366
description = "Format Rust code." ;
3293
3367
package = wrapper ;
3294
- packageOverrides = { cargo = tools . cargo ; rustfmt = tools . rustfmt ; } ;
3295
- entry = "${ hooks . rustfmt . package } /bin/cargo-fmt fmt ${ cargoManifestPathArg } --all -- --color always" ;
3368
+ packageOverrides = { inherit ( tools ) cargo rustfmt ; } ;
3369
+ entry =
3370
+ let
3371
+ inherit ( hooks ) rustfmt ;
3372
+ inherit ( rustfmt ) settings ;
3373
+ cargoArgs = lib . cli . toGNUCommandLineShell { } {
3374
+ inherit ( settings ) all package verbose ;
3375
+ } ;
3376
+ rustfmtArgs = lib . cli . toGNUCommandLineShell { } {
3377
+ inherit ( settings ) check emit config-path color files-with-diff config verbose ;
3378
+ } ;
3379
+ in
3380
+ "${ rustfmt . package } /bin/cargo-fmt fmt ${ cargoArgs } ${ mkAdditionalArgs rustfmtArgs } " ;
3296
3381
files = "\\ .rs$" ;
3297
3382
pass_filenames = false ;
3298
3383
} ;
0 commit comments