2019-02-12 19:23:11 +02:00
|
|
|
#include "flakeref.hh"
|
2019-05-31 20:01:11 +03:00
|
|
|
#include "store-api.hh"
|
2020-03-30 15:03:28 +03:00
|
|
|
#include "url.hh"
|
2020-09-21 19:22:45 +03:00
|
|
|
#include "url-parts.hh"
|
2020-03-30 15:03:28 +03:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "registry.hh"
|
2019-02-12 19:23:11 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
#if 0
|
2019-05-01 21:23:39 +03:00
|
|
|
// '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 + ")*";
|
2020-01-21 17:27:53 +02:00
|
|
|
#endif
|
2019-05-01 21:23:39 +03:00
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
std::string FlakeRef::to_string() const
|
2019-02-12 19:23:11 +02:00
|
|
|
{
|
2020-09-30 00:33:16 +03:00
|
|
|
std::map<std::string, std::string> extraQuery;
|
2020-03-28 19:05:50 +02:00
|
|
|
if (subdir != "")
|
2020-09-30 00:33:16 +03:00
|
|
|
extraQuery.insert_or_assign("dir", subdir);
|
|
|
|
return input.toURLString(extraQuery);
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-02-12 19:23:11 +02:00
|
|
|
|
2020-03-17 21:54:36 +02:00
|
|
|
fetchers::Attrs FlakeRef::toAttrs() const
|
2020-01-31 20:16:40 +02:00
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
auto attrs = input.toAttrs();
|
2020-01-31 20:16:40 +02:00
|
|
|
if (subdir != "")
|
2020-02-21 00:44:06 +02:00
|
|
|
attrs.emplace("dir", subdir);
|
2020-01-31 20:16:40 +02:00
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
|
|
|
|
{
|
|
|
|
str << flakeRef.to_string();
|
|
|
|
return str;
|
|
|
|
}
|
2019-05-31 20:01:11 +03:00
|
|
|
|
2020-01-31 20:16:40 +02:00
|
|
|
bool FlakeRef::operator ==(const FlakeRef & other) const
|
2020-01-21 17:27:53 +02:00
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
return input == other.input && subdir == other.subdir;
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-05-31 20:01:11 +03:00
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
FlakeRef FlakeRef::resolve(ref<Store> store) const
|
|
|
|
{
|
2020-02-20 23:14:44 +02:00
|
|
|
auto [input2, extraAttrs] = lookupInRegistries(store, input);
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
return FlakeRef(std::move(input2), fetchers::maybeGetStrAttr(extraAttrs, "dir").value_or(subdir));
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-05-31 20:01:11 +03:00
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
FlakeRef parseFlakeRef(
|
2021-12-13 21:54:43 +02:00
|
|
|
const std::string & url,
|
|
|
|
const std::optional<Path> & baseDir,
|
|
|
|
bool allowMissing,
|
|
|
|
bool isFlake)
|
2020-01-21 17:27:53 +02:00
|
|
|
{
|
2021-12-13 21:54:43 +02:00
|
|
|
auto [flakeRef, fragment] = parseFlakeRefWithFragment(url, baseDir, allowMissing, isFlake);
|
2020-01-21 17:27:53 +02:00
|
|
|
if (fragment != "")
|
|
|
|
throw Error("unexpected fragment '%s' in flake reference '%s'", fragment, url);
|
|
|
|
return flakeRef;
|
|
|
|
}
|
2019-02-12 19:23:11 +02:00
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
std::optional<FlakeRef> maybeParseFlakeRef(
|
|
|
|
const std::string & url, const std::optional<Path> & baseDir)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return parseFlakeRef(url, baseDir);
|
|
|
|
} catch (Error &) {
|
|
|
|
return {};
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-02-12 19:23:11 +02:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
|
2021-12-13 21:54:43 +02:00
|
|
|
const std::string & url,
|
|
|
|
const std::optional<Path> & baseDir,
|
|
|
|
bool allowMissing,
|
|
|
|
bool isFlake)
|
2020-01-21 17:27:53 +02:00
|
|
|
{
|
2023-04-20 07:51:47 +03:00
|
|
|
std::string path = url;
|
|
|
|
std::string fragment = "";
|
|
|
|
std::map<std::string, std::string> query;
|
|
|
|
auto pathEnd = url.find_first_of("#?");
|
|
|
|
auto fragmentStart = pathEnd;
|
|
|
|
if (pathEnd != std::string::npos && url[pathEnd] == '?') {
|
|
|
|
fragmentStart = url.find("#");
|
|
|
|
}
|
|
|
|
if (pathEnd != std::string::npos) {
|
|
|
|
path = url.substr(0, pathEnd);
|
|
|
|
}
|
|
|
|
if (fragmentStart != std::string::npos) {
|
|
|
|
fragment = percentDecode(url.substr(fragmentStart+1));
|
|
|
|
}
|
|
|
|
if (pathEnd != std::string::npos && fragmentStart != std::string::npos) {
|
2023-12-11 17:02:09 +02:00
|
|
|
query = decodeQuery(url.substr(pathEnd+1, fragmentStart-pathEnd-1));
|
2023-04-20 07:51:47 +03:00
|
|
|
}
|
2020-07-17 15:54:21 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
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);
|
2021-12-03 17:53:41 +02:00
|
|
|
}
|
2023-04-20 07:51:47 +03:00
|
|
|
path = dirOf(path);
|
2021-12-03 17:53:41 +02:00
|
|
|
}
|
2023-04-20 07:51:47 +03:00
|
|
|
if (!found)
|
|
|
|
throw BadURL("could not find a flake.nix file");
|
|
|
|
}
|
2022-01-14 16:15:45 +02:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
if (!S_ISDIR(lstat(path).st_mode))
|
|
|
|
throw BadURL("path '%s' is not a flake (because it's not a directory)", path);
|
2020-07-17 15:54:21 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
if (!allowMissing && !pathExists(path + "/flake.nix"))
|
|
|
|
throw BadURL("path '%s' is not a flake (because it doesn't contain a 'flake.nix' file)", path);
|
2020-07-17 15:54:21 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
auto flakeRoot = path;
|
|
|
|
std::string subdir;
|
2020-07-17 15:54:21 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
while (flakeRoot != "/") {
|
|
|
|
if (pathExists(flakeRoot + "/.git")) {
|
|
|
|
auto base = std::string("git+file://") + flakeRoot;
|
2020-07-17 15:54:21 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
auto parsedURL = ParsedURL{
|
|
|
|
.url = base, // FIXME
|
|
|
|
.base = base,
|
|
|
|
.scheme = "git+file",
|
|
|
|
.authority = "",
|
|
|
|
.path = flakeRoot,
|
|
|
|
.query = query,
|
|
|
|
};
|
2020-07-01 15:57:59 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
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);
|
2020-07-17 15:54:21 +03:00
|
|
|
}
|
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
if (pathExists(flakeRoot + "/.git/shallow"))
|
|
|
|
parsedURL.query.insert_or_assign("shallow", "1");
|
|
|
|
|
|
|
|
return std::make_pair(
|
|
|
|
FlakeRef(fetchers::Input::fromURL(parsedURL), getOr(parsedURL.query, "dir", "")),
|
|
|
|
fragment);
|
2020-07-17 15:54:21 +03:00
|
|
|
}
|
2020-04-27 23:53:11 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
subdir = std::string(baseNameOf(flakeRoot)) + (subdir.empty() ? "" : "/" + subdir);
|
|
|
|
flakeRoot = dirOf(flakeRoot);
|
|
|
|
}
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-02-12 19:23:11 +02:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
} else {
|
|
|
|
if (!hasPrefix(path, "/"))
|
|
|
|
throw BadURL("flake reference '%s' is not an absolute path", url);
|
|
|
|
path = canonPath(path + "/" + getOr(query, "dir", ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchers::Attrs attrs;
|
|
|
|
attrs.insert_or_assign("type", "path");
|
|
|
|
attrs.insert_or_assign("path", path);
|
2019-02-12 19:23:11 +02:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
return std::make_pair(FlakeRef(fetchers::Input::fromAttrs(std::move(attrs)), ""), fragment);
|
|
|
|
};
|
2019-05-01 21:23:39 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
|
|
|
|
/* Check if 'url' is a flake ID. This is an abbreviated syntax for
|
|
|
|
'flake:<flake-id>?ref=<ref>&rev=<rev>'. */
|
|
|
|
std::optional<std::pair<FlakeRef, std::string>> parseFlakeIdRef(
|
|
|
|
const std::string & url,
|
|
|
|
bool isFlake
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
static std::regex flakeRegex(
|
|
|
|
"((" + flakeIdRegexS + ")(?:/(?:" + refAndOrRevRegex + "))?)"
|
2023-10-01 23:09:55 +03:00
|
|
|
+ "(?:#(" + fragmentRegex + "))?",
|
2023-04-20 07:51:47 +03:00
|
|
|
std::regex::ECMAScript);
|
2021-09-21 15:07:16 +03:00
|
|
|
|
2023-05-31 11:36:43 +03:00
|
|
|
if (std::regex_match(url, match, flakeRegex)) {
|
|
|
|
auto parsedURL = ParsedURL{
|
|
|
|
.url = url,
|
|
|
|
.base = "flake:" + match.str(1),
|
|
|
|
.scheme = "flake",
|
|
|
|
.authority = "",
|
|
|
|
.path = match[1],
|
|
|
|
};
|
2021-09-21 15:07:16 +03:00
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
return std::make_pair(
|
2023-04-20 07:51:47 +03:00
|
|
|
FlakeRef(fetchers::Input::fromURL(parsedURL, isFlake), ""),
|
2023-05-31 11:36:43 +03:00
|
|
|
percentDecode(match.str(6)));
|
|
|
|
}
|
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<FlakeRef, std::string>> parseURLFlakeRef(
|
|
|
|
const std::string & url,
|
|
|
|
const std::optional<Path> & baseDir,
|
|
|
|
bool isFlake
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ParsedURL parsedURL;
|
|
|
|
try {
|
|
|
|
parsedURL = parseURL(url);
|
|
|
|
} catch (BadURL &) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-05-31 11:36:43 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
std::string fragment;
|
|
|
|
std::swap(fragment, parsedURL.fragment);
|
2023-05-31 11:36:43 +03:00
|
|
|
|
2023-04-20 07:51:47 +03:00
|
|
|
auto input = fetchers::Input::fromURL(parsedURL, isFlake);
|
|
|
|
input.parent = baseDir;
|
|
|
|
|
|
|
|
return std::make_pair(
|
|
|
|
FlakeRef(std::move(input), getOr(parsedURL.query, "dir", "")),
|
|
|
|
fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
|
|
|
|
const std::string & url,
|
|
|
|
const std::optional<Path> & baseDir,
|
|
|
|
bool allowMissing,
|
|
|
|
bool isFlake)
|
|
|
|
{
|
|
|
|
using namespace fetchers;
|
|
|
|
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
if (auto res = parseFlakeIdRef(url, isFlake)) {
|
|
|
|
return *res;
|
|
|
|
} else if (auto res = parseURLFlakeRef(url, baseDir, isFlake)) {
|
|
|
|
return *res;
|
|
|
|
} else {
|
|
|
|
return parsePathFlakeRefWithFragment(url, baseDir, allowMissing, isFlake);
|
2020-01-21 17:27:53 +02:00
|
|
|
}
|
2019-09-19 00:59:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:27:53 +02:00
|
|
|
std::optional<std::pair<FlakeRef, std::string>> maybeParseFlakeRefWithFragment(
|
|
|
|
const std::string & url, const std::optional<Path> & baseDir)
|
2019-05-31 21:53:23 +03:00
|
|
|
{
|
|
|
|
try {
|
2020-01-21 17:27:53 +02:00
|
|
|
return parseFlakeRefWithFragment(url, baseDir);
|
|
|
|
} catch (Error & e) {
|
2019-05-31 21:53:23 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:54:36 +02:00
|
|
|
FlakeRef FlakeRef::fromAttrs(const fetchers::Attrs & attrs)
|
2020-01-31 20:16:40 +02:00
|
|
|
{
|
|
|
|
auto attrs2(attrs);
|
2020-02-21 00:44:06 +02:00
|
|
|
attrs2.erase("dir");
|
2020-01-31 20:16:40 +02:00
|
|
|
return FlakeRef(
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-30 01:44:11 +03:00
|
|
|
fetchers::Input::fromAttrs(std::move(attrs2)),
|
2020-02-21 00:44:06 +02:00
|
|
|
fetchers::maybeGetStrAttr(attrs, "dir").value_or(""));
|
2020-01-31 20:16:40 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 20:50:21 +03:00
|
|
|
std::pair<StorePath, FlakeRef> FlakeRef::fetchTree(ref<Store> store) const
|
2020-02-02 12:31:58 +02:00
|
|
|
{
|
2023-10-20 20:50:21 +03:00
|
|
|
auto [storePath, lockedInput] = input.fetch(store);
|
|
|
|
return {std::move(storePath), FlakeRef(std::move(lockedInput), subdir)};
|
2020-02-02 12:31:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 09:00:44 +02:00
|
|
|
std::tuple<FlakeRef, std::string, ExtendedOutputsSpec> parseFlakeRefWithFragmentAndExtendedOutputsSpec(
|
2022-04-22 16:17:01 +03:00
|
|
|
const std::string & url,
|
|
|
|
const std::optional<Path> & baseDir,
|
|
|
|
bool allowMissing,
|
|
|
|
bool isFlake)
|
|
|
|
{
|
2023-01-11 09:00:44 +02:00
|
|
|
auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse(url);
|
2023-01-12 01:57:18 +02:00
|
|
|
auto [flakeRef, fragment] = parseFlakeRefWithFragment(std::string { prefix }, baseDir, allowMissing, isFlake);
|
2023-08-16 19:29:23 +03:00
|
|
|
return {std::move(flakeRef), fragment, std::move(extendedOutputsSpec)};
|
2022-04-22 16:17:01 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 03:55:41 +03:00
|
|
|
std::regex flakeIdRegex(flakeIdRegexS, std::regex::ECMAScript);
|
|
|
|
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|