aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/flake/flakeref.cc
blob: e1bce90bc9e1bf20fd0d6f8eb05d76020dceaf3f (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "flakeref.hh"
#include "store-api.hh"
#include "url.hh"
#include "url-parts.hh"
#include "fetchers.hh"
#include "registry.hh"

namespace nix {

#if 0
// 'dir' path elements cannot start with a '.'. We also reject
// potentially dangerous characters like ';'.
const static std::string subDirElemRegex = "(?:[a-zA-Z0-9_-]+[a-zA-Z0-9._-]*)";
const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRegex + ")*";
#endif

std::string FlakeRef::to_string() const
{
    std::map<std::string, std::string> extraQuery;
    if (subdir != "")
        extraQuery.insert_or_assign("dir", subdir);
    return input.toURLString(extraQuery);
}

fetchers::Attrs FlakeRef::toAttrs() const
{
    auto attrs = input.toAttrs();
    if (subdir != "")
        attrs.emplace("dir", subdir);
    return attrs;
}

std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
{
    str << flakeRef.to_string();
    return str;
}

bool FlakeRef::operator ==(const FlakeRef & other) const
{
    return input == other.input && subdir == other.subdir;
}

FlakeRef FlakeRef::resolve(ref<Store> store) const
{
    auto [input2, extraAttrs] = lookupInRegistries(store, input);
    return FlakeRef(std::move(input2), fetchers::maybeGetStrAttr(extraAttrs, "dir").value_or(subdir));
}

FlakeRef parseFlakeRef(
    const std::string & url,
    const std::optional<Path> & baseDir,
    bool allowMissing,
    bool isFlake)
{
    auto [flakeRef, fragment] = parseFlakeRefWithFragment(url, baseDir, allowMissing, isFlake);
    if (fragment != "")
        throw Error("unexpected fragment '%s' in flake reference '%s'", fragment, url);
    return flakeRef;
}

std::optional<FlakeRef> maybeParseFlakeRef(
    const std::string & url, const std::optional<Path> & baseDir)
{
    try {
        return parseFlakeRef(url, baseDir);
    } catch (Error &) {
        return {};
    }
}

std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
    const std::string & url,
    const std::optional<Path> & baseDir,
    bool allowMissing,
    bool isFlake)
{
    using namespace fetchers;

    static std::string fnRegex = "[0-9a-zA-Z-._~!$&'\"()*+,;=]+";

    static std::regex pathUrlRegex(
        "(/?" + fnRegex + "(?:/" + fnRegex + ")*/?)"
        + "(?:\\?(" + queryRegex + "))?"
        + "(?:#(" + queryRegex + "))?",
        std::regex::ECMAScript);

    static std::regex flakeRegex(
        "((" + flakeIdRegexS + ")(?:/(?:" + refAndOrRevRegex + "))?)"
        + "(?:#(" + queryRegex + "))?",
        std::regex::ECMAScript);

    std::smatch match;

    /* Check if 'url' is a flake ID. This is an abbreviated syntax for
       'flake:<flake-id>?ref=<ref>&rev=<rev>'. */

    if (std::regex_match(url, match, flakeRegex)) {
        auto parsedURL = ParsedURL{
            .url = url,
            .base = "flake:" + match.str(1),
            .scheme = "flake",
            .authority = "",
            .path = match[1],
        };

        return std::make_pair(
            FlakeRef(Input::fromURL(parsedURL, isFlake), ""),
            percentDecode(match.str(6)));
    }

    else if (std::regex_match(url, match, pathUrlRegex)) {
        std::string path = match[1];
        std::string fragment = percentDecode(match.str(3));

        if (baseDir) {
            /* Check if 'url' is a path (either absolute or relative
               to 'baseDir'). If so, search upward to the root of the
               repo (i.e. the directory containing .git). */

            path = absPath(path, baseDir);

            if (isFlake) {

                if (!allowMissing && !pathExists(path + "/flake.nix")){
                    notice("path '%s' does not contain a 'flake.nix', searching up",path);

                    // Save device to detect filesystem boundary
                    dev_t device = lstat(path).st_dev;
                    bool found = false;
                    while (path != "/") {
                        if (pathExists(path + "/flake.nix")) {
                            found = true;
                            break;
                        } else if (pathExists(path + "/.git"))
                            throw Error("path '%s' is not part of a flake (neither it nor its parent directories contain a 'flake.nix' file)", path);
                        else {
                            if (lstat(path).st_dev != device)
                                throw Error("unable to find a flake before encountering filesystem boundary at '%s'", path);
                        }
                        path = dirOf(path);
                    }
                    if (!found)
                        throw BadURL("could not find a flake.nix file");
                }

                if (!S_ISDIR(lstat(path).st_mode))
                    throw BadURL("path '%s' is not a flake (because it's not a directory)", path);

                if (!allowMissing && !pathExists(path + "/flake.nix"))
                    throw BadURL("path '%s' is not a flake (because it doesn't contain a 'flake.nix' file)", path);

                auto flakeRoot = path;
                std::string subdir;

                while (flakeRoot != "/") {
                    if (pathExists(flakeRoot + "/.git")) {
                        auto base = std::string("git+file://") + flakeRoot;

                        auto parsedURL = ParsedURL{
                            .url = base, // FIXME
                            .base = base,
                            .scheme = "git+file",
                            .authority = "",
                            .path = flakeRoot,
                            .query = decodeQuery(match[2]),
                        };

                        if (subdir != "") {
                            if (parsedURL.query.count("dir"))
                                throw Error("flake URL '%s' has an inconsistent 'dir' parameter", url);
                            parsedURL.query.insert_or_assign("dir", subdir);
                        }

                        if (pathExists(flakeRoot + "/.git/shallow"))
                            parsedURL.query.insert_or_assign("shallow", "1");

                        return std::make_pair(
                            FlakeRef(Input::fromURL(parsedURL, isFlake), getOr(parsedURL.query, "dir", "")),
                            fragment);
                    }

                    subdir = std::string(baseNameOf(flakeRoot)) + (subdir.empty() ? "" : "/" + subdir);
                    flakeRoot = dirOf(flakeRoot);
                }
            }

        } else {
            if (!hasPrefix(path, "/"))
                throw BadURL("flake reference '%s' is not an absolute path", url);
            auto query = decodeQuery(match[2]);
            path = canonPath(path + "/" + getOr(query, "dir", ""));
        }

        fetchers::Attrs attrs;
        attrs.insert_or_assign("type", "path");
        attrs.insert_or_assign("path", path);

        return std::make_pair(FlakeRef(Input::fromAttrs(std::move(attrs)), ""), fragment);
    }

    else {
        auto parsedURL = parseURL(url);
        std::string fragment;
        std::swap(fragment, parsedURL.fragment);

        auto input = Input::fromURL(parsedURL, isFlake);
        input.parent = baseDir;

        return std::make_pair(
            FlakeRef(std::move(input), getOr(parsedURL.query, "dir", "")),
            fragment);
    }
}

std::optional<std::pair<FlakeRef, std::string>> maybeParseFlakeRefWithFragment(
    const std::string & url, const std::optional<Path> & baseDir)
{
    try {
        return parseFlakeRefWithFragment(url, baseDir);
    } catch (Error & e) {
        return {};
    }
}

FlakeRef FlakeRef::fromAttrs(const fetchers::Attrs & attrs)
{
    auto attrs2(attrs);
    attrs2.erase("dir");
    return FlakeRef(
        fetchers::Input::fromAttrs(std::move(attrs2)),
        fetchers::maybeGetStrAttr(attrs, "dir").value_or(""));
}

std::pair<fetchers::Tree, FlakeRef> FlakeRef::fetchTree(ref<Store> store) const
{
    auto [tree, lockedInput] = input.fetch(store);
    return {std::move(tree), FlakeRef(std::move(lockedInput), subdir)};
}

std::tuple<FlakeRef, std::string, ExtendedOutputsSpec> parseFlakeRefWithFragmentAndExtendedOutputsSpec(
    const std::string & url,
    const std::optional<Path> & baseDir,
    bool allowMissing,
    bool isFlake)
{
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse(url);
    auto [flakeRef, fragment] = parseFlakeRefWithFragment(std::string { prefix }, baseDir, allowMissing, isFlake);
    return {std::move(flakeRef), fragment, std::move(extendedOutputsSpec)};
}

}