2019-10-06 23:54:56 +03:00
|
|
|
#include <sstream>
|
|
|
|
|
2018-08-30 01:59:29 +03:00
|
|
|
#include "command.hh"
|
2019-10-06 23:54:56 +03:00
|
|
|
#include "logging.hh"
|
2018-08-31 02:01:59 +03:00
|
|
|
#include "serve-protocol.hh"
|
2018-08-30 01:59:29 +03:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2019-10-06 23:54:56 +03:00
|
|
|
#include "util.hh"
|
2018-08-31 00:42:28 +03:00
|
|
|
#include "worker-protocol.hh"
|
2018-08-30 01:59:29 +03:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
namespace {
|
|
|
|
|
2018-08-31 00:42:28 +03:00
|
|
|
std::string formatProtocol(unsigned int proto)
|
|
|
|
{
|
|
|
|
if (proto) {
|
|
|
|
auto major = GET_PROTOCOL_MAJOR(proto) >> 8;
|
|
|
|
auto minor = GET_PROTOCOL_MINOR(proto);
|
2023-03-02 16:44:19 +02:00
|
|
|
return fmt("%1%.%2%", major, minor);
|
2018-08-31 00:42:28 +03:00
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
bool checkPass(const std::string & msg) {
|
2022-04-05 12:33:46 +03:00
|
|
|
notice(ANSI_GREEN "[PASS] " ANSI_NORMAL + msg);
|
2019-10-06 23:54:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkFail(const std::string & msg) {
|
2022-04-05 12:33:46 +03:00
|
|
|
notice(ANSI_RED "[FAIL] " ANSI_NORMAL + msg);
|
2019-10-06 23:54:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:21:08 +02:00
|
|
|
void checkInfo(const std::string & msg) {
|
|
|
|
notice(ANSI_BLUE "[INFO] " ANSI_NORMAL + msg);
|
|
|
|
}
|
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
}
|
|
|
|
|
2018-08-30 01:59:29 +03:00
|
|
|
struct CmdDoctor : StoreCommand
|
|
|
|
{
|
2018-10-29 14:09:22 +02:00
|
|
|
bool success = true;
|
|
|
|
|
2023-01-17 06:01:18 +02:00
|
|
|
/**
|
|
|
|
* This command is stable before the others
|
|
|
|
*/
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature() override
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2018-08-30 01:59:29 +03:00
|
|
|
std::string description() override
|
|
|
|
{
|
2020-05-05 16:27:47 +03:00
|
|
|
return "check your system for potential problems and print a PASS or FAIL for each check";
|
2018-08-30 01:59:29 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catNixInstallation; }
|
|
|
|
|
2018-08-30 01:59:29 +03:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-10-06 23:54:56 +03:00
|
|
|
logger->log("Running checks against store uri: " + store->getUri());
|
2018-08-31 00:42:28 +03:00
|
|
|
|
2020-07-18 00:24:02 +03:00
|
|
|
if (store.dynamic_pointer_cast<LocalFSStore>()) {
|
2018-10-29 14:09:22 +02:00
|
|
|
success &= checkNixInPath();
|
|
|
|
success &= checkProfileRoots(store);
|
2018-09-02 14:14:39 +03:00
|
|
|
}
|
2018-10-29 14:09:22 +02:00
|
|
|
success &= checkStoreProtocol(store->getProtocol());
|
2022-12-26 22:21:08 +02:00
|
|
|
checkTrustedUser(store);
|
2018-10-29 14:09:22 +02:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
throw Exit(2);
|
2018-08-31 00:42:28 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:09:22 +02:00
|
|
|
bool checkNixInPath()
|
2018-09-26 23:59:41 +03:00
|
|
|
{
|
2018-09-02 02:01:23 +03:00
|
|
|
PathSet dirs;
|
|
|
|
|
2019-11-22 17:06:44 +02:00
|
|
|
for (auto & dir : tokenizeString<Strings>(getEnv("PATH").value_or(""), ":"))
|
2018-09-02 02:01:23 +03:00
|
|
|
if (pathExists(dir + "/nix-env"))
|
|
|
|
dirs.insert(dirOf(canonPath(dir + "/nix-env", true)));
|
|
|
|
|
|
|
|
if (dirs.size() != 1) {
|
2019-10-06 23:54:56 +03:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Multiple versions of nix found in PATH:\n";
|
2018-09-02 02:01:23 +03:00
|
|
|
for (auto & dir : dirs)
|
2019-10-06 23:54:56 +03:00
|
|
|
ss << " " << dir << "\n";
|
|
|
|
return checkFail(ss.str());
|
2018-09-02 13:52:04 +03:00
|
|
|
}
|
2018-10-29 14:09:22 +02:00
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
return checkPass("PATH contains only one nix version.");
|
2018-09-02 13:52:04 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:09:22 +02:00
|
|
|
bool checkProfileRoots(ref<Store> store)
|
2018-09-26 23:59:41 +03:00
|
|
|
{
|
2018-09-02 13:52:04 +03:00
|
|
|
PathSet dirs;
|
|
|
|
|
2019-11-22 17:06:44 +02:00
|
|
|
for (auto & dir : tokenizeString<Strings>(getEnv("PATH").value_or(""), ":")) {
|
2018-09-26 23:59:41 +03:00
|
|
|
Path profileDir = dirOf(dir);
|
2018-09-02 13:52:04 +03:00
|
|
|
try {
|
2018-09-26 23:59:41 +03:00
|
|
|
Path userEnv = canonPath(profileDir, true);
|
2018-09-02 13:52:04 +03:00
|
|
|
|
2018-09-26 23:59:41 +03: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 13:52:04 +03:00
|
|
|
}
|
|
|
|
} catch (SysError &) {}
|
2018-09-26 23:59:41 +03:00
|
|
|
}
|
2018-09-02 13:52:04 +03:00
|
|
|
|
|
|
|
if (!dirs.empty()) {
|
2019-10-06 23:54:56 +03: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 13:52:04 +03:00
|
|
|
for (auto & dir : dirs)
|
2019-10-06 23:54:56 +03:00
|
|
|
ss << " " << dir << "\n";
|
|
|
|
ss << "\n";
|
|
|
|
return checkFail(ss.str());
|
2018-09-02 02:01:23 +03:00
|
|
|
}
|
2018-10-29 14:09:22 +02:00
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
return checkPass("All profiles are gcroots.");
|
2018-09-02 02:01:23 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:09:22 +02:00
|
|
|
bool checkStoreProtocol(unsigned int storeProto)
|
2018-09-26 23:59:41 +03:00
|
|
|
{
|
2018-10-27 14:07:26 +03:00
|
|
|
unsigned int clientProto = GET_PROTOCOL_MAJOR(SERVE_PROTOCOL_VERSION) == GET_PROTOCOL_MAJOR(storeProto)
|
2018-08-31 02:01:59 +03:00
|
|
|
? SERVE_PROTOCOL_VERSION
|
|
|
|
: PROTOCOL_VERSION;
|
|
|
|
|
|
|
|
if (clientProto != storeProto) {
|
2019-10-06 23:54:56 +03: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-31 00:42:28 +03:00
|
|
|
}
|
2018-10-29 14:09:22 +02:00
|
|
|
|
2019-10-06 23:54:56 +03:00
|
|
|
return checkPass("Client protocol matches store protocol.");
|
2018-08-30 01:59:29 +03:00
|
|
|
}
|
2022-12-26 22:21:08 +02:00
|
|
|
|
|
|
|
void checkTrustedUser(ref<Store> store)
|
|
|
|
{
|
|
|
|
std::string_view trusted = store->isTrustedClient()
|
|
|
|
? "trusted"
|
|
|
|
: "not trusted";
|
|
|
|
checkInfo(fmt("You are %s by store uri: %s", trusted, store->getUri()));
|
|
|
|
}
|
2018-08-30 01:59:29 +03:00
|
|
|
};
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static auto rCmdDoctor = registerCommand<CmdDoctor>("doctor");
|