From 58304c1e1c500f8d8cd8968996079b05d573cfb0 Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Sat, 3 Aug 2024 23:23:56 -0400 Subject: [PATCH] feat(fmt): use nixfmt-rfc-style --- src/libcmd/installable-flake.hh | 2 +- src/nix/fmt.cc | 87 +++++++++++++++++++++++++++++++-- src/nix/fmt.md | 30 ++---------- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/libcmd/installable-flake.hh b/src/libcmd/installable-flake.hh index 8e0a232ef..0af684d64 100644 --- a/src/libcmd/installable-flake.hh +++ b/src/libcmd/installable-flake.hh @@ -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() diff --git a/src/nix/fmt.cc b/src/nix/fmt.cc index d65834495..8afb5c360 100644 --- a/src/nix/fmt.cc +++ b/src/nix/fmt.cc @@ -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(&*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 /ci/pinned-nixpgs.json + if (auto * i = dynamic_cast(&*installable_)) + nixpkgs = i->nixpkgsFlakeRef(); + } catch (Error &) { + // ignoreException(); + } + } + */ + + // Check for /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( + 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); }; }; diff --git a/src/nix/fmt.md b/src/nix/fmt.md index 1c78bb36f..a06e590fe 100644 --- a/src/nix/fmt.md +++ b/src/nix/fmt.md @@ -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