aboutsummaryrefslogtreecommitdiff
path: root/src/nix/app.cc
blob: 2fcf4752c62776106401a42b87e286f0cf9546a9 (plain)
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
#include "installables.hh"
#include "store-api.hh"
#include "eval-inline.hh"
#include "eval-cache.hh"
#include "names.hh"
#include "command.hh"

namespace nix {

struct InstallableDerivedPath : Installable
{
    ref<Store> store;
    const DerivedPath derivedPath;

    InstallableDerivedPath(ref<Store> store, const DerivedPath & derivedPath)
        : store(store)
        , derivedPath(derivedPath)
    {
    }


    std::string what() override { return derivedPath.to_string(*store); }

    DerivedPaths toDerivedPaths() override
    {
        return {derivedPath};
    }

    std::optional<StorePath> getStorePath() override
    {
        return std::nullopt;
    }
};

/**
 * Return the rewrites that are needed to resolve a string whose context is
 * included in `dependencies`
 */
StringPairs resolveRewrites(Store & store, const BuiltPaths dependencies)
{
    StringPairs res;
    for (auto & dep : dependencies)
        if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep))
            for (auto & [ outputName, outputPath ] : drvDep->outputs)
                res.emplace(
                    downstreamPlaceholder(store, drvDep->drvPath, outputName),
                    store.printStorePath(outputPath)
                );
    return res;
}

/**
 * Resolve the given string assuming the given context
 */
std::string resolveString(Store & store, const std::string & toResolve, const BuiltPaths dependencies)
{
    auto rewrites = resolveRewrites(store, dependencies);
    return rewriteStrings(toResolve, rewrites);
}

UnresolvedApp Installable::toApp(EvalState & state)
{
    auto [cursor, attrPath] = getCursor(state);

    auto type = cursor->getAttr("type")->getString();

    if (type == "app") {
        auto [program, context] = cursor->getAttr("program")->getStringWithContext();


        std::vector<StorePathWithOutputs> context2;
        for (auto & [path, name] : context)
            context2.push_back({state.store->parseStorePath(path), {name}});

        return UnresolvedApp{App {
            .context = std::move(context2),
            .program = program,
        }};
    }

    else if (type == "derivation") {
        auto drvPath = cursor->forceDerivation();
        auto outPath = cursor->getAttr(state.sOutPath)->getString();
        auto outputName = cursor->getAttr(state.sOutputName)->getString();
        auto name = cursor->getAttr(state.sName)->getString();
        auto aPname = cursor->maybeGetAttr("pname");
        auto aMeta = cursor->maybeGetAttr("meta");
        auto aMainProgram = aMeta ? aMeta->maybeGetAttr("mainProgram") : nullptr;
        auto mainProgram =
            aMainProgram
            ? aMainProgram->getString()
            : aPname
            ? aPname->getString()
            : DrvName(name).name;
        auto program = outPath + "/bin/" + mainProgram;
        return UnresolvedApp { App {
            .context = { { drvPath, {outputName} } },
            .program = program,
        }};
    }

    else
        throw Error("attribute '%s' has unsupported type '%s'", attrPath, type);
}

// FIXME: move to libcmd
App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
{
    auto res = unresolved;

    std::vector<std::shared_ptr<Installable>> installableContext;

    for (auto & ctxElt : unresolved.context)
        installableContext.push_back(
            std::make_shared<InstallableDerivedPath>(store, ctxElt.toDerivedPath()));

    auto builtContext = build(evalStore, store, Realise::Outputs, installableContext);
    res.program = resolveString(*store, unresolved.program, builtContext);
    if (!store->isInStore(res.program))
        throw Error("app program '%s' is not in the Nix store", res.program);

    return res;
}

}