about summary refs log tree commit diff
path: root/third_party/nix/src/nix/doctor.cc
blob: 3a5cd987a9d6f795c68ad41084ad79bb35eabd7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <absl/strings/match.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_split.h>

#include "libmain/shared.hh"
#include "libstore/serve-protocol.hh"
#include "libstore/store-api.hh"
#include "libstore/worker-protocol.hh"
#include "nix/command.hh"

using namespace nix;

std::string formatProtocol(unsigned int proto) {
  if (proto != 0u) {
    auto major = GET_PROTOCOL_MAJOR(proto) >> 8;
    auto minor = GET_PROTOCOL_MINOR(proto);
    return (format("%1%.%2%") % major % minor).str();
  }
  return "unknown";
}

struct CmdDoctor : StoreCommand {
  bool success = true;

  std::string name() override { return "doctor"; }

  std::string description() override {
    return "check your system for potential problems";
  }

  void run(ref<Store> store) override {
    std::cout << "Store uri: " << store->getUri() << std::endl;
    std::cout << std::endl;

    auto type = getStoreType();

    if (type < tOther) {
      success &= checkNixInPath();
      success &= checkProfileRoots(store);
    }
    success &= checkStoreProtocol(store->getProtocol());

    if (!success) {
      throw Exit(2);
    }
  }

  static bool checkNixInPath() {
    PathSet dirs;

    for (auto& dir : absl::StrSplit(getEnv("PATH"), absl::ByChar(':'))) {
      if (pathExists(absl::StrCat(dir, "/nix-env"))) {
        dirs.insert(dirOf(canonPath(absl::StrCat(dir, "/nix-env"), true)));
      }
    }

    if (dirs.size() != 1) {
      std::cout << "Warning: multiple versions of nix found in PATH."
                << std::endl;
      std::cout << std::endl;
      for (auto& dir : dirs) {
        std::cout << "  " << dir << std::endl;
      }
      std::cout << std::endl;
      return false;
    }

    return true;
  }

  static bool checkProfileRoots(const ref<Store>& store) {
    PathSet dirs;

    for (auto dir : absl::StrSplit(getEnv("PATH"), absl::ByChar(':'))) {
      Path profileDir = dirOf(dir);
      try {
        Path userEnv = canonPath(profileDir, true);

        if (store->isStorePath(userEnv) &&
            absl::EndsWith(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(std::string(dir));
          }
        }
      } catch (SysError&) {
      }
    }

    if (!dirs.empty()) {
      std::cout << "Warning: found profiles outside of " << settings.nixStateDir
                << "/profiles." << std::endl;
      std::cout << "The generation this profile points to might not have a "
                   "gcroot and could be"
                << std::endl;
      std::cout << "garbage collected, resulting in broken symlinks."
                << std::endl;
      std::cout << std::endl;
      for (auto& dir : dirs) {
        std::cout << "  " << dir << std::endl;
      }
      std::cout << std::endl;
      return false;
    }

    return true;
  }

  static bool checkStoreProtocol(unsigned int storeProto) {
    unsigned int clientProto = GET_PROTOCOL_MAJOR(SERVE_PROTOCOL_VERSION) ==
                                       GET_PROTOCOL_MAJOR(storeProto)
                                   ? SERVE_PROTOCOL_VERSION
                                   : PROTOCOL_VERSION;

    if (clientProto != storeProto) {
      std::cout << "Warning: protocol version of this client does not match "
                   "the store."
                << std::endl;
      std::cout << "While this is not necessarily a problem it's recommended "
                   "to keep the client in"
                << std::endl;
      std::cout << "sync with the daemon." << std::endl;
      std::cout << std::endl;
      std::cout << "Client protocol: " << formatProtocol(clientProto)
                << std::endl;
      std::cout << "Store protocol: " << formatProtocol(storeProto)
                << std::endl;
      std::cout << std::endl;
      return false;
    }

    return true;
  }
};

static RegisterCommand r1(make_ref<CmdDoctor>());