2019-10-06 20:54:56 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2018-08-29 22:59:29 +00:00
|
|
|
#include "command.hh"
|
2019-10-06 20:54:56 +00:00
|
|
|
#include "logging.hh"
|
2018-08-30 23:01:59 +00:00
|
|
|
#include "serve-protocol.hh"
|
2018-08-29 22:59:29 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2019-10-06 20:54:56 +00:00
|
|
|
#include "util.hh"
|
2018-08-30 21:42:28 +00:00
|
|
|
#include "worker-protocol.hh"
|
2018-08-29 22:59:29 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2019-10-06 20:54:56 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-08-30 21:42:28 +00:00
|
|
|
std::string formatProtocol(unsigned int proto)
|
|
|
|
{
|
|
|
|
if (proto) {
|
|
|
|
auto major = GET_PROTOCOL_MAJOR(proto) >> 8;
|
|
|
|
auto minor = GET_PROTOCOL_MINOR(proto);
|
|
|
|
return (format("%1%.%2%") % major % minor).str();
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2019-10-06 20:54:56 +00:00
|
|
|
bool checkPass(const std::string & msg) {
|
|
|
|
logger->log(ANSI_GREEN "[PASS] " ANSI_NORMAL + msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkFail(const std::string & msg) {
|
|
|
|
logger->log(ANSI_RED "[FAIL] " ANSI_NORMAL + msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:59:29 +00:00
|
|
|
struct CmdDoctor : StoreCommand
|
|
|
|
{
|
2018-10-29 12:09:22 +00:00
|
|
|
bool success = true;
|
|
|
|
|
2018-08-29 22:59:29 +00:00
|
|
|
std::string description() override
|
|
|
|
{
|
2020-05-05 13:27:47 +00:00
|
|
|
return "check your system for potential problems and print a PASS or FAIL for each check";
|
2018-08-29 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:18:23 +00:00
|
|
|
Category category() override { return catNixInstallation; }
|
|
|
|
|
2018-08-29 22:59:29 +00:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-10-06 20:54:56 +00:00
|
|
|
logger->log("Running checks against store uri: " + store->getUri());
|
2018-08-30 21:42:28 +00:00
|
|
|
|
2018-09-02 11:14:39 +00:00
|
|
|
auto type = getStoreType();
|
|
|
|
|
|
|
|
if (type < tOther) {
|
2018-10-29 12:09:22 +00:00
|
|
|
success &= checkNixInPath();
|
|
|
|
success &= checkProfileRoots(store);
|
2018-09-02 11:14:39 +00:00
|
|
|
}
|
2018-10-29 12:09:22 +00:00
|
|
|
success &= checkStoreProtocol(store->getProtocol());
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
throw Exit(2);
|
2018-08-30 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 12:09:22 +00:00
|
|
|
bool checkNixInPath()
|
2018-09-26 20:59:41 +00:00
|
|
|
{
|
2018-09-01 23:01:23 +00:00
|
|
|
PathSet dirs;
|
|
|
|
|
2019-11-22 15:06:44 +00:00
|
|
|
for (auto & dir : tokenizeString<Strings>(getEnv("PATH").value_or(""), ":"))
|
2018-09-01 23:01:23 +00:00
|
|
|
if (pathExists(dir + "/nix-env"))
|
|
|
|
dirs.insert(dirOf(canonPath(dir + "/nix-env", true)));
|
|
|
|
|
|
|
|
if (dirs.size() != 1) {
|
2019-10-06 20:54:56 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Multiple versions of nix found in PATH:\n";
|
2018-09-01 23:01:23 +00:00
|
|
|
for (auto & dir : dirs)
|
2019-10-06 20:54:56 +00:00
|
|
|
ss << " " << dir << "\n";
|
|
|
|
return checkFail(ss.str());
|
2018-09-02 10:52:04 +00:00
|
|
|
}
|
2018-10-29 12:09:22 +00:00
|
|
|
|
2019-10-06 20:54:56 +00:00
|
|
|
return checkPass("PATH contains only one nix version.");
|
2018-09-02 10:52:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 12:09:22 +00:00
|
|
|
bool checkProfileRoots(ref<Store> store)
|
2018-09-26 20:59:41 +00:00
|
|
|
{
|
2018-09-02 10:52:04 +00:00
|
|
|
PathSet dirs;
|
|
|
|
|
2019-11-22 15:06:44 +00:00
|
|
|
for (auto & dir : tokenizeString<Strings>(getEnv("PATH").value_or(""), ":")) {
|
2018-09-26 20:59:41 +00:00
|
|
|
Path profileDir = dirOf(dir);
|
2018-09-02 10:52:04 +00:00
|
|
|
try {
|
2018-09-26 20:59:41 +00:00
|
|
|
Path userEnv = canonPath(profileDir, true);
|
2018-09-02 10:52:04 +00:00
|
|
|
|
2018-09-26 20:59:41 +00:00
|
|
|
if (store->isStorePath(userEnv) && hasSuffix(userEnv, "user-environment")) {
|
|
|
|
while (profileDir.find("/profiles/") == std::string::npos && isLink(profileDir))
|
|
|
|
profileDir = absPath(readLink(profileDir), dirOf(profileDir));
|
|
|
|
|
|
|
|
if (profileDir.find("/profiles/") == std::string::npos)
|
|
|
|
dirs.insert(dir);
|
2018-09-02 10:52:04 +00:00
|
|
|
}
|
|
|
|
} catch (SysError &) {}
|
2018-09-26 20:59:41 +00:00
|
|
|
}
|
2018-09-02 10:52:04 +00:00
|
|
|
|
|
|
|
if (!dirs.empty()) {
|
2019-10-06 20:54:56 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Found profiles outside of " << settings.nixStateDir << "/profiles.\n"
|
|
|
|
<< "The generation this profile points to might not have a gcroot and could be\n"
|
|
|
|
<< "garbage collected, resulting in broken symlinks.\n\n";
|
2018-09-02 10:52:04 +00:00
|
|
|
for (auto & dir : dirs)
|
2019-10-06 20:54:56 +00:00
|
|
|
ss << " " << dir << "\n";
|
|
|
|
ss << "\n";
|
|
|
|
return checkFail(ss.str());
|
2018-09-01 23:01:23 +00:00
|
|
|
}
|
2018-10-29 12:09:22 +00:00
|
|
|
|
2019-10-06 20:54:56 +00:00
|
|
|
return checkPass("All profiles are gcroots.");
|
2018-09-01 23:01:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 12:09:22 +00:00
|
|
|
bool checkStoreProtocol(unsigned int storeProto)
|
2018-09-26 20:59:41 +00:00
|
|
|
{
|
2018-10-27 11:07:26 +00:00
|
|
|
unsigned int clientProto = GET_PROTOCOL_MAJOR(SERVE_PROTOCOL_VERSION) == GET_PROTOCOL_MAJOR(storeProto)
|
2018-08-30 23:01:59 +00:00
|
|
|
? SERVE_PROTOCOL_VERSION
|
|
|
|
: PROTOCOL_VERSION;
|
|
|
|
|
|
|
|
if (clientProto != storeProto) {
|
2019-10-06 20:54:56 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Warning: protocol version of this client does not match the store.\n"
|
|
|
|
<< "While this is not necessarily a problem it's recommended to keep the client in\n"
|
|
|
|
<< "sync with the daemon.\n\n"
|
|
|
|
<< "Client protocol: " << formatProtocol(clientProto) << "\n"
|
|
|
|
<< "Store protocol: " << formatProtocol(storeProto) << "\n\n";
|
|
|
|
return checkFail(ss.str());
|
2018-08-30 21:42:28 +00:00
|
|
|
}
|
2018-10-29 12:09:22 +00:00
|
|
|
|
2019-10-06 20:54:56 +00:00
|
|
|
return checkPass("Client protocol matches store protocol.");
|
2018-08-29 22:59:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-23 20:19:14 +00:00
|
|
|
static auto r1 = registerCommand<CmdDoctor>("doctor");
|