Skip to content

Commit

Permalink
add 'fmt' mode
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 authored and Patryk27 committed Jul 5, 2023
1 parent 714e701 commit abca1fb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ process, rest is passed-through into `mkDerivation`.
| `cargoTestCommands` | The commands to run in the `checkPhase`. Do not forget to set [`doCheck`](https://nixos.org/nixpkgs/manual/#ssec-check-phase). The argument must be a function modifying the default value. <br/> Default: `[ ''cargo $cargo_options test $cargo_test_options'' ]` |
| `cargoTestOptions` | Options passed to cargo test, i.e. `cargo test <OPTS>`. These options can be accessed during the build through the environment variable `cargo_test_options`. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' ]` |
| `cargoClippyOptions` | Options passed to cargo clippy, i.e. `cargo clippy -- <OPTS>`. These options can be accessed during the build through the environment variable `cargo_clippy_options`. <br /> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "-D warnings" ]` |
| `cargoFmtOptions` | Options passed to cargo fmt, i.e. `cargo fmt -- <OPTS>`. These options can be accessed during the build through the environment variable `cargo_fmt_options`. <br /> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "--check" ]` |
| `nativeBuildInputs` | Extra `nativeBuildInputs` to all derivations. Default: `[]` |
| `buildInputs` | Extra `buildInputs` to all derivations. Default: `[]` |
| `cargoOptions` | Options passed to all cargo commands, i.e. `cargo <OPTS> ...`. These options can be accessed during the build through the environment variable `cargo_options`. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ ]` |
Expand All @@ -260,7 +261,7 @@ process, rest is passed-through into `mkDerivation`.
| `copyTarget` | When true, the `target/` directory is copied to `$out`. Default: `false` |
| `postInstall` | Optional hook to run after the compilation is done; inside this script, `$out/bin` contains compiled Rust binaries. Useful if your application needs e.g. custom environment variables, in which case you can simply run `wrapProgram $out/bin/your-app-name` in here. Default: `false` |
| `usePureFromTOML` | Whether to use the `fromTOML` built-in or not. When set to `false` the python package `remarshal` is used instead (in a derivation) and the JSON output is read with `builtins.fromJSON`. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in `builtins.fromTOML` seem to have been fixed. Default: `true` |
| `mode` | What to do when building the derivation. Either `build`, `check`, `test` or `clippy`. <br/> When set to something other than `build`, no binaries are generated. Default: `"build"` |
| `mode` | What to do when building the derivation. Either `build`, `check`, `test`, `fmt` or `clippy`. <br/> When set to something other than `build`, no binaries are generated. Default: `"build"` |


## Examples
Expand Down
2 changes: 2 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# significantly reducing the Nix closure size.
, removeReferencesToSrcFromDocs
, cargoClippyOptions
, cargoFmtOptions
, mode ? "build" # `build`, `check`, `test` or `clippy`
, gitDependencies
, pname
Expand Down Expand Up @@ -142,6 +143,7 @@ let
cargo_build_options = cargoBuildOptions;
cargo_test_options = cargoTestOptions;
cargo_clippy_options = cargoClippyOptions;
cargo_fmt_options = cargoFmtOptions;
cargo_doc_options = cargoDocOptions;
cargo_bins_jq_filter = copyBinsFilter;
cargo_libs_jq_filter = copyLibsFilter;
Expand Down
13 changes: 12 additions & 1 deletion config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ let
cargoClippyOptions =
allowFun attrs0 "cargoClippyOptions" [ "-D warnings" ];

# Options passed to cargo fmt, i.e. `cargo fmt -- <OPTS>`. These options
# can be accessed during the build through the environment variable
# `cargo_fmt_options`. <br />
# Note: these values are not (shell) escaped, meaning that you can use
# environment variables but must be careful when introducing e.g. spaces. <br/>
cargoFmtOptions =
allowFun attrs0 "cargoFmtOptions" [ "--check" ];

# Extra `nativeBuildInputs` to all derivations.
nativeBuildInputs = attrs0.nativeBuildInputs or [];

Expand Down Expand Up @@ -189,7 +197,7 @@ let
# fixed.
usePureFromTOML = attrs0.usePureFromTOML or true;

# What to do when building the derivation. Either `build`, `check`, `test` or `clippy`. <br/>
# What to do when building the derivation. Either `build`, `check`, `test`, `fmt` or `clippy`. <br/>
# When set to something other than `build`, no binaries are generated.
mode = attrs0.mode or "build";
};
Expand Down Expand Up @@ -257,6 +265,8 @@ let
''cargo $cargo_options test $cargo_test_options >> $cargo_build_output_json''
else if (mode == "clippy") then
''cargo $cargo_options clippy $cargo_build_options -- $cargo_clippy_options >> $cargo_build_output_json''
else if (mode == "fmt") then
''cargo $cargo_options fmt -- $cargo_fmt_options''
else throw "Unknown mode ${mode}, allowed modes: build, check, test, clippy";

# config used during build the prebuild and the final build
Expand Down Expand Up @@ -284,6 +294,7 @@ let
cargoTestOptions

cargoClippyOptions
cargoFmtOptions

doDoc
doDocFail
Expand Down

0 comments on commit abca1fb

Please sign in to comment.