nix-super/src/nix/search.cc

189 lines
6.4 KiB
C++
Raw Normal View History

2017-07-17 20:02:56 +03:00
#include "command.hh"
#include "globals.hh"
#include "eval.hh"
#include "eval-inline.hh"
#include "names.hh"
#include "get-drvs.hh"
2017-07-18 18:30:09 +03:00
#include "common-args.hh"
#include "json.hh"
#include "shared.hh"
#include "eval-cache.hh"
#include "attr-path.hh"
2017-07-17 20:02:56 +03:00
#include <regex>
#include <fstream>
2017-07-17 20:02:56 +03:00
using namespace nix;
std::string wrap(std::string prefix, std::string s)
{
return prefix + s + ANSI_NORMAL;
}
std::string hilite(const std::string & s, const std::smatch & m, std::string postfix)
2017-07-17 20:02:56 +03:00
{
return
m.empty()
? s
: std::string(m.prefix())
+ ANSI_GREEN + std::string(m.str()) + postfix
2017-07-17 20:02:56 +03:00
+ std::string(m.suffix());
}
2020-04-16 16:36:15 +03:00
struct CmdSearch : InstallableCommand, MixJSON
2017-07-17 20:02:56 +03:00
{
std::vector<std::string> res;
2017-07-17 20:02:56 +03:00
CmdSearch()
{
expectArgs("regex", &res);
2017-07-17 20:02:56 +03:00
}
std::string description() override
{
return "query available packages";
}
2017-09-07 21:42:11 +03:00
Examples examples() override
{
return {
Example{
"To show all packages in the flake in the current directory:",
2017-09-07 21:42:11 +03:00
"nix search"
},
Example{
"To show packages in the 'nixpkgs' flake containing 'blender' in its name or description:",
"nix search nixpkgs blender"
2017-09-07 21:42:11 +03:00
},
Example{
"To search for Firefox or Chromium:",
"nix search nixpkgs 'firefox|chromium'"
2017-09-07 21:42:11 +03:00
},
Example{
"To search for packages containing 'git' and either 'frontend' or 'gui':",
"nix search nixpkgs git 'frontend|gui'"
}
2017-09-07 21:42:11 +03:00
};
}
2020-04-16 16:36:15 +03:00
Strings getDefaultFlakeAttrPaths() override
{
return {
"packages." + settings.thisSystem.get() + ".",
"legacyPackages." + settings.thisSystem.get() + "."
};
2020-04-16 16:36:15 +03:00
}
2017-07-17 20:02:56 +03:00
void run(ref<Store> store) override
{
settings.readOnlyMode = true;
// Empty search string should match all packages
// Use "^" here instead of ".*" due to differences in resulting highlighting
// (see #1893 -- libc++ claims empty search string is not in POSIX grammar)
if (res.empty())
res.push_back("^");
std::vector<std::regex> regexes;
regexes.reserve(res.size());
2020-04-16 16:36:15 +03:00
for (auto & re : res)
regexes.push_back(std::regex(re, std::regex::extended | std::regex::icase));
2017-07-17 20:02:56 +03:00
auto state = getEvalState();
2017-11-14 15:27:01 +02:00
auto jsonOut = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
2017-07-18 18:30:09 +03:00
uint64_t results = 0;
std::function<void(eval_cache::AttrCursor & cursor, const std::vector<Symbol> & attrPath)> visit;
2017-07-17 20:02:56 +03:00
visit = [&](eval_cache::AttrCursor & cursor, const std::vector<Symbol> & attrPath)
{
Activity act(*logger, lvlInfo, actUnknown,
fmt("evaluating '%s'", concatStringsSep(".", attrPath)));
2017-07-17 20:02:56 +03:00
try {
auto recurse = [&]()
{
for (const auto & attr : cursor.getAttrs()) {
auto cursor2 = cursor.getAttr(attr);
auto attrPath2(attrPath);
attrPath2.push_back(attr);
visit(*cursor2, attrPath2);
}
};
2017-07-17 20:02:56 +03:00
if (cursor.isDerivation()) {
size_t found = 0;
2017-07-17 20:02:56 +03:00
DrvName name(cursor.getAttr("name")->getString());
2017-07-17 20:02:56 +03:00
auto aMeta = cursor.maybeGetAttr("meta");
auto aDescription = aMeta ? aMeta->maybeGetAttr("description") : nullptr;
auto description = aDescription ? aDescription->getString() : "";
std::replace(description.begin(), description.end(), '\n', ' ');
auto attrPath2 = concatStringsSep(".", attrPath);
2017-07-17 20:02:56 +03:00
std::smatch attrPathMatch;
std::smatch descriptionMatch;
std::smatch nameMatch;
2017-07-17 20:02:56 +03:00
for (auto & regex : regexes) {
std::regex_search(attrPath2, attrPathMatch, regex);
std::regex_search(name.name, nameMatch, regex);
std::regex_search(description, descriptionMatch, regex);
if (!attrPathMatch.empty()
|| !nameMatch.empty()
|| !descriptionMatch.empty())
found++;
}
2017-07-17 20:02:56 +03:00
if (found == res.size()) {
results++;
2017-07-18 18:30:09 +03:00
if (json) {
auto jsonElem = jsonOut->object(attrPath2);
2020-04-24 15:42:17 +03:00
jsonElem.attr("pname", name.name);
jsonElem.attr("version", name.version);
2017-07-18 18:30:09 +03:00
jsonElem.attr("description", description);
} else {
2020-04-24 15:42:17 +03:00
auto name2 = hilite(name.name, nameMatch, "\e[0;2m");
if (results > 1) logger->stdout_("");
logger->stdout_(
2020-04-24 15:42:17 +03:00
"* %s%s",
wrap("\e[0;1m", hilite(attrPath2, attrPathMatch, "\e[0;1m")),
2020-04-24 15:42:17 +03:00
name.version != "" ? " (" + name.version + ")" : "");
if (description != "")
logger->stdout_(
" %s", hilite(description, descriptionMatch, ANSI_NORMAL));
}
}
2017-07-17 20:02:56 +03:00
}
else if (
attrPath.size() == 0
|| (attrPath[0] == "legacyPackages" && attrPath.size() <= 2)
|| (attrPath[0] == "packages" && attrPath.size() <= 2))
recurse();
2017-07-17 20:02:56 +03:00
else if (attrPath[0] == "legacyPackages" && attrPath.size() > 2) {
auto attr = cursor.maybeGetAttr(state->sRecurseForDerivations);
if (attr && attr->getBool())
recurse();
2017-07-17 20:02:56 +03:00
}
} catch (EvalError & e) {
if (!(attrPath.size() > 0 && attrPath[0] == "legacyPackages"))
throw;
2017-07-17 20:02:56 +03:00
}
};
2020-08-07 15:13:24 +03:00
for (auto & [cursor, prefix] : installable->getCursors(*state))
visit(*cursor, parseAttrPath(*state, prefix));
if (!json && !results)
throw Error("no results for the given search term(s)!");
2017-07-17 20:02:56 +03:00
}
};
static auto r1 = registerCommand<CmdSearch>("search");