diff --git a/maintainers/flake-module.nix b/maintainers/flake-module.nix index de71be0d7..2dbeaf889 100644 --- a/maintainers/flake-module.nix +++ b/maintainers/flake-module.nix @@ -605,8 +605,8 @@ ''^tests/functional/flakes/prefetch\.sh$'' ''^tests/functional/flakes/run\.sh$'' ''^tests/functional/flakes/show\.sh$'' - ''^tests/functional/fmt\.sh$'' - ''^tests/functional/fmt\.simple\.sh$'' + ''^tests/functional/formatter\.sh$'' + ''^tests/functional/formatter\.simple\.sh$'' ''^tests/functional/gc-auto\.sh$'' ''^tests/functional/gc-concurrent\.builder\.sh$'' ''^tests/functional/gc-concurrent\.sh$'' diff --git a/src/libcmd/include/nix/cmd/installable-value.hh b/src/libcmd/include/nix/cmd/installable-value.hh index 9c8f1a9fb..e65c199a5 100644 --- a/src/libcmd/include/nix/cmd/installable-value.hh +++ b/src/libcmd/include/nix/cmd/installable-value.hh @@ -21,6 +21,7 @@ struct App struct UnresolvedApp { App unresolved; + std::vector build(ref evalStore, ref store); App resolve(ref evalStore, ref store); }; diff --git a/src/nix/app.cc b/src/nix/app.cc index 75ef874ba..c9a9f9caf 100644 --- a/src/nix/app.cc +++ b/src/nix/app.cc @@ -129,18 +129,23 @@ UnresolvedApp InstallableValue::toApp(EvalState & state) throw Error("attribute '%s' has unsupported type '%s'", cursor->getAttrPathStr(), type); } -// FIXME: move to libcmd -App UnresolvedApp::resolve(ref evalStore, ref store) +std::vector UnresolvedApp::build(ref evalStore, ref store) { - auto res = unresolved; - Installables installableContext; for (auto & ctxElt : unresolved.context) installableContext.push_back( make_ref(store, DerivedPath { ctxElt })); - auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext); + return Installable::build(evalStore, store, Realise::Outputs, installableContext); +} + +// FIXME: move to libcmd +App UnresolvedApp::resolve(ref evalStore, ref store) +{ + auto res = unresolved; + + auto builtContext = build(evalStore, store); res.program = resolveString(*store, unresolved.program, builtContext); if (!store->isInStore(res.program)) throw Error("app program '%s' is not in the Nix store", res.program); diff --git a/src/nix/fmt.cc b/src/nix/fmt.cc deleted file mode 100644 index dc270fb8c..000000000 --- a/src/nix/fmt.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include "nix/cmd/command.hh" -#include "nix/cmd/installable-value.hh" -#include "nix/expr/eval.hh" -#include "run.hh" - -using namespace nix; - -struct CmdFmt : SourceExprCommand { - std::vector args; - - CmdFmt() { expectArgs({.label = "args", .handler = {&args}}); } - - std::string description() override { - return "reformat your code in the standard style"; - } - - std::string doc() override { - return - #include "fmt.md" - ; - } - - Category category() override { return catSecondary; } - - Strings getDefaultFlakeAttrPaths() override { - return Strings{"formatter." + settings.thisSystem.get()}; - } - - Strings getDefaultFlakeAttrPathPrefixes() override { return Strings{}; } - - void run(ref store) override - { - auto evalState = getEvalState(); - auto evalStore = getEvalStore(); - - auto installable_ = parseInstallable(store, "."); - auto & installable = InstallableValue::require(*installable_); - auto app = installable.toApp(*evalState).resolve(evalStore, store); - - Strings programArgs{app.program}; - - // Propagate arguments from the CLI - for (auto &i : args) { - programArgs.push_back(i); - } - - // Release our references to eval caches to ensure they are persisted to disk, because - // we are about to exec out of this process without running C++ destructors. - evalState->evalCaches.clear(); - - execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs); - }; -}; - -static auto r2 = registerCommand("fmt"); diff --git a/src/nix/formatter-build.md b/src/nix/formatter-build.md new file mode 100644 index 000000000..fbb6adb1c --- /dev/null +++ b/src/nix/formatter-build.md @@ -0,0 +1,23 @@ +R""( + +# Description + +`nix formatter build` builds the formatter specified in the flake. + +Similar to [`nix build`](@docroot@/command-ref/new-cli/nix3-build.md), +unless `--no-link` is specified, after a successful +build, it creates a symlink to the store path of the formatter. This symlink is +named `./result` by default; this can be overridden using the +`--out-link` option. + +It always prints the command to standard output. + +# Examples + +* Build the formatter: + + ```console + # nix formatter build + /nix/store/cb9w44vkhk2x4adfxwgdkkf5gjmm856j-treefmt/bin/treefmt + ``` +)"" diff --git a/src/nix/fmt.md b/src/nix/formatter-run.md similarity index 90% rename from src/nix/fmt.md rename to src/nix/formatter-run.md index b4693eb65..a01b52a9d 100644 --- a/src/nix/fmt.md +++ b/src/nix/formatter-run.md @@ -2,7 +2,7 @@ R""( # Description -`nix fmt` calls the formatter specified in the flake. +`nix fmt` (an alias for `nix formatter run`) calls the formatter specified in the flake. Flags can be forwarded to the formatter by using `--` followed by the flags. diff --git a/src/nix/formatter.cc b/src/nix/formatter.cc new file mode 100644 index 000000000..8b171b244 --- /dev/null +++ b/src/nix/formatter.cc @@ -0,0 +1,143 @@ +#include "nix/cmd/command.hh" +#include "nix/cmd/installable-value.hh" +#include "nix/expr/eval.hh" +#include "nix/store/local-fs-store.hh" +#include "nix/cmd/installable-derived-path.hh" +#include "run.hh" + +using namespace nix; + +struct CmdFormatter : NixMultiCommand +{ + CmdFormatter() + : NixMultiCommand("formatter", RegisterCommand::getCommandsFor({"formatter"})) + { + } + + std::string description() override + { + return "build or run the formatter"; + } + + Category category() override + { + return catSecondary; + } +}; + +static auto rCmdFormatter = registerCommand("formatter"); + +/** Common implementation bits for the `nix formatter` subcommands. */ +struct MixFormatter : SourceExprCommand +{ + Strings getDefaultFlakeAttrPaths() override + { + return Strings{"formatter." + settings.thisSystem.get()}; + } + + Strings getDefaultFlakeAttrPathPrefixes() override + { + return Strings{}; + } +}; + +struct CmdFormatterRun : MixFormatter, MixJSON +{ + std::vector args; + + CmdFormatterRun() + { + expectArgs({.label = "args", .handler = {&args}}); + } + + std::string description() override + { + return "reformat your code in the standard style"; + } + + std::string doc() override + { + return +#include "formatter-run.md" + ; + } + + Category category() override + { + return catSecondary; + } + + void run(ref store) override + { + auto evalState = getEvalState(); + auto evalStore = getEvalStore(); + + auto installable_ = parseInstallable(store, "."); + auto & installable = InstallableValue::require(*installable_); + auto app = installable.toApp(*evalState).resolve(evalStore, store); + + Strings programArgs{app.program}; + + // Propagate arguments from the CLI + for (auto & i : args) { + programArgs.push_back(i); + } + + // Release our references to eval caches to ensure they are persisted to disk, because + // we are about to exec out of this process without running C++ destructors. + evalState->evalCaches.clear(); + + execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs); + }; +}; + +static auto rFormatterRun = registerCommand2({"formatter", "run"}); + +struct CmdFormatterBuild : MixFormatter, MixOutLinkByDefault +{ + CmdFormatterBuild() {} + + std::string description() override + { + return "build the current flake's formatter"; + } + + std::string doc() override + { + return +#include "formatter-build.md" + ; + } + + Category category() override + { + return catSecondary; + } + + void run(ref store) override + { + auto evalState = getEvalState(); + auto evalStore = getEvalStore(); + + auto installable_ = parseInstallable(store, "."); + auto & installable = InstallableValue::require(*installable_); + auto unresolvedApp = installable.toApp(*evalState); + auto app = unresolvedApp.resolve(evalStore, store); + auto buildables = unresolvedApp.build(evalStore, store); + createOutLinksMaybe(buildables, store); + + logger->cout("%s", app.program); + }; +}; + +static auto rFormatterBuild = registerCommand2({"formatter", "build"}); + +struct CmdFmt : CmdFormatterRun +{ + void run(ref store) override + { + CmdFormatterRun::run(store); + } +}; + +static auto rFmt = registerCommand("fmt"); diff --git a/src/nix/meson.build b/src/nix/meson.build index 901021330..11c30914b 100644 --- a/src/nix/meson.build +++ b/src/nix/meson.build @@ -78,7 +78,7 @@ nix_sources = [config_priv_h] + files( 'env.cc', 'eval.cc', 'flake.cc', - 'fmt.cc', + 'formatter.cc', 'hash.cc', 'log.cc', 'ls.cc', diff --git a/tests/functional/fmt.sh b/tests/functional/fmt.sh deleted file mode 100755 index e9bff50d5..000000000 --- a/tests/functional/fmt.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -source common.sh - -TODO_NixOS # Provide a `shell` variable. Try not to `export` it, perhaps. - -clearStoreIfPossible -rm -rf "$TEST_HOME"/.cache "$TEST_HOME"/.config "$TEST_HOME"/.local - -cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh "${config_nix}" "$TEST_HOME" - -cd "$TEST_HOME" - -nix fmt --help | grep "forward" - -cat << EOF > flake.nix -{ - outputs = _: { - formatter.$system = - with import ./config.nix; - mkDerivation { - name = "formatter"; - buildCommand = '' - mkdir -p \$out/bin - echo "#! ${shell}" > \$out/bin/formatter - cat \${./fmt.simple.sh} >> \$out/bin/formatter - chmod +x \$out/bin/formatter - ''; - }; - }; -} -EOF -# No arguments check -[[ "$(nix fmt)" = "Formatting(0):" ]] -# Argument forwarding check -nix fmt ./file ./folder | grep 'Formatting(2): ./file ./folder' -nix flake check -nix flake show | grep -P "package 'formatter'" diff --git a/tests/functional/formatter.sh b/tests/functional/formatter.sh new file mode 100755 index 000000000..ea6f9e1ce --- /dev/null +++ b/tests/functional/formatter.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash + +source common.sh + +TODO_NixOS # Provide a `shell` variable. Try not to `export` it, perhaps. + +clearStoreIfPossible +rm -rf "$TEST_HOME"/.cache "$TEST_HOME"/.config "$TEST_HOME"/.local + +cp ./simple.nix ./simple.builder.sh ./formatter.simple.sh "${config_nix}" "$TEST_HOME" + +cd "$TEST_HOME" + +nix formatter --help | grep "build or run the formatter" +nix fmt --help | grep "reformat your code" +nix fmt run --help | grep "reformat your code" +nix fmt build --help | grep "build" + +cat << EOF > flake.nix +{ + outputs = _: { + formatter.$system = + with import ./config.nix; + mkDerivation { + name = "formatter"; + buildCommand = '' + mkdir -p \$out/bin + echo "#! ${shell}" > \$out/bin/formatter + cat \${./formatter.simple.sh} >> \$out/bin/formatter + chmod +x \$out/bin/formatter + ''; + }; + }; +} +EOF + +# No arguments check +[[ "$(nix fmt)" = "Formatting(0):" ]] +[[ "$(nix formatter run)" = "Formatting(0):" ]] + +# Argument forwarding check +nix fmt ./file ./folder | grep 'Formatting(2): ./file ./folder' +nix formatter run ./file ./folder | grep 'Formatting(2): ./file ./folder' + +# Build checks +## Defaults to a ./result. +nix formatter build | grep ".\+/bin/formatter" +[[ -L ./result ]] +rm result + +## Can prevent the symlink. +nix formatter build --no-link +[[ ! -e ./result ]] + +## Can change the symlink name. +nix formatter build --out-link my-result | grep ".\+/bin/formatter" +[[ -L ./my-result ]] +rm ./my-result + +# Flake outputs check. +nix flake check +nix flake show | grep -P "package 'formatter'" diff --git a/tests/functional/fmt.simple.sh b/tests/functional/formatter.simple.sh similarity index 100% rename from tests/functional/fmt.simple.sh rename to tests/functional/formatter.simple.sh diff --git a/tests/functional/meson.build b/tests/functional/meson.build index f2d6a64ea..b2005d9d9 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -132,7 +132,7 @@ suites = [ 'nix-copy-ssh-ng.sh', 'post-hook.sh', 'function-trace.sh', - 'fmt.sh', + 'formatter.sh', 'eval-store.sh', 'why-depends.sh', 'derivation-json.sh',