Give errors in resolveFlake

If DontUpdate but the lockfile isn't correct
This commit is contained in:
Nick Van den Broeck 2019-05-01 11:38:48 +02:00
parent d9ad3723d5
commit 98f20dee41
6 changed files with 70 additions and 41 deletions

View file

@ -50,8 +50,7 @@ LockFile::FlakeEntry readFlakeEntry(nlohmann::json json)
if (!flakeRef.isImmutable()) if (!flakeRef.isImmutable())
throw Error("cannot use mutable flake '%s' in pure mode", flakeRef); throw Error("cannot use mutable flake '%s' in pure mode", flakeRef);
Hash hash = Hash((std::string) json["contentHash"]); LockFile::FlakeEntry entry(flakeRef, Hash((std::string) json["contentHash"]));
LockFile::FlakeEntry entry(flakeRef, hash);
auto nonFlakeRequires = json["nonFlakeRequires"]; auto nonFlakeRequires = json["nonFlakeRequires"];
@ -59,9 +58,8 @@ LockFile::FlakeEntry readFlakeEntry(nlohmann::json json)
FlakeRef flakeRef(i->value("uri", "")); FlakeRef flakeRef(i->value("uri", ""));
if (!flakeRef.isImmutable()) if (!flakeRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef); throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef);
Hash hash = Hash((std::string) i->value("contentHash", "")); LockFile::NonFlakeEntry nonEntry(flakeRef, Hash(i->value("contentHash", "")));
LockFile::NonFlakeEntry newEntry(flakeRef, hash); entry.nonFlakeEntries.insert_or_assign(i.key(), nonEntry);
entry.nonFlakeEntries.insert_or_assign(i.key(), newEntry);
} }
auto requires = json["requires"]; auto requires = json["requires"];
@ -89,10 +87,10 @@ LockFile readLockFile(const Path & path)
for (auto i = nonFlakeRequires.begin(); i != nonFlakeRequires.end(); ++i) { for (auto i = nonFlakeRequires.begin(); i != nonFlakeRequires.end(); ++i) {
FlakeRef flakeRef(i->value("uri", "")); FlakeRef flakeRef(i->value("uri", ""));
LockFile::NonFlakeEntry entry(flakeRef, Hash((std::string) json["contentHash"])); LockFile::NonFlakeEntry nonEntry(flakeRef, Hash(i->value("contentHash", "")));
if (!flakeRef.isImmutable()) if (!flakeRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", flakeRef); throw Error("found mutable FlakeRef '%s' in lockfile at path %s", flakeRef, path);
lockFile.nonFlakeEntries.insert_or_assign(i.key(), entry); lockFile.nonFlakeEntries.insert_or_assign(i.key(), nonEntry);
} }
auto requires = json["requires"]; auto requires = json["requires"];
@ -374,19 +372,25 @@ LockFile entryToLockFile(const LockFile::FlakeEntry & entry)
return lockFile; return lockFile;
} }
ResolvedFlake resolveFlakeFromLockFile(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, ResolvedFlake resolveFlakeFromLockFile(EvalState & state, const FlakeRef & flakeRef,
LockFile lockFile, bool isTopFlake = false) ShouldUpdateLockFile update, LockFile lockFile = {})
{ {
bool allowRegistries = registryAccess == AllowRegistry || (registryAccess == AllowRegistryAtTop && isTopFlake); Flake flake = getFlake(state, flakeRef, update != DontUpdate);
Flake flake = getFlake(state, flakeRef, allowRegistries);
ResolvedFlake deps(flake); ResolvedFlake deps(flake);
for (auto & nonFlakeInfo : flake.nonFlakeRequires) { for (auto & nonFlakeInfo : flake.nonFlakeRequires) {
FlakeRef ref = nonFlakeInfo.second; FlakeRef ref = nonFlakeInfo.second;
auto i = lockFile.nonFlakeEntries.find(nonFlakeInfo.first); auto i = lockFile.nonFlakeEntries.find(nonFlakeInfo.first);
if (i != lockFile.nonFlakeEntries.end()) ref = i->second.ref; if (i != lockFile.nonFlakeEntries.end()) {
deps.nonFlakeDeps.push_back(getNonFlake(state, ref, nonFlakeInfo.first)); NonFlake nonFlake = getNonFlake(state, i->second.ref, nonFlakeInfo.first);
if (nonFlake.hash != i->second.contentHash)
throw Error("the content hash of flakeref %s doesn't match", i->second.ref.to_string());
deps.nonFlakeDeps.push_back(nonFlake);
} else {
if (update == DontUpdate) throw Error("the lockfile requires updating nonflake dependency %s in DontUpdate mode", nonFlakeInfo.first);
deps.nonFlakeDeps.push_back(getNonFlake(state, nonFlakeInfo.second, nonFlakeInfo.first));
}
} }
for (auto newFlakeRef : flake.requires) { for (auto newFlakeRef : flake.requires) {
@ -394,10 +398,14 @@ ResolvedFlake resolveFlakeFromLockFile(EvalState & state, const FlakeRef & flake
LockFile newLockFile; LockFile newLockFile;
auto i = lockFile.flakeEntries.find(newFlakeRef); auto i = lockFile.flakeEntries.find(newFlakeRef);
if (i != lockFile.flakeEntries.end()) { // Propagate lockFile downwards if possible if (i != lockFile.flakeEntries.end()) { // Propagate lockFile downwards if possible
ref = i->second.ref; ResolvedFlake newResFlake = resolveFlakeFromLockFile(state, i->second.ref, update, entryToLockFile(i->second));
newLockFile = entryToLockFile(i->second); if (newResFlake.flake.hash != i->second.contentHash)
throw Error("the content hash of flakeref %s doesn't match", i->second.ref.to_string());
deps.flakeDeps.push_back(newResFlake);
} else {
if (update == DontUpdate) throw Error("the lockfile requires updating flake dependency %s in DontUpdate mode", newFlakeRef.to_string());
deps.flakeDeps.push_back(resolveFlakeFromLockFile(state, newFlakeRef, update));
} }
deps.flakeDeps.push_back(resolveFlakeFromLockFile(state, ref, registryAccess, newLockFile));
} }
return deps; return deps;
@ -406,17 +414,18 @@ ResolvedFlake resolveFlakeFromLockFile(EvalState & state, const FlakeRef & flake
/* Given a flake reference, recursively fetch it and its dependencies. /* Given a flake reference, recursively fetch it and its dependencies.
FIXME: this should return a graph of flakes. FIXME: this should return a graph of flakes.
*/ */
ResolvedFlake resolveFlake(EvalState & state, const FlakeRef & topRef, RegistryAccess registryAccess, ResolvedFlake resolveFlake(EvalState & state, const FlakeRef & topRef, ShouldUpdateLockFile update)
bool recreateLockFile)
{ {
bool allowRegistries = registryAccess == AllowRegistry || registryAccess == AllowRegistryAtTop; if (!std::get_if<FlakeRef::IsPath>(&topRef.data)) update = DontUpdate;
Flake flake = getFlake(state, topRef, allowRegistries); Flake flake = getFlake(state, topRef, update != DontUpdate);
LockFile lockFile; LockFile lockFile;
if (!recreateLockFile) // If recreateLockFile, start with an empty lockfile if (update != RecreateLockFile) {
// If recreateLockFile, start with an empty lockfile
lockFile = readLockFile(flake.storePath + "/flake.lock"); // FIXME: symlink attack lockFile = readLockFile(flake.storePath + "/flake.lock"); // FIXME: symlink attack
}
return resolveFlakeFromLockFile(state, topRef, registryAccess, lockFile, true); return resolveFlakeFromLockFile(state, topRef, update, lockFile);
} }
LockFile::FlakeEntry dependenciesToFlakeEntry(const ResolvedFlake & resolvedFlake) LockFile::FlakeEntry dependenciesToFlakeEntry(const ResolvedFlake & resolvedFlake)
@ -426,15 +435,17 @@ LockFile::FlakeEntry dependenciesToFlakeEntry(const ResolvedFlake & resolvedFlak
for (auto & newResFlake : resolvedFlake.flakeDeps) for (auto & newResFlake : resolvedFlake.flakeDeps)
entry.flakeEntries.insert_or_assign(newResFlake.flake.originalRef, dependenciesToFlakeEntry(newResFlake)); entry.flakeEntries.insert_or_assign(newResFlake.flake.originalRef, dependenciesToFlakeEntry(newResFlake));
for (auto & nonFlake : resolvedFlake.nonFlakeDeps) for (auto & nonFlake : resolvedFlake.nonFlakeDeps) {
entry.nonFlakeEntries.insert_or_assign(nonFlake.alias, LockFile::NonFlakeEntry(nonFlake.resolvedRef, nonFlake.hash)); LockFile::NonFlakeEntry nonEntry(nonFlake.resolvedRef, nonFlake.hash);
entry.nonFlakeEntries.insert_or_assign(nonFlake.alias, nonEntry);
}
return entry; return entry;
} }
static LockFile makeLockFile(EvalState & evalState, FlakeRef & flakeRef, bool recreateLockFile) static LockFile makeLockFile(EvalState & evalState, FlakeRef & flakeRef, bool recreateLockFile)
{ {
ResolvedFlake resFlake = resolveFlake(evalState, flakeRef, AllowRegistry, recreateLockFile); ResolvedFlake resFlake = resolveFlake(evalState, flakeRef, recreateLockFile ? RecreateLockFile : UpdateLockFile);
return entryToLockFile(dependenciesToFlakeEntry(resFlake)); return entryToLockFile(dependenciesToFlakeEntry(resFlake));
} }
@ -501,17 +512,16 @@ void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
// Return the `provides` of the top flake, while assigning to `v` the provides // Return the `provides` of the top flake, while assigning to `v` the provides
// of the dependencies as well. // of the dependencies as well.
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v, bool recreateLockFile) void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, ShouldUpdateLockFile update, Value & v)
{ {
callFlake(state, resolveFlake(state, flakeRef, registryAccess, recreateLockFile), v); callFlake(state, resolveFlake(state, flakeRef, update), v);
} }
// This function is exposed to be used in nix files. // This function is exposed to be used in nix files.
static void prim_getFlake(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_getFlake(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
makeFlakeValue(state, state.forceStringNoCtx(*args[0], pos), makeFlakeValue(state, state.forceStringNoCtx(*args[0], pos),
evalSettings.pureEval ? DisallowRegistry : AllowRegistryAtTop, v, false); evalSettings.pureEval ? DontUpdate : UpdateLockFile, v);
// `recreateLockFile == false` because this is the evaluation stage, which should be pure, and hence not recreate lockfiles.
} }
static RegisterPrimOp r2("getFlake", 1, prim_getFlake); static RegisterPrimOp r2("getFlake", 1, prim_getFlake);

View file

@ -43,9 +43,9 @@ typedef std::vector<std::shared_ptr<FlakeRegistry>> Registries;
Path getUserRegistryPath(); Path getUserRegistryPath();
enum RegistryAccess { DisallowRegistry, AllowRegistry, AllowRegistryAtTop }; enum ShouldUpdateLockFile { DontUpdate, UpdateLockFile, RecreateLockFile};
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v, bool recreateLockFile); void makeFlakeValue(EvalState &, const FlakeRef &, ShouldUpdateLockFile, Value &);
std::shared_ptr<FlakeRegistry> readRegistry(const Path &); std::shared_ptr<FlakeRegistry> readRegistry(const Path &);
@ -84,8 +84,8 @@ struct NonFlake
FlakeRef originalRef; FlakeRef originalRef;
FlakeRef resolvedRef; FlakeRef resolvedRef;
std::optional<uint64_t> revCount; std::optional<uint64_t> revCount;
Hash hash;
Path storePath; Path storePath;
Hash hash; // content hash
// date // date
NonFlake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef), NonFlake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef),
resolvedRef(sourceInfo.resolvedRef), revCount(sourceInfo.revCount), storePath(sourceInfo.storePath) {}; resolvedRef(sourceInfo.resolvedRef), revCount(sourceInfo.revCount), storePath(sourceInfo.storePath) {};
@ -103,7 +103,7 @@ struct ResolvedFlake
ResolvedFlake(const Flake & flake) : flake(flake) {} ResolvedFlake(const Flake & flake) : flake(flake) {}
}; };
ResolvedFlake resolveFlake(EvalState &, const FlakeRef &, RegistryAccess, bool recreateLockFile); ResolvedFlake resolveFlake(EvalState &, const FlakeRef &, ShouldUpdateLockFile);
void updateLockFile(EvalState &, const FlakeUri &, bool recreateLockFile); void updateLockFile(EvalState &, const FlakeUri &, bool recreateLockFile);

View file

@ -114,8 +114,7 @@ struct CmdFlakeDeps : FlakeCommand, MixJSON, StoreCommand, MixEvalArgs
FlakeRef flakeRef(flakeUri); FlakeRef flakeRef(flakeUri);
bool recreateLockFile = false; ResolvedFlake resFlake = resolveFlake(*evalState, flakeRef, UpdateLockFile);
ResolvedFlake resFlake = resolveFlake(*evalState, flakeRef, AllowRegistryAtTop, recreateLockFile);
std::queue<ResolvedFlake> todo; std::queue<ResolvedFlake> todo;
todo.push(resFlake); todo.push(resFlake);

View file

@ -161,7 +161,7 @@ struct InstallableFlake : InstallableValue
if (std::get_if<FlakeRef::IsPath>(&flakeRef.data)) if (std::get_if<FlakeRef::IsPath>(&flakeRef.data))
updateLockFile(state, flakeRef.to_string(), cmd.recreateLockFile); updateLockFile(state, flakeRef.to_string(), cmd.recreateLockFile);
makeFlakeValue(state, flakeRef, AllowRegistryAtTop, *vFlake, cmd.recreateLockFile); makeFlakeValue(state, flakeRef, cmd.recreateLockFile ? RecreateLockFile : UpdateLockFile, *vFlake);
auto vProvides = (*vFlake->attrs->get(state.symbols.create("provides")))->value; auto vProvides = (*vFlake->attrs->get(state.symbols.create("provides")))->value;

20
tests/config.nix Normal file
View file

@ -0,0 +1,20 @@
with import <nix/config.nix>;
rec {
inherit shell;
path = coreutils;
system = "x86_64-linux";
shared = builtins.getEnv "_NIX_TEST_SHARED";
mkDerivation = args:
derivation ({
inherit system;
builder = shell;
args = ["-e" args.builder or (builtins.toFile "builder.sh" "if [ -e .attrs.sh ]; then source .attrs.sh; fi; eval \"$buildCommand\"")];
PATH = path;
} // removeAttrs args ["builder" "meta"])
// { meta = args.meta or {}; };
}

View file

@ -59,7 +59,7 @@ EOF
git -C $flake2Dir add flake.nix git -C $flake2Dir add flake.nix
git -C $flake2Dir commit -m 'Initial' git -C $flake2Dir commit -m 'Initial'
cat > $flake3/flake.nix <<EOF cat > $flake3Dir/flake.nix <<EOF
{ {
name = "flake3"; name = "flake3";
@ -75,8 +75,8 @@ cat > $flake3/flake.nix <<EOF
} }
EOF EOF
git -C $flake3 add flake.nix git -C $flake3Dir add flake.nix
git -C $flake3 commit -m 'Initial' git -C $flake3Dir commit -m 'Initial'
cat > $registry <<EOF cat > $registry <<EOF
{ {
@ -139,4 +139,4 @@ nix build -o $TEST_ROOT/result --flake-registry $registry flake2:bar
nix build -o $TEST_ROOT/result file://$flake2Dir:bar nix build -o $TEST_ROOT/result file://$flake2Dir:bar
# Test whether indirect dependencies work. # Test whether indirect dependencies work.
#nix build -o $TEST_ROOT/result --flake-registry $registry $flake3:xyzzy nix build -o $TEST_ROOT/result --flake-registry $registry $flake3Dir:xyzzy