Allow bare flakerefs as installables

So now

  $ nix build blender-bin

works and builds the default package from that flake. You don't need
to add a colon at the end anymore.
This commit is contained in:
Eelco Dolstra 2019-05-31 20:53:23 +02:00
parent 7adb10d29b
commit ccb1bad612
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
4 changed files with 32 additions and 10 deletions

View file

@ -65,7 +65,7 @@ FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
auto handleSubdir = [&](const std::string & name, const std::string & value) {
if (name == "dir") {
if (value != "" && !std::regex_match(value, subDirRegex2))
throw Error("flake '%s' has invalid subdirectory '%s'", uri, value);
throw BadFlakeRef("flake '%s' has invalid subdirectory '%s'", uri, value);
subdir = value;
return true;
} else
@ -75,11 +75,11 @@ FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
auto handleGitParams = [&](const std::string & name, const std::string & value) {
if (name == "rev") {
if (!std::regex_match(value, revRegex))
throw Error("invalid Git revision '%s'", value);
throw BadFlakeRef("invalid Git revision '%s'", value);
rev = Hash(value, htSHA1);
} else if (name == "ref") {
if (!std::regex_match(value, refRegex2))
throw Error("invalid Git ref '%s'", value);
throw BadFlakeRef("invalid Git ref '%s'", value);
ref = value;
} else if (handleSubdir(name, value))
;
@ -114,7 +114,7 @@ FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
if (handleSubdir(param.first, param.second))
;
else
throw Error("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
}
data = d;
}
@ -129,14 +129,16 @@ FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
;
else
// FIXME: should probably pass through unknown parameters
throw Error("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
}
if (rev && !ref)
throw Error("flake URI '%s' lacks a Git ref", uri);
throw BadFlakeRef("flake URI '%s' lacks a Git ref", uri);
data = d;
}
else if (hasPrefix(uri, "/") || (allowRelative && (hasPrefix(uri, "./") || hasPrefix(uri, "../") || uri == "."))) {
else if ((hasPrefix(uri, "/") || (allowRelative && (hasPrefix(uri, "./") || hasPrefix(uri, "../") || uri == ".")))
&& uri.find(':') == std::string::npos)
{
IsPath d;
d.path = allowRelative ? absPath(uri) : canonPath(uri);
data = d;
@ -144,12 +146,12 @@ FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
if (handleGitParams(param.first, param.second))
;
else
throw Error("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
}
}
else
throw Error("'%s' is not a valid flake reference", uri);
throw BadFlakeRef("'%s' is not a valid flake reference", uri);
}
std::string FlakeRef::to_string() const
@ -225,4 +227,15 @@ FlakeRef FlakeRef::baseRef() const // Removes the ref and rev from a FlakeRef.
result.rev = std::nullopt;
return result;
}
std::optional<FlakeRef> parseFlakeRef(
const std::string & uri, bool allowRelative)
{
try {
return FlakeRef(uri, allowRelative);
} catch (BadFlakeRef & e) {
return {};
}
}
}

View file

@ -180,4 +180,9 @@ struct FlakeRef
std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef);
MakeError(BadFlakeRef, Error);
std::optional<FlakeRef> parseFlakeRef(
const std::string & uri, bool allowRelative = false);
}

View file

@ -332,6 +332,10 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
getDefaultFlakeAttrPaths()));
}
else if (auto flakeRef = parseFlakeRef(s, true))
result.push_back(std::make_shared<InstallableFlake>(*this, s,
getDefaultFlakeAttrPaths()));
else
result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef("nixpkgs"), s));
}

View file

@ -131,7 +131,7 @@ nix build -o $TEST_ROOT/result --flake-registry $registry flake1:foo
[[ -e $TEST_ROOT/result/hello ]]
# Test defaultPackage.
nix build -o $TEST_ROOT/result --flake-registry $registry flake1:
nix build -o $TEST_ROOT/result --flake-registry $registry flake1
[[ -e $TEST_ROOT/result/hello ]]
# Building a flake with an unlocked dependency should fail in pure mode.