|
| 1 | +namespace nix { |
| 2 | + |
| 3 | +struct ExternalDerivationBuilder : DerivationBuilderImpl |
| 4 | +{ |
| 5 | + Settings::ExternalBuilder externalBuilder; |
| 6 | + |
| 7 | + ExternalDerivationBuilder( |
| 8 | + LocalStore & store, |
| 9 | + std::unique_ptr<DerivationBuilderCallbacks> miscMethods, |
| 10 | + DerivationBuilderParams params, |
| 11 | + Settings::ExternalBuilder externalBuilder) |
| 12 | + : DerivationBuilderImpl(store, std::move(miscMethods), std::move(params)) |
| 13 | + , externalBuilder(std::move(externalBuilder)) |
| 14 | + { |
| 15 | + experimentalFeatureSettings.require(Xp::ExternalBuilders); |
| 16 | + } |
| 17 | + |
| 18 | + static std::unique_ptr<ExternalDerivationBuilder> newIfSupported( |
| 19 | + LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> & miscMethods, DerivationBuilderParams & params) |
| 20 | + { |
| 21 | + for (auto & handler : settings.externalBuilders.get()) { |
| 22 | + for (auto & system : handler.systems) |
| 23 | + if (params.drv.platform == system) |
| 24 | + return std::make_unique<ExternalDerivationBuilder>( |
| 25 | + store, std::move(miscMethods), std::move(params), handler); |
| 26 | + } |
| 27 | + return {}; |
| 28 | + } |
| 29 | + |
| 30 | + Path tmpDirInSandbox() override |
| 31 | + { |
| 32 | + /* In a sandbox, for determinism, always use the same temporary |
| 33 | + directory. */ |
| 34 | + return "/build"; |
| 35 | + } |
| 36 | + |
| 37 | + void setBuildTmpDir() override |
| 38 | + { |
| 39 | + tmpDir = topTmpDir + "/build"; |
| 40 | + createDir(tmpDir, 0700); |
| 41 | + } |
| 42 | + |
| 43 | + void checkSystem() override {} |
| 44 | + |
| 45 | + void startChild() override |
| 46 | + { |
| 47 | + if (drvOptions.getRequiredSystemFeatures(drv).count("recursive-nix")) |
| 48 | + throw Error("'recursive-nix' is not supported yet by external derivation builders"); |
| 49 | + |
| 50 | + auto json = nlohmann::json::object(); |
| 51 | + |
| 52 | + json.emplace("version", 1); |
| 53 | + json.emplace("builder", drv.builder); |
| 54 | + { |
| 55 | + auto l = nlohmann::json::array(); |
| 56 | + for (auto & i : drv.args) |
| 57 | + l.push_back(rewriteStrings(i, inputRewrites)); |
| 58 | + json.emplace("args", std::move(l)); |
| 59 | + } |
| 60 | + { |
| 61 | + auto j = nlohmann::json::object(); |
| 62 | + for (auto & [name, value] : env) |
| 63 | + j.emplace(name, rewriteStrings(value, inputRewrites)); |
| 64 | + json.emplace("env", std::move(j)); |
| 65 | + } |
| 66 | + json.emplace("topTmpDir", topTmpDir); |
| 67 | + json.emplace("tmpDir", tmpDir); |
| 68 | + json.emplace("tmpDirInSandbox", tmpDirInSandbox()); |
| 69 | + json.emplace("storeDir", store.storeDir); |
| 70 | + json.emplace("realStoreDir", store.config->realStoreDir.get()); |
| 71 | + json.emplace("system", drv.platform); |
| 72 | + { |
| 73 | + auto l = nlohmann::json::array(); |
| 74 | + for (auto & i : inputPaths) |
| 75 | + l.push_back(store.printStorePath(i)); |
| 76 | + json.emplace("inputPaths", std::move(l)); |
| 77 | + } |
| 78 | + { |
| 79 | + auto l = nlohmann::json::object(); |
| 80 | + for (auto & i : scratchOutputs) |
| 81 | + l.emplace(i.first, store.printStorePath(i.second)); |
| 82 | + json.emplace("outputs", std::move(l)); |
| 83 | + } |
| 84 | + |
| 85 | + // TODO(cole-h): writing this to stdin is too much effort right now, if we want to revisit |
| 86 | + // that, see this comment by Eelco about how to make it not suck: |
| 87 | + // https://github.com/DeterminateSystems/nix-src/pull/141#discussion_r2205493257 |
| 88 | + auto jsonFile = std::filesystem::path{topTmpDir} / "build.json"; |
| 89 | + writeFile(jsonFile, json.dump()); |
| 90 | + |
| 91 | + pid = startProcess([&]() { |
| 92 | + openSlave(); |
| 93 | + try { |
| 94 | + commonChildInit(); |
| 95 | + |
| 96 | + Strings args = {externalBuilder.program}; |
| 97 | + |
| 98 | + if (!externalBuilder.args.empty()) { |
| 99 | + args.insert(args.end(), externalBuilder.args.begin(), externalBuilder.args.end()); |
| 100 | + } |
| 101 | + |
| 102 | + args.insert(args.end(), jsonFile); |
| 103 | + |
| 104 | + if (chdir(tmpDir.c_str()) == -1) |
| 105 | + throw SysError("changing into '%1%'", tmpDir); |
| 106 | + |
| 107 | + chownToBuilder(topTmpDir); |
| 108 | + |
| 109 | + setUser(); |
| 110 | + |
| 111 | + debug("executing external builder: %s", concatStringsSep(" ", args)); |
| 112 | + execv(externalBuilder.program.c_str(), stringsToCharPtrs(args).data()); |
| 113 | + |
| 114 | + throw SysError("executing '%s'", externalBuilder.program); |
| 115 | + } catch (...) { |
| 116 | + handleChildException(true); |
| 117 | + _exit(1); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +} // namespace nix |
0 commit comments