mirror of
https://github.com/NixOS/nix.git
synced 2025-12-15 13:31:05 +01:00
Fixed issue 65
lockfile updating
This commit is contained in:
parent
b531695331
commit
d9ad3723d5
6 changed files with 90 additions and 75 deletions
|
|
@ -289,8 +289,6 @@ Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowe
|
|||
|
||||
FlakeRef resolvedRef = sourceInfo.resolvedRef;
|
||||
|
||||
resolvedRef = sourceInfo.resolvedRef; // `resolvedRef` is now immutable
|
||||
|
||||
state.store->assertStorePath(sourceInfo.storePath);
|
||||
|
||||
if (state.allowedPaths)
|
||||
|
|
@ -368,35 +366,59 @@ NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeAlias al
|
|||
return nonFlake;
|
||||
}
|
||||
|
||||
/* Given a flake reference, recursively fetch it and its
|
||||
dependencies.
|
||||
FIXME: this should return a graph of flakes.
|
||||
*/
|
||||
ResolvedFlake resolveFlake(EvalState & state, const FlakeRef & topRef,
|
||||
RegistryAccess registryAccess, bool isTopFlake)
|
||||
LockFile entryToLockFile(const LockFile::FlakeEntry & entry)
|
||||
{
|
||||
LockFile lockFile;
|
||||
lockFile.flakeEntries = entry.flakeEntries;
|
||||
lockFile.nonFlakeEntries = entry.nonFlakeEntries;
|
||||
return lockFile;
|
||||
}
|
||||
|
||||
ResolvedFlake resolveFlakeFromLockFile(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess,
|
||||
LockFile lockFile, bool isTopFlake = false)
|
||||
{
|
||||
bool allowRegistries = registryAccess == AllowRegistry || (registryAccess == AllowRegistryAtTop && isTopFlake);
|
||||
Flake flake = getFlake(state, topRef, allowRegistries);
|
||||
LockFile lockFile;
|
||||
|
||||
if (isTopFlake)
|
||||
lockFile = readLockFile(flake.storePath + flake.resolvedRef.subdir + "/flake.lock"); // FIXME: symlink attack
|
||||
Flake flake = getFlake(state, flakeRef, allowRegistries);
|
||||
|
||||
ResolvedFlake deps(flake);
|
||||
|
||||
for (auto & nonFlakeInfo : flake.nonFlakeRequires)
|
||||
deps.nonFlakeDeps.push_back(getNonFlake(state, nonFlakeInfo.second, nonFlakeInfo.first));
|
||||
for (auto & nonFlakeInfo : flake.nonFlakeRequires) {
|
||||
FlakeRef ref = nonFlakeInfo.second;
|
||||
auto i = lockFile.nonFlakeEntries.find(nonFlakeInfo.first);
|
||||
if (i != lockFile.nonFlakeEntries.end()) ref = i->second.ref;
|
||||
deps.nonFlakeDeps.push_back(getNonFlake(state, ref, nonFlakeInfo.first));
|
||||
}
|
||||
|
||||
for (auto newFlakeRef : flake.requires) {
|
||||
FlakeRef ref = newFlakeRef;
|
||||
LockFile newLockFile;
|
||||
auto i = lockFile.flakeEntries.find(newFlakeRef);
|
||||
if (i != lockFile.flakeEntries.end()) newFlakeRef = i->second.ref;
|
||||
// FIXME: propagate lockFile downwards
|
||||
deps.flakeDeps.push_back(resolveFlake(state, newFlakeRef, registryAccess, false));
|
||||
if (i != lockFile.flakeEntries.end()) { // Propagate lockFile downwards if possible
|
||||
ref = i->second.ref;
|
||||
newLockFile = entryToLockFile(i->second);
|
||||
}
|
||||
deps.flakeDeps.push_back(resolveFlakeFromLockFile(state, ref, registryAccess, newLockFile));
|
||||
}
|
||||
|
||||
return deps;
|
||||
}
|
||||
|
||||
/* Given a flake reference, recursively fetch it and its dependencies.
|
||||
FIXME: this should return a graph of flakes.
|
||||
*/
|
||||
ResolvedFlake resolveFlake(EvalState & state, const FlakeRef & topRef, RegistryAccess registryAccess,
|
||||
bool recreateLockFile)
|
||||
{
|
||||
bool allowRegistries = registryAccess == AllowRegistry || registryAccess == AllowRegistryAtTop;
|
||||
Flake flake = getFlake(state, topRef, allowRegistries);
|
||||
LockFile lockFile;
|
||||
|
||||
if (!recreateLockFile) // If recreateLockFile, start with an empty lockfile
|
||||
lockFile = readLockFile(flake.storePath + "/flake.lock"); // FIXME: symlink attack
|
||||
|
||||
return resolveFlakeFromLockFile(state, topRef, registryAccess, lockFile, true);
|
||||
}
|
||||
|
||||
LockFile::FlakeEntry dependenciesToFlakeEntry(const ResolvedFlake & resolvedFlake)
|
||||
{
|
||||
LockFile::FlakeEntry entry(resolvedFlake.flake.resolvedRef, resolvedFlake.flake.hash);
|
||||
|
|
@ -410,31 +432,25 @@ LockFile::FlakeEntry dependenciesToFlakeEntry(const ResolvedFlake & resolvedFlak
|
|||
return entry;
|
||||
}
|
||||
|
||||
static LockFile makeLockFile(EvalState & evalState, FlakeRef & flakeRef)
|
||||
static LockFile makeLockFile(EvalState & evalState, FlakeRef & flakeRef, bool recreateLockFile)
|
||||
{
|
||||
ResolvedFlake resFlake = resolveFlake(evalState, flakeRef, AllowRegistry);
|
||||
LockFile::FlakeEntry entry = dependenciesToFlakeEntry(resFlake);
|
||||
LockFile lockFile;
|
||||
lockFile.flakeEntries = entry.flakeEntries;
|
||||
lockFile.nonFlakeEntries = entry.nonFlakeEntries;
|
||||
return lockFile;
|
||||
ResolvedFlake resFlake = resolveFlake(evalState, flakeRef, AllowRegistry, recreateLockFile);
|
||||
return entryToLockFile(dependenciesToFlakeEntry(resFlake));
|
||||
}
|
||||
|
||||
void updateLockFile(EvalState & state, const FlakeUri & flakeUri)
|
||||
void updateLockFile(EvalState & state, const FlakeUri & uri, bool recreateLockFile)
|
||||
{
|
||||
// FIXME: We are writing the lockfile to the store here! Very bad practice!
|
||||
FlakeRef flakeRef = FlakeRef(flakeUri);
|
||||
FlakeRef flakeRef = FlakeRef(uri);
|
||||
auto lockFile = makeLockFile(state, flakeRef, recreateLockFile);
|
||||
if (auto refData = std::get_if<FlakeRef::IsPath>(&flakeRef.data)) {
|
||||
auto lockFile = makeLockFile(state, flakeRef);
|
||||
writeLockFile(lockFile, refData->path + "/" + flakeRef.subdir + "/flake.lock");
|
||||
writeLockFile(lockFile, refData->path + (flakeRef.subdir == "" ? "" : "/" + flakeRef.subdir) + "/flake.lock");
|
||||
|
||||
// Hack: Make sure that flake.lock is visible to Git. Otherwise,
|
||||
// exportGit will fail to copy it to the Nix store.
|
||||
runProgram("git", true,
|
||||
{ "-C", refData->path, "add", "--intent-to-add",
|
||||
(flakeRef.subdir == "" ? "" : flakeRef.subdir + "/") + "flake.lock" });
|
||||
runProgram("git", true, { "-C", refData->path, "add",
|
||||
(flakeRef.subdir == "" ? "" : flakeRef.subdir + "/") + "flake.lock" });
|
||||
} else
|
||||
throw Error("flakeUri %s can't be updated because it is not a path", flakeUri);
|
||||
throw Error("flakeUri %s can't be updated because it is not a path", uri);
|
||||
}
|
||||
|
||||
void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
|
||||
|
|
@ -485,16 +501,17 @@ void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
|
|||
|
||||
// Return the `provides` of the top flake, while assigning to `v` the provides
|
||||
// of the dependencies as well.
|
||||
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v)
|
||||
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v, bool recreateLockFile)
|
||||
{
|
||||
callFlake(state, resolveFlake(state, flakeRef, registryAccess), v);
|
||||
callFlake(state, resolveFlake(state, flakeRef, registryAccess, recreateLockFile), v);
|
||||
}
|
||||
|
||||
// This function is exposed to be used in nix files.
|
||||
static void prim_getFlake(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||
{
|
||||
makeFlakeValue(state, state.forceStringNoCtx(*args[0], pos),
|
||||
evalSettings.pureEval ? DisallowRegistry : AllowRegistryAtTop, v);
|
||||
evalSettings.pureEval ? DisallowRegistry : AllowRegistryAtTop, v, false);
|
||||
// `recreateLockFile == false` because this is the evaluation stage, which should be pure, and hence not recreate lockfiles.
|
||||
}
|
||||
|
||||
static RegisterPrimOp r2("getFlake", 1, prim_getFlake);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ Path getUserRegistryPath();
|
|||
|
||||
enum RegistryAccess { DisallowRegistry, AllowRegistry, AllowRegistryAtTop };
|
||||
|
||||
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v);
|
||||
void makeFlakeValue(EvalState & state, const FlakeRef & flakeRef, RegistryAccess registryAccess, Value & v, bool recreateLockFile);
|
||||
|
||||
std::shared_ptr<FlakeRegistry> readRegistry(const Path &);
|
||||
|
||||
|
|
@ -103,9 +103,9 @@ struct ResolvedFlake
|
|||
ResolvedFlake(const Flake & flake) : flake(flake) {}
|
||||
};
|
||||
|
||||
ResolvedFlake resolveFlake(EvalState &, const FlakeRef &, RegistryAccess registryAccess, bool isTopFlake = true);
|
||||
ResolvedFlake resolveFlake(EvalState &, const FlakeRef &, RegistryAccess, bool recreateLockFile);
|
||||
|
||||
void updateLockFile(EvalState &, const FlakeUri &);
|
||||
void updateLockFile(EvalState &, const FlakeUri &, bool recreateLockFile);
|
||||
|
||||
void gitCloneFlake (std::string flakeUri, EvalState &, Registries, Path);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,10 +76,10 @@ struct SourceExprCommand : virtual Args, StoreCommand, MixEvalArgs
|
|||
{
|
||||
std::optional<Path> file;
|
||||
|
||||
bool updateLockFile = true;
|
||||
|
||||
SourceExprCommand();
|
||||
|
||||
bool recreateLockFile = false;
|
||||
|
||||
ref<EvalState> getEvalState();
|
||||
|
||||
std::vector<std::shared_ptr<Installable>> parseInstallables(
|
||||
|
|
|
|||
|
|
@ -114,7 +114,8 @@ struct CmdFlakeDeps : FlakeCommand, MixJSON, StoreCommand, MixEvalArgs
|
|||
|
||||
FlakeRef flakeRef(flakeUri);
|
||||
|
||||
ResolvedFlake resFlake = resolveFlake(*evalState, flakeRef, AllowRegistryAtTop);
|
||||
bool recreateLockFile = false;
|
||||
ResolvedFlake resFlake = resolveFlake(*evalState, flakeRef, AllowRegistryAtTop, recreateLockFile);
|
||||
|
||||
std::queue<ResolvedFlake> todo;
|
||||
todo.push(resFlake);
|
||||
|
|
@ -132,7 +133,7 @@ struct CmdFlakeDeps : FlakeCommand, MixJSON, StoreCommand, MixEvalArgs
|
|||
}
|
||||
};
|
||||
|
||||
struct CmdFlakeUpdate : StoreCommand, GitRepoCommand, MixEvalArgs
|
||||
struct CmdFlakeUpdate : StoreCommand, FlakeCommand, MixEvalArgs
|
||||
{
|
||||
std::string name() override
|
||||
{
|
||||
|
|
@ -148,8 +149,8 @@ struct CmdFlakeUpdate : StoreCommand, GitRepoCommand, MixEvalArgs
|
|||
{
|
||||
auto evalState = std::make_shared<EvalState>(searchPath, store);
|
||||
|
||||
if (gitPath == "") gitPath = absPath(".");
|
||||
updateLockFile(*evalState, gitPath);
|
||||
bool recreateLockFile = true;
|
||||
updateLockFile(*evalState, flakeUri, recreateLockFile);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@ SourceExprCommand::SourceExprCommand()
|
|||
.dest(&file);
|
||||
|
||||
mkFlag()
|
||||
.longName("no-update")
|
||||
.description("don't create/update flake lock files")
|
||||
.set(&updateLockFile, false);
|
||||
.longName("recreate-lock-file")
|
||||
.description("recreate lock file from scratch")
|
||||
.set(&recreateLockFile, true);
|
||||
}
|
||||
|
||||
ref<EvalState> SourceExprCommand::getEvalState()
|
||||
|
|
@ -157,13 +157,11 @@ struct InstallableFlake : InstallableValue
|
|||
|
||||
Value * toValue(EvalState & state) override
|
||||
{
|
||||
auto path = std::get_if<FlakeRef::IsPath>(&flakeRef.data);
|
||||
if (cmd.updateLockFile && path) {
|
||||
updateLockFile(state, path->path);
|
||||
}
|
||||
|
||||
auto vFlake = state.allocValue();
|
||||
makeFlakeValue(state, flakeRef, AllowRegistryAtTop, *vFlake);
|
||||
if (std::get_if<FlakeRef::IsPath>(&flakeRef.data))
|
||||
updateLockFile(state, flakeRef.to_string(), cmd.recreateLockFile);
|
||||
|
||||
makeFlakeValue(state, flakeRef, AllowRegistryAtTop, *vFlake, cmd.recreateLockFile);
|
||||
|
||||
auto vProvides = (*vFlake->attrs->get(state.symbols.create("provides")))->value;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue