HandleLockFile -> LockFileMode

This commit is contained in:
Eelco Dolstra 2020-01-24 13:07:03 +01:00
parent cd973fa07f
commit 2b8ca654b0
4 changed files with 23 additions and 33 deletions

View file

@ -213,17 +213,7 @@ static std::pair<fetchers::Tree, FlakeRef> getNonFlake(
return std::make_pair(std::move(sourceInfo), resolvedRef); return std::make_pair(std::move(sourceInfo), resolvedRef);
} }
bool allowedToWrite(HandleLockFile handle) bool allowedToUseRegistries(LockFileMode handle, bool isTopRef)
{
return handle == UpdateLockFile || handle == RecreateLockFile;
}
bool recreateLockFile(HandleLockFile handle)
{
return handle == RecreateLockFile || handle == UseNewLockFile;
}
bool allowedToUseRegistries(HandleLockFile handle, bool isTopRef)
{ {
if (handle == AllPure) return false; if (handle == AllPure) return false;
else if (handle == TopRefUsesRegistries) return isTopRef; else if (handle == TopRefUsesRegistries) return isTopRef;
@ -248,7 +238,7 @@ static std::pair<Flake, LockedInput> updateLocks(
const std::string & inputPath, const std::string & inputPath,
EvalState & state, EvalState & state,
const Flake & flake, const Flake & flake,
HandleLockFile handleLockFile, LockFileMode lockFileMode,
const LockedInputs & oldEntry, const LockedInputs & oldEntry,
bool topRef) bool topRef)
{ {
@ -265,7 +255,7 @@ static std::pair<Flake, LockedInput> updateLocks(
if (i != oldEntry.inputs.end() && i->second.originalRef == input.ref) { if (i != oldEntry.inputs.end() && i->second.originalRef == input.ref) {
newEntry.inputs.insert_or_assign(id, i->second); newEntry.inputs.insert_or_assign(id, i->second);
} else { } else {
if (handleLockFile == AllPure || handleLockFile == TopRefUsesRegistries) if (lockFileMode == AllPure || lockFileMode == TopRefUsesRegistries)
throw Error("cannot update flake input '%s' in pure mode", id); throw Error("cannot update flake input '%s' in pure mode", id);
auto warn = [&](const FlakeRef & resolvedRef, const fetchers::Tree & sourceInfo) { auto warn = [&](const FlakeRef & resolvedRef, const fetchers::Tree & sourceInfo) {
@ -279,15 +269,15 @@ static std::pair<Flake, LockedInput> updateLocks(
if (input.isFlake) { if (input.isFlake) {
auto actualInput = getFlake(state, input.ref, auto actualInput = getFlake(state, input.ref,
allowedToUseRegistries(handleLockFile, false), refMap); allowedToUseRegistries(lockFileMode, false), refMap);
warn(actualInput.resolvedRef, *actualInput.sourceInfo); warn(actualInput.resolvedRef, *actualInput.sourceInfo);
postponed.push_back([&, id{id}, inputPath2, actualInput]() { postponed.push_back([&, id{id}, inputPath2, actualInput]() {
newEntry.inputs.insert_or_assign(id, newEntry.inputs.insert_or_assign(id,
updateLocks(refMap, inputPath2, state, actualInput, handleLockFile, {}, false).second); updateLocks(refMap, inputPath2, state, actualInput, lockFileMode, {}, false).second);
}); });
} else { } else {
auto [sourceInfo, resolvedRef] = getNonFlake(state, input.ref, auto [sourceInfo, resolvedRef] = getNonFlake(state, input.ref,
allowedToUseRegistries(handleLockFile, false), refMap); allowedToUseRegistries(lockFileMode, false), refMap);
warn(resolvedRef, sourceInfo); warn(resolvedRef, sourceInfo);
newEntry.inputs.insert_or_assign(id, newEntry.inputs.insert_or_assign(id,
LockedInput(resolvedRef, input.ref, sourceInfo.narHash)); LockedInput(resolvedRef, input.ref, sourceInfo.narHash));
@ -305,16 +295,16 @@ static std::pair<Flake, LockedInput> updateLocks(
LockedFlake lockFlake( LockedFlake lockFlake(
EvalState & state, EvalState & state,
const FlakeRef & topRef, const FlakeRef & topRef,
HandleLockFile handleLockFile) LockFileMode lockFileMode)
{ {
settings.requireExperimentalFeature("flakes"); settings.requireExperimentalFeature("flakes");
auto flake = getFlake(state, topRef, auto flake = getFlake(state, topRef,
allowedToUseRegistries(handleLockFile, true)); allowedToUseRegistries(lockFileMode, true));
LockFile oldLockFile; LockFile oldLockFile;
if (!recreateLockFile(handleLockFile)) { if (lockFileMode != RecreateLockFile && lockFileMode != UseNewLockFile) {
// If recreateLockFile, start with an empty lockfile // If recreateLockFile, start with an empty lockfile
// FIXME: symlink attack // FIXME: symlink attack
oldLockFile = LockFile::read( oldLockFile = LockFile::read(
@ -325,21 +315,21 @@ LockedFlake lockFlake(
RefMap refMap; RefMap refMap;
LockFile lockFile(updateLocks( LockFile newLockFile(updateLocks(
refMap, "", state, flake, handleLockFile, oldLockFile, true).second); refMap, "", state, flake, lockFileMode, oldLockFile, true).second);
debug("new lock file: %s", lockFile); debug("new lock file: %s", newLockFile);
if (!(lockFile == oldLockFile)) { if (!(newLockFile == oldLockFile)) {
if (allowedToWrite(handleLockFile)) { if (lockFileMode == UpdateLockFile || lockFileMode == RecreateLockFile) {
if (auto sourcePath = topRef.input->getSourcePath()) { if (auto sourcePath = topRef.input->getSourcePath()) {
if (!lockFile.isImmutable()) { if (!newLockFile.isImmutable()) {
if (settings.warnDirty) if (settings.warnDirty)
warn("will not write lock file of flake '%s' because it has a mutable input", topRef); warn("will not write lock file of flake '%s' because it has a mutable input", topRef);
} else { } else {
warn("updated lock file of flake '%s'", topRef); warn("updated lock file of flake '%s'", topRef);
lockFile.write(*sourcePath + (topRef.subdir == "" ? "" : "/" + topRef.subdir) + "/flake.lock"); newLockFile.write(*sourcePath + (topRef.subdir == "" ? "" : "/" + topRef.subdir) + "/flake.lock");
// FIXME: rewriting the lockfile changed the // FIXME: rewriting the lockfile changed the
// top-level repo, so we should re-read it. // top-level repo, so we should re-read it.
@ -355,11 +345,11 @@ LockedFlake lockFlake(
} }
} else } else
warn("cannot write lock file of remote flake '%s'", topRef); warn("cannot write lock file of remote flake '%s'", topRef);
} else if (handleLockFile != AllPure && handleLockFile != TopRefUsesRegistries) } else if (lockFileMode != AllPure && lockFileMode != TopRefUsesRegistries)
warn("using updated lock file without writing it to file"); warn("using updated lock file without writing it to file");
} }
return LockedFlake { .flake = std::move(flake), .lockFile = std::move(lockFile) }; return LockedFlake { .flake = std::move(flake), .lockFile = std::move(newLockFile) };
} }
static void emitSourceInfoAttrs(EvalState & state, const fetchers::Tree & sourceInfo, Value & vAttrs) static void emitSourceInfoAttrs(EvalState & state, const fetchers::Tree & sourceInfo, Value & vAttrs)

View file

@ -13,7 +13,7 @@ namespace fetchers { struct Tree; }
namespace flake { namespace flake {
enum HandleLockFile : unsigned int enum LockFileMode : unsigned int
{ AllPure // Everything is handled 100% purely { AllPure // Everything is handled 100% purely
, TopRefUsesRegistries // The top FlakeRef uses the registries, apart from that, everything happens 100% purely , TopRefUsesRegistries // The top FlakeRef uses the registries, apart from that, everything happens 100% purely
, UpdateLockFile // Update the existing lockfile and write it to file , UpdateLockFile // Update the existing lockfile and write it to file
@ -54,7 +54,7 @@ struct LockedFlake
Fingerprint getFingerprint() const; Fingerprint getFingerprint() const;
}; };
LockedFlake lockFlake(EvalState &, const FlakeRef &, HandleLockFile); LockedFlake lockFlake(EvalState &, const FlakeRef &, LockFileMode);
void callFlake(EvalState & state, void callFlake(EvalState & state,
const Flake & flake, const Flake & flake,

View file

@ -16,7 +16,7 @@ struct Pos;
class Store; class Store;
namespace flake { namespace flake {
enum HandleLockFile : unsigned int; enum LockFileMode : unsigned int;
} }
/* A command that requires a Nix store. */ /* A command that requires a Nix store. */
@ -49,7 +49,7 @@ struct MixFlakeOptions : virtual Args
MixFlakeOptions(); MixFlakeOptions();
flake::HandleLockFile getLockFileMode(); flake::LockFileMode getLockFileMode();
}; };
struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions

View file

@ -35,7 +35,7 @@ MixFlakeOptions::MixFlakeOptions()
.set(&useRegistries, false); .set(&useRegistries, false);
} }
flake::HandleLockFile MixFlakeOptions::getLockFileMode() flake::LockFileMode MixFlakeOptions::getLockFileMode()
{ {
using namespace flake; using namespace flake;
return return