Skip to content

Commit

Permalink
Add taploFmt TOML format function to mkLib (ipetkov#674)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Ivan Petkov <[email protected]>
  • Loading branch information
eureka-cpu and ipetkov authored Aug 31, 2024
1 parent 6339656 commit 96fd12c
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions examples/quick-start-workspace/.config/hakari.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
5 changes: 1 addition & 4 deletions examples/quick-start-workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[workspace]
resolver = "2"
members = [
"my-*",
"my-workspace-hack",
]
members = ["my-*", "my-workspace-hack"]

[workspace.package]
version = "0.1.0"
Expand Down
7 changes: 1 addition & 6 deletions examples/quick-start-workspace/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
6 changes: 6 additions & 0 deletions examples/quick-start-workspace/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions examples/quick-start-workspace/taplo.toml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions examples/quick-start/deny.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[licenses]
allow = [
"MIT"
]
allow = ["MIT"]
6 changes: 6 additions & 0 deletions examples/quick-start/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions examples/quick-start/taplo.toml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
mdbook
nix-eval-jobs
nixpkgs-fmt
taplo
];
};
});
Expand Down
1 change: 1 addition & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 { };
Expand Down
24 changes: 24 additions & 0 deletions lib/taploFmt.nix
Original file line number Diff line number Diff line change
@@ -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}
'';
})

0 comments on commit 96fd12c

Please sign in to comment.