| 1 | #include "derivations.hh"
|
| 2 | #include "store-api.hh"
|
| 3 | #include "globals.hh"
|
| 4 | #include "util.hh"
|
| 5 |
|
| 6 | #include <format>
|
| 7 |
|
| 8 | #include <cassert>
|
| 9 |
|
| 10 | namespace nix {
|
| 11 |
|
| 12 |
|
| 13 | void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash & hash) const
|
| 14 | {
|
| 15 | recursive = false;
|
| 16 | string algo = hashAlgo;
|
| 17 |
|
| 18 | if (algo.starts_with("r:")) {
|
| 19 | recursive = true;
|
| 20 | algo = string(algo, 2);
|
| 21 | }
|
| 22 |
|
| 23 | hashType = parseHashType(algo);
|
| 24 | if (hashType == htUnknown)
|
| 25 | throw Error(std::format("unknown hash algorithm `{}'", algo));
|
| 26 |
|
| 27 | hash = parseHash(hashType, this->hash);
|
| 28 | }
|
| 29 |
|
| 30 |
|
| 31 | Path writeDerivation(StoreAPI & store,
|
| 32 | const Derivation & drv, const string & name, bool repair)
|
| 33 | {
|
| 34 | PathSet references;
|
| 35 | references.insert(drv.inputSrcs.begin(), drv.inputSrcs.end());
|
| 36 | for (const auto& i : drv.inputDrvs)
|
| 37 | references.insert(i.first);
|
| 38 | |
| 39 | (that can be missing (of course) and should not necessarily be
|
| 40 | held during a garbage collection).
|
| 41 | string suffix = name + drvExtension;
|
| 42 | string contents = unparseDerivation(drv);
|
| 43 | return settings.readOnlyMode
|
| 44 | ? computeStorePathForText(suffix, contents, references)
|
| 45 | : store.addTextToStore(suffix, contents, references, repair);
|
| 46 | }
|
| 47 |
|
| 48 |
|
| 49 | static Path parsePath(std::istream & str)
|
| 50 | {
|
| 51 | string s = parseString(str);
|
| 52 | if (s.size() == 0 || s[0] != '/')
|
| 53 | throw FormatError(std::format("bad path `{}' in derivation", s));
|
| 54 | return s;
|
| 55 | }
|
| 56 |
|
| 57 |
|
| 58 | static StringSet parseStrings(std::istream & str, bool arePaths)
|
| 59 | {
|
| 60 | StringSet res;
|
| 61 | while (!endOfList(str))
|
| 62 | res.insert(arePaths ? parsePath(str) : parseString(str));
|
| 63 | return res;
|
| 64 | }
|
| 65 |
|
| 66 |
|
| 67 | static Derivation parseDerivation(const string & s)
|
| 68 | {
|
| 69 | Derivation drv;
|
| 70 | std::istringstream str(s);
|
| 71 | expect(str, "Derive([");
|
| 72 |
|
| 73 |
|
| 74 | while (!endOfList(str)) {
|
| 75 | DerivationOutput out;
|
| 76 | expect(str, "("); string id = parseString(str);
|
| 77 | expect(str, ","); out.path = parsePath(str);
|
| 78 | expect(str, ","); out.hashAlgo = parseString(str);
|
| 79 | expect(str, ","); out.hash = parseString(str);
|
| 80 | expect(str, ")");
|
| 81 | drv.outputs[id] = out;
|
| 82 | }
|
| 83 |
|
| 84 |
|
| 85 | expect(str, ",[");
|
| 86 | while (!endOfList(str)) {
|
| 87 | expect(str, "(");
|
| 88 | Path drvPath = parsePath(str);
|
| 89 | expect(str, ",[");
|
| 90 | drv.inputDrvs[drvPath] = parseStrings(str, false);
|
| 91 | expect(str, ")");
|
| 92 | }
|
| 93 |
|
| 94 | expect(str, ",["); drv.inputSrcs = parseStrings(str, true);
|
| 95 | expect(str, ","); drv.platform = parseString(str);
|
| 96 | expect(str, ","); drv.builder = parseString(str);
|
| 97 |
|
| 98 |
|
| 99 | expect(str, ",[");
|
| 100 | while (!endOfList(str))
|
| 101 | drv.args.push_back(parseString(str));
|
| 102 |
|
| 103 |
|
| 104 | expect(str, ",[");
|
| 105 | while (!endOfList(str)) {
|
| 106 | expect(str, "("); string name = parseString(str);
|
| 107 | expect(str, ","); string value = parseString(str);
|
| 108 | expect(str, ")");
|
| 109 | drv.env[name] = value;
|
| 110 | }
|
| 111 |
|
| 112 | expect(str, ")");
|
| 113 | return drv;
|
| 114 | }
|
| 115 |
|
| 116 |
|
| 117 | Derivation readDerivation(const Path & drvPath)
|
| 118 | {
|
| 119 | try {
|
| 120 | return parseDerivation(readFile(drvPath));
|
| 121 | } catch (FormatError & e) {
|
| 122 | throw Error(std::format("error parsing derivation `{}': {}", drvPath, e.msg()));
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 |
|
| 127 | static void printString(string & res, const string & s)
|
| 128 | {
|
| 129 | res += '"';
|
| 130 | for (const char * i = s.c_str(); *i; i++)
|
| 131 | if (*i == '\"' || *i == '\\') { res += "\\"; res += *i; }
|
| 132 | else if (*i == '\n') res += "\\n";
|
| 133 | else if (*i == '\r') res += "\\r";
|
| 134 | else if (*i == '\t') res += "\\t";
|
| 135 | else res += *i;
|
| 136 | res += '"';
|
| 137 | }
|
| 138 |
|
| 139 |
|
| 140 | template<class ForwardIterator>
|
| 141 | static void printStrings(string & res, ForwardIterator i, ForwardIterator j)
|
| 142 | {
|
| 143 | res += '[';
|
| 144 | bool first = true;
|
| 145 | for ( ; i != j; ++i) {
|
| 146 | if (first) first = false; else res += ',';
|
| 147 | printString(res, *i);
|
| 148 | }
|
| 149 | res += ']';
|
| 150 | }
|
| 151 |
|
| 152 |
|
| 153 | string unparseDerivation(const Derivation & drv)
|
| 154 | {
|
| 155 | string s;
|
| 156 | s.reserve(65536);
|
| 157 | s += "Derive([";
|
| 158 |
|
| 159 | bool first = true;
|
| 160 | for (const auto& i : drv.outputs) {
|
| 161 | if (first) first = false; else s += ',';
|
| 162 | s += '('; printString(s, i.first);
|
| 163 | s += ','; printString(s, i.second.path);
|
| 164 | s += ','; printString(s, i.second.hashAlgo);
|
| 165 | s += ','; printString(s, i.second.hash);
|
| 166 | s += ')';
|
| 167 | }
|
| 168 |
|
| 169 | s += "],[";
|
| 170 | first = true;
|
| 171 | for (const auto& i : drv.inputDrvs) {
|
| 172 | if (first) first = false; else s += ',';
|
| 173 | s += '('; printString(s, i.first);
|
| 174 | s += ','; printStrings(s, i.second.begin(), i.second.end());
|
| 175 | s += ')';
|
| 176 | }
|
| 177 |
|
| 178 | s += "],";
|
| 179 | printStrings(s, drv.inputSrcs.begin(), drv.inputSrcs.end());
|
| 180 |
|
| 181 | s += ','; printString(s, drv.platform);
|
| 182 | s += ','; printString(s, drv.builder);
|
| 183 | s += ','; printStrings(s, drv.args.begin(), drv.args.end());
|
| 184 |
|
| 185 | s += ",[";
|
| 186 | first = true;
|
| 187 | for (const auto& i : drv.env) {
|
| 188 | if (first) first = false; else s += ',';
|
| 189 | s += '('; printString(s, i.first);
|
| 190 | s += ','; printString(s, i.second);
|
| 191 | s += ')';
|
| 192 | }
|
| 193 |
|
| 194 | s += "])";
|
| 195 |
|
| 196 | return s;
|
| 197 | }
|
| 198 |
|
| 199 |
|
| 200 | bool isDerivation(const string & fileName)
|
| 201 | {
|
| 202 | return fileName.ends_with(drvExtension);
|
| 203 | }
|
| 204 |
|
| 205 |
|
| 206 | bool isFixedOutputDrv(const Derivation & drv)
|
| 207 | {
|
| 208 | return drv.outputs.size() == 1 &&
|
| 209 | drv.outputs.begin()->first == "out" &&
|
| 210 | drv.outputs.begin()->second.hash != "";
|
| 211 | }
|
| 212 |
|
| 213 |
|
| 214 | |
| 215 | subderivations. A fixed-output derivation is a derivation with one
|
| 216 | output (`out') for which an expected hash and hash algorithm are
|
| 217 | specified (using the `outputHash' and `outputHashAlgo'
|
| 218 | attributes). We don't want changes to such derivations to
|
| 219 | propagate upwards through the dependency graph, changing output
|
| 220 | paths everywhere.
|
| 221 |
|
| 222 | For instance, if we change the url in a call to the `fetchurl'
|
| 223 | function, we do not want to rebuild everything depending on it
|
| 224 | (after all, (the hash of) the file being downloaded is unchanged).
|
| 225 | So the *output paths* should not change. On the other hand, the
|
| 226 | *derivation paths* should change to reflect the new dependency
|
| 227 | graph.
|
| 228 |
|
| 229 | That's what this function does: it returns a hash which is just the
|
| 230 | hash of the derivation ATerm, except that any input derivation
|
| 231 | paths have been replaced by the result of a recursive call to this
|
| 232 | function, and that for fixed-output derivations we return a hash of
|
| 233 | its output path.
|
| 234 | Hash hashDerivationModulo(StoreAPI & store, Derivation drv)
|
| 235 | {
|
| 236 |
|
| 237 | if (isFixedOutputDrv(drv)) {
|
| 238 | DerivationOutputs::const_iterator i = drv.outputs.begin();
|
| 239 | return hashString(htSHA256, "fixed:out:"
|
| 240 | + i->second.hashAlgo + ":"
|
| 241 | + i->second.hash + ":"
|
| 242 | + i->second.path);
|
| 243 | }
|
| 244 |
|
| 245 | |
| 246 | calls to this function.
|
| 247 | DerivationInputs inputs2;
|
| 248 | for (const auto& i : drv.inputDrvs) {
|
| 249 | Hash h = drvHashes[i.first];
|
| 250 | if (h.type == htUnknown) {
|
| 251 | assert(store.isValidPath(i.first));
|
| 252 | Derivation drv2 = readDerivation(i.first);
|
| 253 | h = hashDerivationModulo(store, drv2);
|
| 254 | drvHashes[i.first] = h;
|
| 255 | }
|
| 256 | inputs2[printHash(h)] = i.second;
|
| 257 | }
|
| 258 | drv.inputDrvs = inputs2;
|
| 259 |
|
| 260 | return hashString(htSHA256, unparseDerivation(drv));
|
| 261 | }
|
| 262 |
|
| 263 |
|
| 264 | DrvPathWithOutputs parseDrvPathWithOutputs(const string & s)
|
| 265 | {
|
| 266 | size_t n = s.find("!");
|
| 267 | return n == s.npos
|
| 268 | ? DrvPathWithOutputs(s, std::set<string>())
|
| 269 | : DrvPathWithOutputs(string(s, 0, n), tokenizeString<std::set<string> >(string(s, n + 1), ","));
|
| 270 | }
|
| 271 |
|
| 272 |
|
| 273 | Path makeDrvPathWithOutputs(const Path & drvPath, const std::set<string> & outputs)
|
| 274 | {
|
| 275 | return outputs.empty()
|
| 276 | ? drvPath
|
| 277 | : drvPath + "!" + concatStringsSep(",", outputs);
|
| 278 | }
|
| 279 |
|
| 280 |
|
| 281 | bool wantOutput(const string & output, const std::set<string> & wanted)
|
| 282 | {
|
| 283 | return wanted.empty() || wanted.find(output) != wanted.end();
|
| 284 | }
|
| 285 |
|
| 286 |
|
| 287 | PathSet outputPaths(const Derivation & drv)
|
| 288 | {
|
| 289 | PathSet paths;
|
| 290 | for (auto & i : drv.outputs)
|
| 291 | paths.insert(i.second.path);
|
| 292 | return paths;
|
| 293 | }
|
| 294 |
|
| 295 |
|
| 296 | }
|