about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/parsed-derivations.cc
blob: 72ed36d32da685953cf865fa53cceab9e113fa6d (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
#include "parsed-derivations.hh"

namespace nix {

ParsedDerivation::ParsedDerivation(const Path& drvPath, BasicDerivation& drv)
    : drvPath(drvPath), drv(drv) {
  /* Parse the __json attribute, if any. */
  auto jsonAttr = drv.env.find("__json");
  if (jsonAttr != drv.env.end()) {
    try {
      structuredAttrs = nlohmann::json::parse(jsonAttr->second);
    } catch (std::exception& e) {
      throw Error("cannot process __json attribute of '%s': %s", drvPath,
                  e.what());
    }
  }
}

std::optional<std::string> ParsedDerivation::getStringAttr(
    const std::string& name) const {
  if (structuredAttrs) {
    auto i = structuredAttrs->find(name);
    if (i == structuredAttrs->end()) {
      return {};
    } else {
      if (!i->is_string()) {
        throw Error("attribute '%s' of derivation '%s' must be a string", name,
                    drvPath);
      }
      return i->get<std::string>();
    }
  } else {
    auto i = drv.env.find(name);
    if (i == drv.env.end()) {
      return {};
    } else {
      return i->second;
    }
  }
}

bool ParsedDerivation::getBoolAttr(const std::string& name, bool def) const {
  if (structuredAttrs) {
    auto i = structuredAttrs->find(name);
    if (i == structuredAttrs->end()) {
      return def;
    } else {
      if (!i->is_boolean()) {
        throw Error("attribute '%s' of derivation '%s' must be a Boolean", name,
                    drvPath);
      }
      return i->get<bool>();
    }
  } else {
    auto i = drv.env.find(name);
    if (i == drv.env.end()) {
      return def;
    } else {
      return i->second == "1";
    }
  }
}

std::optional<Strings> ParsedDerivation::getStringsAttr(
    const std::string& name) const {
  if (structuredAttrs) {
    auto i = structuredAttrs->find(name);
    if (i == structuredAttrs->end()) {
      return {};
    } else {
      if (!i->is_array()) {
        throw Error(
            "attribute '%s' of derivation '%s' must be a list of strings", name,
            drvPath);
      }
      Strings res;
      for (auto j = i->begin(); j != i->end(); ++j) {
        if (!j->is_string()) {
          throw Error(
              "attribute '%s' of derivation '%s' must be a list of strings",
              name, drvPath);
        }
        res.push_back(j->get<std::string>());
      }
      return res;
    }
  } else {
    auto i = drv.env.find(name);
    if (i == drv.env.end()) {
      return {};
    } else {
      return tokenizeString<Strings>(i->second);
    }
  }
}

StringSet ParsedDerivation::getRequiredSystemFeatures() const {
  StringSet res;
  for (auto& i : getStringsAttr("requiredSystemFeatures").value_or(Strings())) {
    res.insert(i);
  }
  return res;
}

bool ParsedDerivation::canBuildLocally() const {
  if (drv.platform != settings.thisSystem.get() &&
      !settings.extraPlatforms.get().count(drv.platform) && !drv.isBuiltin()) {
    return false;
  }

  for (auto& feature : getRequiredSystemFeatures()) {
    if (!settings.systemFeatures.get().count(feature)) {
      return false;
    }
  }

  return true;
}

bool ParsedDerivation::willBuildLocally() const {
  return getBoolAttr("preferLocalBuild") && canBuildLocally();
}

bool ParsedDerivation::substitutesAllowed() const {
  return getBoolAttr("allowSubstitutes", true);
}

}  // namespace nix