mirror of
https://github.com/NixOS/nix.git
synced 2025-11-20 17:29:36 +01:00
Add 'nix list-options' command
This commit is contained in:
parent
edfd676e05
commit
38b339d447
1 changed files with 67 additions and 0 deletions
67
src/nix/options.cc
Normal file
67
src/nix/options.cc
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
#include "command.hh"
|
||||||
|
#include "common-args.hh"
|
||||||
|
#include "shared.hh"
|
||||||
|
#include "store-api.hh"
|
||||||
|
#include "eval.hh"
|
||||||
|
#include "eval-inline.hh"
|
||||||
|
#include "json.hh"
|
||||||
|
#include "value-to-json.hh"
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
|
||||||
|
struct CmdListOptions : InstallableCommand
|
||||||
|
{
|
||||||
|
CmdListOptions()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string description() override
|
||||||
|
{
|
||||||
|
return "show the options provided by a Nix configuration";
|
||||||
|
}
|
||||||
|
|
||||||
|
//Category category() override { return catSecondary; }
|
||||||
|
|
||||||
|
void run(ref<Store> store) override
|
||||||
|
{
|
||||||
|
auto state = getEvalState();
|
||||||
|
|
||||||
|
auto vModule = installable->toValue(*state).first;
|
||||||
|
state->forceAttrs(*vModule);
|
||||||
|
|
||||||
|
auto aAllOptions = vModule->attrs->get(state->symbols.create("_allOptions"));
|
||||||
|
if (!aAllOptions)
|
||||||
|
throw Error("module does not have an '_allOptions' attribute");
|
||||||
|
state->forceAttrs(*aAllOptions->value);
|
||||||
|
|
||||||
|
auto aFinal = vModule->attrs->get(state->symbols.create("final"));
|
||||||
|
if (!aFinal)
|
||||||
|
throw Error("module does not have an 'final' attribute");
|
||||||
|
state->forceAttrs(*aFinal->value);
|
||||||
|
|
||||||
|
for (const auto & [n, option] : enumerate(aAllOptions->value->attrs->lexicographicOrder())) {
|
||||||
|
if (n) logger->stdout("");
|
||||||
|
logger->stdout(ANSI_BOLD "%s" ANSI_NORMAL, option->name);
|
||||||
|
|
||||||
|
state->forceAttrs(*option->value);
|
||||||
|
|
||||||
|
std::string description = ANSI_ITALIC "<no description>" ANSI_NORMAL;
|
||||||
|
auto aDescription = option->value->attrs->get(state->symbols.create("description"));
|
||||||
|
if (aDescription)
|
||||||
|
// FIXME: render markdown.
|
||||||
|
description = state->forceString(*aDescription->value);
|
||||||
|
logger->stdout(" " ANSI_BOLD "Description:" ANSI_NORMAL " %s", description);
|
||||||
|
|
||||||
|
auto aValue = aFinal->value->attrs->get(option->name);
|
||||||
|
assert(aValue);
|
||||||
|
|
||||||
|
std::ostringstream str;
|
||||||
|
JSONPlaceholder jsonOut(str);
|
||||||
|
PathSet context;
|
||||||
|
printValueAsJSON(*state, true, *aValue->value, jsonOut, context);
|
||||||
|
logger->stdout(" " ANSI_BOLD "Value:" ANSI_NORMAL " %s", str.str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto r1 = registerCommand<CmdListOptions>("list-options");
|
||||||
Loading…
Add table
Add a link
Reference in a new issue