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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "lix/libstore/build/worker.hh"
#include "lix/libstore/build/substitution-goal.hh"
#include "lix/libstore/build/derivation-goal.hh"
#include "lix/libstore/local-store.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/strings.hh"

namespace nix {

kj::Promise<Result<void>> Store::buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode, std::shared_ptr<Store> evalStore)
try {
    auto results =
        TRY_AWAIT(processGoals(*this, evalStore ? *evalStore : *this, [&](GoalFactory & gf) {
            Worker::Targets goals;
            for (auto & br : reqs) {
                goals.emplace_back(gf.makeGoal(br, buildMode));
            }
            return goals;
        }));

    StringSet failed;
    std::shared_ptr<Error> ex;
    for (auto & [i, result] : results.goals) {
        if (result.ex) {
            if (ex)
                logError(result.ex->info());
            else
                ex = result.ex;
        }
        if (result.exitCode != Goal::ecSuccess) {
            if (result.storePath)
                failed.insert(printStorePath(*result.storePath));
        }
    }

    if (failed.size() == 1 && ex) {
        ex->withExitStatus(results.failingExitStatus);
        throw std::move(*ex);
    } else if (!failed.empty()) {
        if (ex) logError(ex->info());
        throw Error(results.failingExitStatus, "build of %s failed", concatStringsSep(", ", quoteStrings(failed)));
    }

    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<std::vector<KeyedBuildResult>>> Store::buildPathsWithResults(
    const std::vector<DerivedPath> & reqs,
    BuildMode buildMode,
    std::shared_ptr<Store> evalStore)
try {
    auto goals =
        TRY_AWAIT(processGoals(*this, evalStore ? *evalStore : *this, [&](GoalFactory & gf) {
            Worker::Targets goals;
            for (const auto & req : reqs) {
                goals.emplace_back(gf.makeGoal(req, buildMode));
            }
            return goals;
        })).goals;

    std::vector<KeyedBuildResult> results;

    for (auto && [goalIdx, req] : enumerate(reqs))
        results.emplace_back(goals[goalIdx].result.restrictTo(req));

    co_return results;
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<BuildResult>> Store::buildDerivation(const StorePath & drvPath, const BasicDerivation & drv,
    BuildMode buildMode)
try {
    try {
        auto results = TRY_AWAIT(processGoals(*this, *this, [&](GoalFactory & gf) {
            // sometimes clang lints are really annoying. this would be safe without
            // the explicit coroutine param captures, but clang-tidy does not see it
            return [](auto && gf, auto && drvPath, auto && drv, auto buildMode
                   ) -> kj::Promise<Result<Worker::Targets>> {
                try {
                    Worker::Targets goals;
                    goals.emplace_back(TRY_AWAIT(
                        gf.makeBasicDerivationGoal(drvPath, drv, OutputsSpec::All{}, buildMode)
                    ));
                    co_return goals;
                } catch (...) {
                    co_return result::current_exception();
                }
            }(gf, drvPath, drv, buildMode);
        }));
        auto & result = results.goals.begin()->second;
        co_return result.result.restrictTo(DerivedPath::Built {
            .drvPath = makeConstantStorePath(drvPath),
            .outputs = OutputsSpec::All {},
        });
    } catch (Error & e) {
        co_return BuildResult {
            .status = BuildResult::MiscFailure,
            .errorMsg = e.msg(),
        };
    };
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<void>> Store::ensurePath(const StorePath & path)
try {
    /* If the path is already valid, we're done. */
    if (TRY_AWAIT(isValidPath(path))) co_return result::success();

    auto results = TRY_AWAIT(processGoals(*this, *this, [&](GoalFactory & gf) {
        Worker::Targets goals;
        goals.emplace_back(gf.makePathSubstitutionGoal(path));
        return goals;
    }));
    auto & result = results.goals.begin()->second;

    if (result.exitCode != Goal::ecSuccess) {
        if (result.ex) {
            result.ex->withExitStatus(results.failingExitStatus);
            throw std::move(*result.ex);
        } else
            throw Error(results.failingExitStatus, "path '%s' does not exist and cannot be created", printStorePath(path));
    }

    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<void>> Store::repairPath(const StorePath & path)
try {
    auto results = TRY_AWAIT(processGoals(*this, *this, [&](GoalFactory & gf) {
        Worker::Targets goals;
        goals.emplace_back(gf.makePathSubstitutionGoal(path, Repair));
        return goals;
    }));
    auto & result = results.goals.begin()->second;

    if (result.exitCode != Goal::ecSuccess) {
        /* Since substituting the path didn't work, if we have a valid
           deriver, then rebuild the deriver. */
        auto info = TRY_AWAIT(queryPathInfo(path));
        if (info->deriver && TRY_AWAIT(isValidPath(*info->deriver))) {
            TRY_AWAIT(processGoals(*this, *this, [&](GoalFactory & gf) {
                Worker::Targets goals;
                goals.emplace_back(gf.makeGoal(
                    DerivedPath::Built{
                        .drvPath = makeConstantStorePath(*info->deriver),
                        // FIXME: Should just build the specific output we need.
                        .outputs = OutputsSpec::All{},
                    },
                    bmRepair
                ));
                return goals;
            }));
        } else
            throw Error(
                results.failingExitStatus,
                "cannot repair path '%s'",
                toRealPath(printStorePath(path))
            );
    }

    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}

}