@@ -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,80 @@ 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
+ lib . optionalString ( config != { } ) ( builtins . concatStringsSep "," config' ) ;
1430
+ } ;
1431
+ config-path = mkOption {
1432
+ type = types . nullOr types . str ;
1433
+ description = "Path to rustfmt.toml config file" ;
1434
+ default = null ;
1435
+ } ;
1436
+ emit = mkOption {
1437
+ type = types . nullOr ( types . enum [ "files" "stdout" ] ) ;
1438
+ description = "What data to emit and how" ;
1439
+ default = null ;
1440
+ } ;
1441
+ files-with-diff = mkOption {
1442
+ type = types . bool ;
1443
+ description = "" ;
1444
+ default = hooks . rustfmt . settings . message-format == "short" ;
1445
+ } ;
1446
+ manifest-path = mkOption {
1447
+ type = types . nullOr types . str ;
1448
+ description = "Path to Cargo.toml" ;
1449
+ default = settings . rust . cargoManifestPath ;
1450
+ } ;
1451
+ message-format = mkOption {
1452
+ type = types . nullOr ( types . enum [ "human" "short" ] ) ;
1453
+ description = "The output format of diagnostic messages" ;
1454
+ default = null ;
1455
+ } ;
1456
+ package = mkOption {
1457
+ type = types . listOf nameType ;
1458
+ description = "Package(s) to check" ;
1459
+ default = [ ] ;
1460
+ } ;
1461
+ verbose = mkOption {
1462
+ type = types . bool ;
1463
+ description = "Use verbose output" ;
1464
+ default = false ;
1465
+ } ;
1466
+ } ;
1467
+ } ;
1468
+ config . extraPackages = [
1469
+ config . packageOverrides . cargo
1470
+ config . packageOverrides . rustfmt
1471
+ ] ;
1472
+ } ) ;
1405
1473
} ;
1406
1474
shfmt = mkOption {
1407
1475
description = "shfmt hook" ;
@@ -3276,23 +3344,36 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
3276
3344
} ;
3277
3345
rustfmt =
3278
3346
let
3347
+ mkAdditionalArgs = args : lib . optionalString ( args != "" ) " -- ${ args } " ;
3348
+
3279
3349
inherit ( hooks . rustfmt ) packageOverrides ;
3280
3350
wrapper = pkgs . symlinkJoin {
3281
3351
name = "rustfmt-wrapped" ;
3282
3352
paths = [ packageOverrides . rustfmt ] ;
3283
3353
nativeBuildInputs = [ pkgs . makeWrapper ] ;
3284
3354
postBuild = ''
3285
3355
wrapProgram $out/bin/cargo-fmt \
3286
- --prefix PATH : ${ lib . makeBinPath [ packageOverrides . cargo packageOverrides . rustfmt ] }
3356
+ --prefix PATH : ${ lib . makeBinPath ( builtins . attrValues packageOverrides ) }
3287
3357
'' ;
3288
3358
} ;
3289
3359
in
3290
3360
{
3291
3361
name = "rustfmt" ;
3292
3362
description = "Format Rust code." ;
3293
3363
package = wrapper ;
3294
- packageOverrides = { cargo = tools . cargo ; rustfmt = tools . rustfmt ; } ;
3295
- entry = "${ hooks . rustfmt . package } /bin/cargo-fmt fmt ${ cargoManifestPathArg } --all -- --color always" ;
3364
+ packageOverrides = { inherit ( tools ) cargo rustfmt ; } ;
3365
+ entry =
3366
+ let
3367
+ inherit ( hooks ) rustfmt ;
3368
+ inherit ( rustfmt ) settings ;
3369
+ cargoArgs = lib . cli . toGNUCommandLineShell { } {
3370
+ inherit ( settings ) all package verbose ;
3371
+ } ;
3372
+ rustfmtArgs = lib . cli . toGNUCommandLineShell { } {
3373
+ inherit ( settings ) check color config emit verbose ;
3374
+ } ;
3375
+ in
3376
+ "${ rustfmt . package } /bin/cargo-fmt fmt ${ cargoArgs } ${ mkAdditionalArgs rustfmtArgs } " ;
3296
3377
files = "\\ .rs$" ;
3297
3378
pass_filenames = false ;
3298
3379
} ;
0 commit comments