about summary refs log tree commit diff
path: root/src/nix/installables.cc
blob: 8341bbc5a3a41a4bed3336a75639471501f5a147 (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
#include "attr-path.hh"
#include "common-opts.hh"
#include "derivations.hh"
#include "eval-inline.hh"
#include "eval.hh"
#include "get-drvs.hh"
#include "installables.hh"
#include "store-api.hh"

namespace nix {

Value * MixInstallables::buildSourceExpr(EvalState & state)
{
    Value * vRoot = state.allocValue();

    if (file != "") {
        Expr * e = state.parseExprFromFile(resolveExprPath(lookupFileArg(state, file)));
        state.eval(e, *vRoot);
    }

    else {

        /* Construct the installation source from $NIX_PATH. */

        auto searchPath = state.getSearchPath();

        state.mkAttrs(*vRoot, searchPath.size());

        std::unordered_set<std::string> seen;

        for (auto & i : searchPath) {
            if (i.first == "") continue;
            if (seen.count(i.first)) continue;
            seen.insert(i.first);
            if (!pathExists(i.second)) continue;
            mkApp(*state.allocAttr(*vRoot, state.symbols.create(i.first)),
                state.getBuiltin("import"),
                mkString(*state.allocValue(), i.second));
        }

        vRoot->attrs->sort();
    }

    return vRoot;
}

UserEnvElems MixInstallables::evalInstallables(ref<Store> store)
{
    UserEnvElems res;

    for (auto & installable : installables) {

        if (std::string(installable, 0, 1) == "/") {

            if (store->isStorePath(installable)) {

                if (isDerivation(installable)) {
                    UserEnvElem elem;
                    // FIXME: handle empty case, drop version
                    elem.attrPath = {storePathToName(installable)};
                    elem.isDrv = true;
                    elem.drvPath = installable;
                    res.push_back(elem);
                }

                else {
                    UserEnvElem elem;
                    // FIXME: handle empty case, drop version
                    elem.attrPath = {storePathToName(installable)};
                    elem.isDrv = false;
                    elem.outPaths = {installable};
                    res.push_back(elem);
                }
            }

            else
                throw UsageError(format("don't know what to do with ‘%1%’") % installable);
        }

        else {

            EvalState state({}, store);

            auto vRoot = buildSourceExpr(state);

            std::map<string, string> autoArgs_;
            Bindings & autoArgs(*evalAutoArgs(state, autoArgs_));

            Value & v(*findAlongAttrPath(state, installable, autoArgs, *vRoot));
            state.forceValue(v);

            DrvInfos drvs;
            getDerivations(state, v, "", autoArgs, drvs, false);

            for (auto & i : drvs) {
                UserEnvElem elem;
                elem.isDrv = true;
                elem.drvPath = i.queryDrvPath();
                res.push_back(elem);
            }
        }
    }

    return res;
}

}