1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

feat(fmt): use nixfmt-rfc-style

This commit is contained in:
Tom Bereknyei 2024-08-03 23:23:56 -04:00
parent 09199a40cd
commit 58304c1e1c
3 changed files with 87 additions and 32 deletions

View file

@ -74,7 +74,7 @@ struct InstallableFlake : InstallableValue
* have their own Nixpkgs input, or other installables.
*
* It is a layer violation for Nix to know about Nixpkgs; currently just
* `nix develop` does. Be wary of using this /
* `nix develop` and `nix fmt` does. Be wary of using this /
* `InstallableFlake::nixpkgsFlakeRef` more places.
*/
static inline FlakeRef defaultNixpkgsFlakeRef()

View file

@ -1,5 +1,6 @@
#include "command.hh"
#include "installable-value.hh"
#include "installable-flake.hh"
#include "eval.hh"
#include "run.hh"
@ -33,11 +34,89 @@ struct CmdFmt : SourceExprCommand {
auto evalState = getEvalState();
auto evalStore = getEvalStore();
Path nixfmt = "nixfmt";
FlakeRef nixpkgs = defaultNixpkgsFlakeRef();
nixpkgs = nixpkgs.resolve(evalStore);
auto nixpkgsLockFlags = lockFlags;
auto installable_ = parseInstallable(store, ".");
auto & installable = InstallableValue::require(*installable_);
auto app = installable.toApp(*evalState).resolve(evalStore, store);
Strings programArgs{app.program};
// Check for "formatters.SYSTEM", too slow on Nixpkgs until lazy-trees
/*
try {
if (auto * i = dynamic_cast<const InstallableFlake *>(&*installable_)) {
auto & installable = InstallableFlake::require(*installable_);
auto app = installable.toApp(*evalState).resolve(evalStore, store);
nixfmt = app.program;
}
} catch (Error &) {
// ignoreException();
}
*/
// Check for nixpkgs input, too slow on Nixpkgs until lazy-trees
/*
if (nixfmt == "nixfmt") {
try {
nixpkgsLockFlags.inputOverrides = {};
nixpkgsLockFlags.inputUpdates = {};
// Hard code the nixpkgs revision from <nixpkgs>/ci/pinned-nixpgs.json
if (auto * i = dynamic_cast<const InstallableFlake *>(&*installable_))
nixpkgs = i->nixpkgsFlakeRef();
} catch (Error &) {
// ignoreException();
}
}
*/
// Check for <nixpkgs>/ci/pinned-nixpkgs.json and resolve it
if (nixfmt == "nixfmt") {
try {
auto res = nixpkgs.fetchTree(store);
auto s = store->printStorePath(res.first) + "/ci/pinned-nixpkgs.json";
if (pathExists(s)) {
nlohmann::json pinned_json = nlohmann::json::parse(readFile(s));
auto pinned_rev = getString(pinned_json["rev"]);
nixpkgs = FlakeRef::fromAttrs(fetchSettings, {{"type","indirect"}, {"id", "nixpkgs"},{"rev", pinned_rev}});
nixpkgs = nixpkgs.resolve(evalStore);
}
} catch (Error &) {
// ignoreException();
}
}
// Check for nixfmt, otherwise use PATH
if (nixfmt == "nixfmt") {
try {
auto nixfmtInstallable = make_ref<InstallableFlake>(
this,
evalState,
std::move(nixpkgs),
"nixfmt-rfc-style",
ExtendedOutputsSpec::Default(),
Strings{},
Strings{"legacyPackages." + settings.thisSystem.get() + "."},
nixpkgsLockFlags);
bool found = false;
for (auto & path : Installable::toStorePathSet(getEvalStore(), store, Realise::Outputs, OperateOn::Output, {nixfmtInstallable})) {
auto s = store->printStorePath(path) + "/bin/nixfmt";
if (pathExists(s)) {
nixfmt = s;
found = true;
break;
}
}
if (!found)
throw Error("package 'nixpkgs#nixfmt-rfc-style' does not provide a 'bin/nixfmt'");
} catch (Error &) {
ignoreException();
}
}
Strings programArgs{nixfmt};
// Propagate arguments from the CLI
if (args.empty()) {
@ -54,7 +133,7 @@ struct CmdFmt : SourceExprCommand {
// we are about to exec out of this process without running C++ destructors.
evalState->evalCaches.clear();
execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs);
execProgramInStore(store, UseLookupPath::DontUse, nixfmt, programArgs);
};
};

View file

@ -2,48 +2,24 @@ R""(
# Examples
With [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt):
```nix
# flake.nix
{
outputs = { nixpkgs, self }: {
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixpkgs-fmt;
};
}
```
With [nixfmt](https://github.com/NixOS/nixfmt):
- Format the current flake: `$ nix fmt`
- Format a specific folder or file: `$ nix fmt ./folder ./file.nix`
With [nixfmt](https://github.com/serokell/nixfmt):
```nix
# flake.nix
{
outputs = { nixpkgs, self }: {
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt;
};
}
```
- Format specific files: `$ nix fmt ./file1.nix ./file2.nix`
With [Alejandra](https://github.com/kamadorueda/alejandra):
## **disabled** Overriding the formatter used
```nix
# flake.nix
{
outputs = { nixpkgs, self }: {
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.alejandra;
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt-rfc-style;
};
}
```
- Format the current flake: `$ nix fmt`
- Format a specific folder or file: `$ nix fmt ./folder ./file.nix`
# Description