Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); |