Skip to content

Commit 1156aab

Browse files
committed
feat(rustfmt): add settings
1 parent 4ebefca commit 1156aab

File tree

1 file changed

+94
-13
lines changed

1 file changed

+94
-13
lines changed

modules/hooks.nix

+94-13
Original file line numberDiff line numberDiff line change
@@ -1383,10 +1383,10 @@ in
13831383
hooks.rustfmt.packageOverrides.rustfmt = pkgs.rustfmt;
13841384
```
13851385
'';
1386-
type = types.submodule
1387-
({ config, ... }: {
1388-
imports = [ hookModule ];
1389-
options.packageOverrides = {
1386+
type = types.submodule ({ config, ... }: {
1387+
imports = [ hookModule ];
1388+
options = {
1389+
packageOverrides = {
13901390
cargo = mkOption {
13911391
type = types.package;
13921392
description = "The cargo package to use.";
@@ -1396,12 +1396,80 @@ in
13961396
description = "The rustfmt package to use.";
13971397
};
13981398
};
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+
});
14051473
};
14061474
shfmt = mkOption {
14071475
description = "shfmt hook";
@@ -3276,23 +3344,36 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
32763344
};
32773345
rustfmt =
32783346
let
3347+
mkAdditionalArgs = args: lib.optionalString (args != "") " -- ${args}";
3348+
32793349
inherit (hooks.rustfmt) packageOverrides;
32803350
wrapper = pkgs.symlinkJoin {
32813351
name = "rustfmt-wrapped";
32823352
paths = [ packageOverrides.rustfmt ];
32833353
nativeBuildInputs = [ pkgs.makeWrapper ];
32843354
postBuild = ''
32853355
wrapProgram $out/bin/cargo-fmt \
3286-
--prefix PATH : ${lib.makeBinPath [ packageOverrides.cargo packageOverrides.rustfmt ]}
3356+
--prefix PATH : ${lib.makeBinPath (builtins.attrValues packageOverrides)}
32873357
'';
32883358
};
32893359
in
32903360
{
32913361
name = "rustfmt";
32923362
description = "Format Rust code.";
32933363
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}";
32963377
files = "\\.rs$";
32973378
pass_filenames = false;
32983379
};

0 commit comments

Comments
 (0)