Add cargoFmt

This commit is contained in:
Ivan Petkov 2022-01-16 17:49:39 -08:00
parent e457182093
commit 438154f265
No known key found for this signature in database
GPG Key ID: BB6F9EFC065832B6
5 changed files with 67 additions and 0 deletions

View File

@ -242,6 +242,43 @@ environment variables during the build, you can bring them back via
`.overrideAttrs`.
* `cargoClippyExtraArgs`
### `lib.cargoFmt`
`cargoFmt :: set -> drv`
Create a derivation which will run a `cargo fmt` invocation in a cargo
workspace.
Except where noted below, all derivation attributes are delegated to
`cargoBuild`, and can be used to influence its behavior.
* `cargoArtifacts` is disabled/cleared
* `cargoBuildCommand` will be set to run `cargo fmt` for all targets in the
workspace.
* `cargoExtraArgs` will have `rustFmtExtraArgs` appended to it
- Default value: `""`
* `cargoVendorDir` is disabled/cleared
* `doCheck` is disabled
* `doInstallCargoArtifacts` is disabled
* `doRemapSourcePathPrefix` is disabled
* `pnameSuffix` will be set to `"-fmt"`
#### Optional attributes
* `rustFmtExtraArgs`: additional flags to be passed in the rustfmt invocation
- Default value: `""`
* `cargoExtraArgs`: additional flags to be passed in the cargo invocation
- Default value: `""`
#### Native build dependencies
The `rustfmt` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller.
#### Remove attributes
The following attributes will be removed before being lowered to
`cargoBuild`. If you absolutely need these attributes present as
environment variables during the build, you can bring them back via
`.overrideAttrs`.
* `rustFmtExtraArgs`
### `lib.cleanCargoToml`
`cleanCargoToml :: set -> set`

View File

@ -15,6 +15,10 @@ onlyDrvs (lib.makeScope myLib.newScope (self:
clippy = callPackage ./clippy { };
cargoFmt = myLib.cargoFmt {
src = ./simple;
};
compilesFresh = callPackage ./compilesFresh.nix { };
compilesFreshSimple = self.compilesFresh ./simple "simple" { };
compilesFreshOverlappingTargets = self.compilesFresh

View File

@ -36,6 +36,7 @@
craneLib = (crane.mkLib pkgs).overrideScope' (final: prev: {
rustc = rustWithWasiTarget;
cargo = rustWithWasiTarget;
rustfmt = rustWithWasiTarget;
});
my-crate = craneLib.buildPackage {

24
lib/cargoFmt.nix Normal file
View File

@ -0,0 +1,24 @@
{ cargoBuild
, rustfmt
}:
{ cargoExtraArgs ? ""
, rustFmtExtraArgs ? ""
, ...
}@origArgs:
let
args = builtins.removeAttrs origArgs [ "rustFmtExtraArgs" ];
in
cargoBuild (args // {
cargoArtifacts = null;
cargoVendorDir = null;
doCheck = false;
doInstallCargoArtifacts = false;
doRemapSourcePathPrefix = false;
pnameSuffix = "-fmt";
cargoBuildCommand = "cargo fmt";
cargoExtraArgs = "${cargoExtraArgs} -- --check ${rustFmtExtraArgs}";
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ rustfmt ];
})

View File

@ -16,6 +16,7 @@ lib.makeScope newScope (self:
buildPackage = callPackage ./buildPackage.nix { };
cargoBuild = callPackage ./cargoBuild.nix { };
cargoClippy = callPackage ./cargoClippy.nix { };
cargoFmt = callPackage ./cargoFmt.nix { };
cleanCargoToml = callPackage ./cleanCargoToml.nix { };
crateNameFromCargoToml = callPackage ./crateNameFromCargoToml.nix { };
downloadCargoPackage = callPackage ./downloadCargoPackage.nix { };