Skip to content

Commit 6d7586d

Browse files
authored
Merge pull request cachix#504 from sivizius/extend-rustfmt
feat(rustfmt): add settings
2 parents cd91c7c + 6ce0397 commit 6d7586d

File tree

1 file changed

+98
-13
lines changed

1 file changed

+98
-13
lines changed

modules/hooks.nix

+98-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,84 @@ 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+
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+
});
14051477
};
14061478
shfmt = mkOption {
14071479
description = "shfmt hook";
@@ -3276,23 +3348,36 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
32763348
};
32773349
rustfmt =
32783350
let
3351+
mkAdditionalArgs = args: lib.optionalString (args != "") " -- ${args}";
3352+
32793353
inherit (hooks.rustfmt) packageOverrides;
32803354
wrapper = pkgs.symlinkJoin {
32813355
name = "rustfmt-wrapped";
32823356
paths = [ packageOverrides.rustfmt ];
32833357
nativeBuildInputs = [ pkgs.makeWrapper ];
32843358
postBuild = ''
32853359
wrapProgram $out/bin/cargo-fmt \
3286-
--prefix PATH : ${lib.makeBinPath [ packageOverrides.cargo packageOverrides.rustfmt ]}
3360+
--prefix PATH : ${lib.makeBinPath (builtins.attrValues packageOverrides)}
32873361
'';
32883362
};
32893363
in
32903364
{
32913365
name = "rustfmt";
32923366
description = "Format Rust code.";
32933367
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}";
32963381
files = "\\.rs$";
32973382
pass_filenames = false;
32983383
};

0 commit comments

Comments
 (0)