Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rustfmt): add settings #504

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 98 additions & 13 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,10 @@ in
hooks.rustfmt.packageOverrides.rustfmt = pkgs.rustfmt;
```
'';
type = types.submodule
({ config, ... }: {
imports = [ hookModule ];
options.packageOverrides = {
type = types.submodule ({ config, ... }: {
imports = [ hookModule ];
options = {
packageOverrides = {
cargo = mkOption {
type = types.package;
description = "The cargo package to use.";
Expand All @@ -1396,12 +1396,84 @@ in
description = "The rustfmt package to use.";
};
};

config.extraPackages = [
config.packageOverrides.cargo
config.packageOverrides.rustfmt
];
});
settings =
let
nameType = types.strMatching "[][*?!0-9A-Za-z_-]+";
in
{
all = mkOption {
type = types.bool;
description = "Format all packages, and also their local path-based dependencies";
default = true;
};
check = mkOption {
type = types.bool;
description = "Run rustfmt in check mode";
default = false;
};
color = mkOption {
type = types.enum [ "auto" "always" "never" ];
description = "Coloring the output";
default = "always";
};
config = mkOption {
type = types.attrs;
description = "Override configuration values";
default = { };
apply = config:
let
config' = lib.mapAttrsToList
(key: value: "${key}=${toString value}")
config;
in
if config != { }
then
(builtins.concatStringsSep "," config')
else
null;
};
config-path = mkOption {
type = types.nullOr types.str;
description = "Path to rustfmt.toml config file";
default = null;
};
emit = mkOption {
type = types.nullOr (types.enum [ "files" "stdout" ]);
description = "What data to emit and how";
default = null;
};
files-with-diff = mkOption {
type = types.bool;
description = "";
default = hooks.rustfmt.settings.message-format == "short";
};
manifest-path = mkOption {
type = types.nullOr types.str;
description = "Path to Cargo.toml";
default = settings.rust.cargoManifestPath;
};
message-format = mkOption {
type = types.nullOr (types.enum [ "human" "short" ]);
description = "The output format of diagnostic messages";
default = null;
};
package = mkOption {
type = types.listOf nameType;
description = "Package(s) to check";
default = [ ];
};
verbose = mkOption {
type = types.bool;
description = "Use verbose output";
default = false;
};
};
};
config.extraPackages = [
config.packageOverrides.cargo
config.packageOverrides.rustfmt
];
});
};
shfmt = mkOption {
description = "shfmt hook";
Expand Down Expand Up @@ -3276,23 +3348,36 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
};
rustfmt =
let
mkAdditionalArgs = args: lib.optionalString (args != "") " -- ${args}";

inherit (hooks.rustfmt) packageOverrides;
wrapper = pkgs.symlinkJoin {
name = "rustfmt-wrapped";
paths = [ packageOverrides.rustfmt ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/cargo-fmt \
--prefix PATH : ${lib.makeBinPath [ packageOverrides.cargo packageOverrides.rustfmt ]}
--prefix PATH : ${lib.makeBinPath (builtins.attrValues packageOverrides)}
'';
};
in
{
name = "rustfmt";
description = "Format Rust code.";
package = wrapper;
packageOverrides = { cargo = tools.cargo; rustfmt = tools.rustfmt; };
entry = "${hooks.rustfmt.package}/bin/cargo-fmt fmt ${cargoManifestPathArg} --all -- --color always";
packageOverrides = { inherit (tools) cargo rustfmt; };
entry =
let
inherit (hooks) rustfmt;
inherit (rustfmt) settings;
cargoArgs = lib.cli.toGNUCommandLineShell { } {
inherit (settings) all package verbose;
};
rustfmtArgs = lib.cli.toGNUCommandLineShell { } {
inherit (settings) check emit config-path color files-with-diff config verbose;
};
in
"${rustfmt.package}/bin/cargo-fmt fmt ${cargoArgs}${mkAdditionalArgs rustfmtArgs}";
files = "\\.rs$";
pass_filenames = false;
};
Expand Down