aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/flake/lockfile.cc
blob: 3d796eadc8b92cdda95d9c494a3ec920562487b7 (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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "lockfile.hh"
#include "store-api.hh"
#include "fetchers/regex.hh"

#include <nlohmann/json.hpp>

namespace nix::flake {

FlakeRef flakeRefFromJson(const nlohmann::json & json)
{
    fetchers::Input::Attrs attrs;

    for (auto & i : json.items()) {
        if (i.value().is_number())
            attrs.emplace(i.key(), i.value().get<int64_t>());
        else if (i.value().is_string())
            attrs.emplace(i.key(), i.value().get<std::string>());
        else
            throw Error("unsupported input attribute type in lock file");
    }

    return FlakeRef::fromAttrs(attrs);
}

FlakeRef getFlakeRef(
    const nlohmann::json & json,
    const char * version3Attr1,
    const char * version3Attr2,
    const char * version4Attr)
{
    auto i = json.find(version4Attr);
    if (i != json.end())
        return flakeRefFromJson(*i);

    // FIXME: remove these.
    i = json.find(version3Attr1);
    if (i != json.end())
        return parseFlakeRef(*i);

    i = json.find(version3Attr2);
    if (i != json.end())
        return parseFlakeRef(*i);

    throw Error("attribute '%s' missing in lock file", version4Attr);
}

static TreeInfo parseTreeInfo(const nlohmann::json & json)
{
    TreeInfo info;

    auto i = json.find("info");
    if (i != json.end()) {
        const nlohmann::json & i2(*i);

        auto j = i2.find("narHash");
        if (j != i2.end())
            info.narHash = Hash((std::string) *j);
        else
            throw Error("attribute 'narHash' missing in lock file");

        j = i2.find("revCount");
        if (j != i2.end())
            info.revCount = *j;

        j = i2.find("lastModified");
        if (j != i2.end())
            info.lastModified = *j;

        return info;
    }

    i = json.find("narHash");
    if (i != json.end()) {
        info.narHash = Hash((std::string) *i);
        return info;
    }

    throw Error("attribute 'info' missing in lock file");
}

LockedInput::LockedInput(const nlohmann::json & json)
    : LockedInputs(json)
    , lockedRef(getFlakeRef(json, "url", "uri", "locked"))
    , originalRef(getFlakeRef(json, "originalUrl", "originalUri", "original"))
    , info(parseTreeInfo(json))
{
    if (!lockedRef.input->isImmutable())
        throw Error("lockfile contains mutable flakeref '%s'", lockedRef);
}

static nlohmann::json treeInfoToJson(const TreeInfo & info)
{
    nlohmann::json json;
    assert(info.narHash);
    json["narHash"] = info.narHash.to_string(SRI);
    if (info.revCount)
        json["revCount"] = *info.revCount;
    if (info.lastModified)
        json["lastModified"] = *info.lastModified;
    return json;
}

nlohmann::json LockedInput::toJson() const
{
    auto json = LockedInputs::toJson();
    json["original"] = fetchers::attrsToJson(originalRef.toAttrs());
    json["locked"] = fetchers::attrsToJson(lockedRef.toAttrs());
    json["info"] = treeInfoToJson(info);
    return json;
}

StorePath LockedInput::computeStorePath(Store & store) const
{
    return info.computeStorePath(store);
}

LockedInputs::LockedInputs(const nlohmann::json & json)
{
    for (auto & i : json["inputs"].items())
        inputs.insert_or_assign(i.key(), LockedInput(i.value()));
}

nlohmann::json LockedInputs::toJson() const
{
    nlohmann::json json;
    {
        auto j = nlohmann::json::object();
        for (auto & i : inputs)
            j[i.first] = i.second.toJson();
        json["inputs"] = std::move(j);
    }
    return json;
}

bool LockedInputs::isImmutable() const
{
    for (auto & i : inputs)
        if (!i.second.lockedRef.input->isImmutable() || !i.second.isImmutable()) return false;

    return true;
}

std::optional<LockedInput *> LockedInputs::findInput(const InputPath & path)
{
    assert(!path.empty());

    LockedInputs * pos = this;

    for (auto & elem : path) {
        auto i = pos->inputs.find(elem);
        if (i == pos->inputs.end())
            return {};
        pos = &i->second;
    }

    return (LockedInput *) pos;
}

void LockedInputs::removeInput(const InputPath & path)
{
    assert(!path.empty());

    LockedInputs * pos = this;

    for (size_t n = 0; n < path.size(); n++) {
        auto i = pos->inputs.find(path[n]);
        if (i == pos->inputs.end()) return;
        if (n + 1 == path.size())
            pos->inputs.erase(i);
        else
            pos = &i->second;
    }
}

nlohmann::json LockFile::toJson() const
{
    auto json = LockedInputs::toJson();
    json["version"] = 4;
    return json;
}

LockFile LockFile::read(const Path & path)
{
    if (pathExists(path)) {
        auto json = nlohmann::json::parse(readFile(path));

        auto version = json.value("version", 0);
        if (version != 3 && version != 4)
            throw Error("lock file '%s' has unsupported version %d", path, version);

        return LockFile(json);
    } else
        return LockFile();
}

std::ostream & operator <<(std::ostream & stream, const LockFile & lockFile)
{
    stream << lockFile.toJson().dump(2);
    return stream;
}

void LockFile::write(const Path & path) const
{
    createDirs(dirOf(path));
    writeFile(path, fmt("%s\n", *this));
}

InputPath parseInputPath(std::string_view s)
{
    InputPath path;

    for (auto & elem : tokenizeString<std::vector<std::string>>(s, "/")) {
        if (!std::regex_match(elem, fetchers::flakeIdRegex))
            throw Error("invalid flake input path element '%s'", elem);
        path.push_back(elem);
    }

    if (path.empty())
        throw Error("flake input path is empty");

    return path;
}

}