From 2b8ca654b058cf18101a5243090706be97930275 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 24 Jan 2020 13:07:03 +0100 Subject: HandleLockFile -> LockFileMode --- src/libexpr/flake/flake.cc | 46 ++++++++++++++++++---------------------------- src/libexpr/flake/flake.hh | 4 ++-- src/nix/command.hh | 4 ++-- src/nix/installables.cc | 2 +- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index a0036c7bd..1475c6c99 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -213,17 +213,7 @@ static std::pair getNonFlake( return std::make_pair(std::move(sourceInfo), resolvedRef); } -bool allowedToWrite(HandleLockFile handle) -{ - return handle == UpdateLockFile || handle == RecreateLockFile; -} - -bool recreateLockFile(HandleLockFile handle) -{ - return handle == RecreateLockFile || handle == UseNewLockFile; -} - -bool allowedToUseRegistries(HandleLockFile handle, bool isTopRef) +bool allowedToUseRegistries(LockFileMode handle, bool isTopRef) { if (handle == AllPure) return false; else if (handle == TopRefUsesRegistries) return isTopRef; @@ -248,7 +238,7 @@ static std::pair updateLocks( const std::string & inputPath, EvalState & state, const Flake & flake, - HandleLockFile handleLockFile, + LockFileMode lockFileMode, const LockedInputs & oldEntry, bool topRef) { @@ -265,7 +255,7 @@ static std::pair updateLocks( if (i != oldEntry.inputs.end() && i->second.originalRef == input.ref) { newEntry.inputs.insert_or_assign(id, i->second); } else { - if (handleLockFile == AllPure || handleLockFile == TopRefUsesRegistries) + if (lockFileMode == AllPure || lockFileMode == TopRefUsesRegistries) throw Error("cannot update flake input '%s' in pure mode", id); auto warn = [&](const FlakeRef & resolvedRef, const fetchers::Tree & sourceInfo) { @@ -279,15 +269,15 @@ static std::pair updateLocks( if (input.isFlake) { auto actualInput = getFlake(state, input.ref, - allowedToUseRegistries(handleLockFile, false), refMap); + allowedToUseRegistries(lockFileMode, false), refMap); warn(actualInput.resolvedRef, *actualInput.sourceInfo); postponed.push_back([&, id{id}, inputPath2, actualInput]() { newEntry.inputs.insert_or_assign(id, - updateLocks(refMap, inputPath2, state, actualInput, handleLockFile, {}, false).second); + updateLocks(refMap, inputPath2, state, actualInput, lockFileMode, {}, false).second); }); } else { auto [sourceInfo, resolvedRef] = getNonFlake(state, input.ref, - allowedToUseRegistries(handleLockFile, false), refMap); + allowedToUseRegistries(lockFileMode, false), refMap); warn(resolvedRef, sourceInfo); newEntry.inputs.insert_or_assign(id, LockedInput(resolvedRef, input.ref, sourceInfo.narHash)); @@ -305,16 +295,16 @@ static std::pair updateLocks( LockedFlake lockFlake( EvalState & state, const FlakeRef & topRef, - HandleLockFile handleLockFile) + LockFileMode lockFileMode) { settings.requireExperimentalFeature("flakes"); auto flake = getFlake(state, topRef, - allowedToUseRegistries(handleLockFile, true)); + allowedToUseRegistries(lockFileMode, true)); LockFile oldLockFile; - if (!recreateLockFile(handleLockFile)) { + if (lockFileMode != RecreateLockFile && lockFileMode != UseNewLockFile) { // If recreateLockFile, start with an empty lockfile // FIXME: symlink attack oldLockFile = LockFile::read( @@ -325,21 +315,21 @@ LockedFlake lockFlake( RefMap refMap; - LockFile lockFile(updateLocks( - refMap, "", state, flake, handleLockFile, oldLockFile, true).second); + LockFile newLockFile(updateLocks( + refMap, "", state, flake, lockFileMode, oldLockFile, true).second); - debug("new lock file: %s", lockFile); + debug("new lock file: %s", newLockFile); - if (!(lockFile == oldLockFile)) { - if (allowedToWrite(handleLockFile)) { + if (!(newLockFile == oldLockFile)) { + if (lockFileMode == UpdateLockFile || lockFileMode == RecreateLockFile) { if (auto sourcePath = topRef.input->getSourcePath()) { - if (!lockFile.isImmutable()) { + if (!newLockFile.isImmutable()) { if (settings.warnDirty) warn("will not write lock file of flake '%s' because it has a mutable input", topRef); } else { 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 // top-level repo, so we should re-read it. @@ -355,11 +345,11 @@ LockedFlake lockFlake( } } else 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"); } - 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) diff --git a/src/libexpr/flake/flake.hh b/src/libexpr/flake/flake.hh index a9864c5e8..eff33363d 100644 --- a/src/libexpr/flake/flake.hh +++ b/src/libexpr/flake/flake.hh @@ -13,7 +13,7 @@ namespace fetchers { struct Tree; } namespace flake { -enum HandleLockFile : unsigned int +enum LockFileMode : unsigned int { AllPure // Everything is handled 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 @@ -54,7 +54,7 @@ struct LockedFlake Fingerprint getFingerprint() const; }; -LockedFlake lockFlake(EvalState &, const FlakeRef &, HandleLockFile); +LockedFlake lockFlake(EvalState &, const FlakeRef &, LockFileMode); void callFlake(EvalState & state, const Flake & flake, diff --git a/src/nix/command.hh b/src/nix/command.hh index fc69d7154..08fa0c5fa 100644 --- a/src/nix/command.hh +++ b/src/nix/command.hh @@ -16,7 +16,7 @@ struct Pos; class Store; namespace flake { -enum HandleLockFile : unsigned int; +enum LockFileMode : unsigned int; } /* A command that requires a Nix store. */ @@ -49,7 +49,7 @@ struct MixFlakeOptions : virtual Args MixFlakeOptions(); - flake::HandleLockFile getLockFileMode(); + flake::LockFileMode getLockFileMode(); }; struct SourceExprCommand : virtual Args, EvalCommand, MixFlakeOptions diff --git a/src/nix/installables.cc b/src/nix/installables.cc index a42f7a76f..85b090bfc 100644 --- a/src/nix/installables.cc +++ b/src/nix/installables.cc @@ -35,7 +35,7 @@ MixFlakeOptions::MixFlakeOptions() .set(&useRegistries, false); } -flake::HandleLockFile MixFlakeOptions::getLockFileMode() +flake::LockFileMode MixFlakeOptions::getLockFileMode() { using namespace flake; return -- cgit v1.2.3