From 96fd12c7100e9e05fa1a0a5bd108525600ce282f Mon Sep 17 00:00:00 2001 From: "[eureka@nixos]" <57543709+eureka-cpu@users.noreply.github.com> Date: Sat, 31 Aug 2024 10:27:30 -0700 Subject: [PATCH] Add `taploFmt` TOML format function to `mkLib` (#674) --------- Co-authored-by: Ivan Petkov --- CHANGELOG.md | 3 ++ docs/API.md | 34 +++++++++++++++++++ .../quick-start-workspace/.config/hakari.toml | 6 ++-- examples/quick-start-workspace/Cargo.toml | 5 +-- examples/quick-start-workspace/deny.toml | 7 +--- examples/quick-start-workspace/flake.nix | 6 ++++ examples/quick-start-workspace/taplo.toml | 13 +++++++ examples/quick-start/deny.toml | 4 +-- examples/quick-start/flake.nix | 6 ++++ examples/quick-start/taplo.toml | 13 +++++++ flake.nix | 1 + lib/default.nix | 1 + lib/taploFmt.nix | 24 +++++++++++++ 13 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 examples/quick-start-workspace/taplo.toml create mode 100644 examples/quick-start/taplo.toml create mode 100644 lib/taploFmt.nix diff --git a/CHANGELOG.md b/CHANGELOG.md index e127d0bf..fb59bdc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased +### Added +* `taploFmt` is now available for checking TOML formatting + ### Changed * **Breaking** (technically): `buildPackage` no longer adds `jq` to `nativeBuildInputs` as doing so can result in rebuilding any `*-sys` crates diff --git a/docs/API.md b/docs/API.md index 30d13b99..72483dee 100644 --- a/docs/API.md +++ b/docs/API.md @@ -538,6 +538,40 @@ environment variables during the build, you can bring them back via * `cargoExtraArgs` * `rustFmtExtraArgs` +### `craneLib.taploFmt` + +`taploFmt :: set -> drv` + +Create a derivation which will run a `taplo fmt` invocation in a cargo +workspace. + +Except where noted below, all derivation attributes are delegated to +`mkCargoDerivation`, and can be used to influence its behavior. +* `buildPhaseCargoCommand` will be set to run `taplo fmt` (in check mode) in the + workspace. +* `cargoArtifacts` is disabled/cleared +* `cargoVendorDir` is disabled/cleared +* `pnameSuffix` will be set to `"-tomlfmt"` + +#### Optional attributes +* `taploExtraArgs`: additional flags to be passed in the taplo invocation + - Default value: `""` + +`taplo` command line options for setting `taploExtraArgs` and configuration options +for `taplo.toml` config files can be found in the _Command Line_ and _Configuration_ +sections of the [taplo documentation](https://taplo.tamasfe.dev/). + +#### Native build dependencies +The `taplo` 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 +`mkCargoDerivation`. If you absolutely need these attributes present as +environment variables during the build, you can bring them back via +`.overrideAttrs`. +* `taploExtraArgs` + ### `craneLib.cargoLlvmCov` `cargoLlvmCov :: set -> drv` diff --git a/examples/quick-start-workspace/.config/hakari.toml b/examples/quick-start-workspace/.config/hakari.toml index 2fd5e568..ec38ea26 100644 --- a/examples/quick-start-workspace/.config/hakari.toml +++ b/examples/quick-start-workspace/.config/hakari.toml @@ -15,9 +15,9 @@ resolver = "2" # Add triples corresponding to platforms commonly used by developers here. # https://doc.rust-lang.org/rustc/platform-support.html platforms = [ - # "x86_64-unknown-linux-gnu", - # "x86_64-apple-darwin", - # "x86_64-pc-windows-msvc", + # "x86_64-unknown-linux-gnu", + # "x86_64-apple-darwin", + # "x86_64-pc-windows-msvc", ] # Write out exact versions rather than a semver range. (Defaults to false.) diff --git a/examples/quick-start-workspace/Cargo.toml b/examples/quick-start-workspace/Cargo.toml index e70fbc39..2aebcb0c 100644 --- a/examples/quick-start-workspace/Cargo.toml +++ b/examples/quick-start-workspace/Cargo.toml @@ -1,9 +1,6 @@ [workspace] resolver = "2" -members = [ - "my-*", - "my-workspace-hack", -] +members = ["my-*", "my-workspace-hack"] [workspace.package] version = "0.1.0" diff --git a/examples/quick-start-workspace/deny.toml b/examples/quick-start-workspace/deny.toml index 009ed245..31accdfc 100644 --- a/examples/quick-start-workspace/deny.toml +++ b/examples/quick-start-workspace/deny.toml @@ -3,9 +3,4 @@ multiple-versions = 'allow' [licenses] private = { ignore = true } -allow = [ - "Apache-2.0", - "BSD-3-Clause", - "MIT", - "Unicode-DFS-2016", -] +allow = ["Apache-2.0", "BSD-3-Clause", "MIT", "Unicode-DFS-2016"] diff --git a/examples/quick-start-workspace/flake.nix b/examples/quick-start-workspace/flake.nix index d5c6abe8..c5ee53dd 100644 --- a/examples/quick-start-workspace/flake.nix +++ b/examples/quick-start-workspace/flake.nix @@ -117,6 +117,12 @@ inherit src; }; + my-workspace-toml-fmt = craneLib.taploFmt { + src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ]; + # taplo arguments can be further customized below as needed + # taploExtraArgs = "--config ./taplo.toml"; + }; + # Audit dependencies my-workspace-audit = craneLib.cargoAudit { inherit src advisory-db; diff --git a/examples/quick-start-workspace/taplo.toml b/examples/quick-start-workspace/taplo.toml new file mode 100644 index 00000000..e832af10 --- /dev/null +++ b/examples/quick-start-workspace/taplo.toml @@ -0,0 +1,13 @@ +# Sorts `Cargo.toml` dependencies. All other `.toml` files are formatted with the default config. +# +# https://taplo.tamasfe.dev/configuration/file.html#configuration-file + +[formatting] +reorder_keys = false + +[[rule]] +include = ["**/Cargo.toml"] +keys = ["dependencies"] + +[rule.formatting] +reorder_keys = true diff --git a/examples/quick-start/deny.toml b/examples/quick-start/deny.toml index b951a73f..ab173681 100644 --- a/examples/quick-start/deny.toml +++ b/examples/quick-start/deny.toml @@ -1,4 +1,2 @@ [licenses] -allow = [ - "MIT" -] +allow = ["MIT"] diff --git a/examples/quick-start/flake.nix b/examples/quick-start/flake.nix index f9c5f731..a1db4fe0 100644 --- a/examples/quick-start/flake.nix +++ b/examples/quick-start/flake.nix @@ -88,6 +88,12 @@ inherit src; }; + my-crate-toml-fmt = craneLib.taploFmt { + src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ]; + # taplo arguments can be further customized below as needed + # taploExtraArgs = "--config ./taplo.toml"; + }; + # Audit dependencies my-crate-audit = craneLib.cargoAudit { inherit src advisory-db; diff --git a/examples/quick-start/taplo.toml b/examples/quick-start/taplo.toml new file mode 100644 index 00000000..e832af10 --- /dev/null +++ b/examples/quick-start/taplo.toml @@ -0,0 +1,13 @@ +# Sorts `Cargo.toml` dependencies. All other `.toml` files are formatted with the default config. +# +# https://taplo.tamasfe.dev/configuration/file.html#configuration-file + +[formatting] +reorder_keys = false + +[[rule]] +include = ["**/Cargo.toml"] +keys = ["dependencies"] + +[rule.formatting] +reorder_keys = true diff --git a/flake.nix b/flake.nix index 146ebce7..2e93fdfa 100644 --- a/flake.nix +++ b/flake.nix @@ -161,6 +161,7 @@ mdbook nix-eval-jobs nixpkgs-fmt + taplo ]; }; }); diff --git a/lib/default.nix b/lib/default.nix index 6f9aa6e8..bb805055 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -119,6 +119,7 @@ let registryFromSparse = callPackage ./registryFromSparse.nix { }; removeReferencesToVendoredSourcesHook = callPackage ./setupHooks/removeReferencesToVendoredSources.nix { }; replaceCargoLockHook = callPackage ./setupHooks/replaceCargoLockHook.nix { }; + taploFmt = callPackage ./taploFmt.nix { }; urlForCargoPackage = callPackage ./urlForCargoPackage.nix { }; vendorCargoDeps = callPackage ./vendorCargoDeps.nix { }; vendorMultipleCargoDeps = callPackage ./vendorMultipleCargoDeps.nix { }; diff --git a/lib/taploFmt.nix b/lib/taploFmt.nix new file mode 100644 index 00000000..b6a73145 --- /dev/null +++ b/lib/taploFmt.nix @@ -0,0 +1,24 @@ +{ taplo +, mkCargoDerivation +}: + +{ taploExtraArgs ? "" +, ... +}@origArgs: +let + args = builtins.removeAttrs origArgs [ "taploExtraArgs" ]; +in +mkCargoDerivation (args // { + cargoArtifacts = null; + cargoVendorDir = null; + pnameSuffix = "-tomlfmt"; + + buildPhaseCargoCommand = "taplo fmt --check ${taploExtraArgs}"; + + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ taplo ]; + + preInstallPhases = [ "ensureTargetDir" ] ++ (args.preInstallPhases or [ ]); + ensureTargetDir = '' + mkdir -p ''${CARGO_TARGET_DIR:-target} + ''; +})