1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 14:51:16 +02:00

lockFlake(): Allow registry lookups for the top-level flake

Fixes #13050.

(cherry picked from commit 68de26d38a)
This commit is contained in:
Eelco Dolstra 2025-04-24 18:54:16 +02:00
parent 3019007eb5
commit dfbb52e6bd
9 changed files with 55 additions and 26 deletions

View file

@ -40,7 +40,7 @@ void completeFlakeInputAttrPath(
std::string_view prefix) std::string_view prefix)
{ {
for (auto & flakeRef : flakeRefs) { for (auto & flakeRef : flakeRefs) {
auto flake = flake::getFlake(*evalState, flakeRef, true); auto flake = flake::getFlake(*evalState, flakeRef, fetchers::UseRegistries::All);
for (auto & input : flake.inputs) for (auto & input : flake.inputs)
if (hasPrefix(input.first, prefix)) if (hasPrefix(input.first, prefix))
completions.add(input.first); completions.add(input.first);

View file

@ -181,7 +181,7 @@ static void fetchTree(
} }
if (!state.settings.pureEval && !input.isDirect() && experimentalFeatureSettings.isEnabled(Xp::Flakes)) if (!state.settings.pureEval && !input.isDirect() && experimentalFeatureSettings.isEnabled(Xp::Flakes))
input = lookupInRegistries(state.store, input).first; input = lookupInRegistries(state.store, input, fetchers::UseRegistries::Limited).first;
if (state.settings.pureEval && !input.isLocked()) { if (state.settings.pureEval && !input.isLocked()) {
if (input.getNarHash()) if (input.getNarHash())

View file

@ -65,7 +65,11 @@ void overrideRegistry(
const Input & to, const Input & to,
const Attrs & extraAttrs); const Attrs & extraAttrs);
using RegistryFilter = std::function<bool(Registry::RegistryType)>; enum class UseRegistries : int {
No,
All,
Limited, // global and flag registry only
};
/** /**
* Rewrite a flakeref using the registries. If `filter` is set, only * Rewrite a flakeref using the registries. If `filter` is set, only
@ -74,6 +78,6 @@ using RegistryFilter = std::function<bool(Registry::RegistryType)>;
std::pair<Input, Attrs> lookupInRegistries( std::pair<Input, Attrs> lookupInRegistries(
ref<Store> store, ref<Store> store,
const Input & input, const Input & input,
const RegistryFilter & filter = {}); UseRegistries useRegistries);
} }

View file

@ -14,6 +14,8 @@ std::shared_ptr<Registry> Registry::read(
const Settings & settings, const Settings & settings,
const Path & path, RegistryType type) const Path & path, RegistryType type)
{ {
debug("reading registry '%s'", path);
auto registry = std::make_shared<Registry>(settings, type); auto registry = std::make_shared<Registry>(settings, type);
if (!pathExists(path)) if (!pathExists(path))
@ -179,29 +181,36 @@ Registries getRegistries(const Settings & settings, ref<Store> store)
std::pair<Input, Attrs> lookupInRegistries( std::pair<Input, Attrs> lookupInRegistries(
ref<Store> store, ref<Store> store,
const Input & _input, const Input & _input,
const RegistryFilter & filter) UseRegistries useRegistries)
{ {
Attrs extraAttrs; Attrs extraAttrs;
int n = 0; int n = 0;
Input input(_input); Input input(_input);
if (useRegistries == UseRegistries::No)
return {input, extraAttrs};
restart: restart:
n++; n++;
if (n > 100) throw Error("cycle detected in flake registry for '%s'", input.to_string()); if (n > 100) throw Error("cycle detected in flake registry for '%s'", input.to_string());
for (auto & registry : getRegistries(*input.settings, store)) { for (auto & registry : getRegistries(*input.settings, store)) {
if (filter && !filter(registry->type)) continue; if (useRegistries == UseRegistries::Limited
&& !(registry->type == fetchers::Registry::Flag || registry->type == fetchers::Registry::Global))
continue;
// FIXME: O(n) // FIXME: O(n)
for (auto & entry : registry->entries) { for (auto & entry : registry->entries) {
if (entry.exact) { if (entry.exact) {
if (entry.from == input) { if (entry.from == input) {
debug("resolved flakeref '%s' against registry %d exactly", input.to_string(), registry->type);
input = entry.to; input = entry.to;
extraAttrs = entry.extraAttrs; extraAttrs = entry.extraAttrs;
goto restart; goto restart;
} }
} else { } else {
if (entry.from.contains(input)) { if (entry.from.contains(input)) {
debug("resolved flakeref '%s' against registry %d", input.to_string(), registry->type);
input = entry.to.applyOverrides( input = entry.to.applyOverrides(
!entry.from.getRef() && input.getRef() ? input.getRef() : std::optional<std::string>(), !entry.from.getRef() && input.getRef() ? input.getRef() : std::optional<std::string>(),
!entry.from.getRev() && input.getRev() ? input.getRev() : std::optional<Hash>()); !entry.from.getRev() && input.getRev() ? input.getRev() : std::optional<Hash>());

View file

@ -45,7 +45,7 @@ static std::optional<FetchedFlake> lookupInFlakeCache(
static std::tuple<ref<SourceAccessor>, FlakeRef, FlakeRef> fetchOrSubstituteTree( static std::tuple<ref<SourceAccessor>, FlakeRef, FlakeRef> fetchOrSubstituteTree(
EvalState & state, EvalState & state,
const FlakeRef & originalRef, const FlakeRef & originalRef,
bool useRegistries, fetchers::UseRegistries useRegistries,
FlakeCache & flakeCache) FlakeCache & flakeCache)
{ {
auto fetched = lookupInFlakeCache(flakeCache, originalRef); auto fetched = lookupInFlakeCache(flakeCache, originalRef);
@ -56,14 +56,8 @@ static std::tuple<ref<SourceAccessor>, FlakeRef, FlakeRef> fetchOrSubstituteTree
auto [accessor, lockedRef] = originalRef.lazyFetch(state.store); auto [accessor, lockedRef] = originalRef.lazyFetch(state.store);
fetched.emplace(FetchedFlake{.lockedRef = lockedRef, .accessor = accessor}); fetched.emplace(FetchedFlake{.lockedRef = lockedRef, .accessor = accessor});
} else { } else {
if (useRegistries) { if (useRegistries != fetchers::UseRegistries::No) {
resolvedRef = originalRef.resolve( resolvedRef = originalRef.resolve(state.store, useRegistries);
state.store,
[](fetchers::Registry::RegistryType type) {
/* Only use the global registry and CLI flags
to resolve indirect flakerefs. */
return type == fetchers::Registry::Flag || type == fetchers::Registry::Global;
});
fetched = lookupInFlakeCache(flakeCache, originalRef); fetched = lookupInFlakeCache(flakeCache, originalRef);
if (!fetched) { if (!fetched) {
auto [accessor, lockedRef] = resolvedRef.lazyFetch(state.store); auto [accessor, lockedRef] = resolvedRef.lazyFetch(state.store);
@ -396,7 +390,7 @@ static FlakeRef applySelfAttrs(
static Flake getFlake( static Flake getFlake(
EvalState & state, EvalState & state,
const FlakeRef & originalRef, const FlakeRef & originalRef,
bool useRegistries, fetchers::UseRegistries useRegistries,
FlakeCache & flakeCache, FlakeCache & flakeCache,
const InputAttrPath & lockRootAttrPath) const InputAttrPath & lockRootAttrPath)
{ {
@ -415,7 +409,7 @@ static Flake getFlake(
// FIXME: need to remove attrs that are invalidated by the changed input attrs, such as 'narHash'. // FIXME: need to remove attrs that are invalidated by the changed input attrs, such as 'narHash'.
newLockedRef.input.attrs.erase("narHash"); newLockedRef.input.attrs.erase("narHash");
auto [accessor2, resolvedRef2, lockedRef2] = fetchOrSubstituteTree( auto [accessor2, resolvedRef2, lockedRef2] = fetchOrSubstituteTree(
state, newLockedRef, false, flakeCache); state, newLockedRef, fetchers::UseRegistries::No, flakeCache);
accessor = accessor2; accessor = accessor2;
lockedRef = lockedRef2; lockedRef = lockedRef2;
} }
@ -427,7 +421,7 @@ static Flake getFlake(
return readFlake(state, originalRef, resolvedRef, lockedRef, state.storePath(storePath), lockRootAttrPath); return readFlake(state, originalRef, resolvedRef, lockedRef, state.storePath(storePath), lockRootAttrPath);
} }
Flake getFlake(EvalState & state, const FlakeRef & originalRef, bool useRegistries) Flake getFlake(EvalState & state, const FlakeRef & originalRef, fetchers::UseRegistries useRegistries)
{ {
FlakeCache flakeCache; FlakeCache flakeCache;
return getFlake(state, originalRef, useRegistries, flakeCache, {}); return getFlake(state, originalRef, useRegistries, flakeCache, {});
@ -455,8 +449,15 @@ LockedFlake lockFlake(
FlakeCache flakeCache; FlakeCache flakeCache;
auto useRegistries = lockFlags.useRegistries.value_or(settings.useRegistries); auto useRegistries = lockFlags.useRegistries.value_or(settings.useRegistries);
auto useRegistriesTop = useRegistries ? fetchers::UseRegistries::All : fetchers::UseRegistries::No;
auto useRegistriesInputs = useRegistries ? fetchers::UseRegistries::Limited : fetchers::UseRegistries::No;
auto flake = getFlake(state, topRef, useRegistries, flakeCache, {}); auto flake = getFlake(
state,
topRef,
useRegistriesTop,
flakeCache,
{});
if (lockFlags.applyNixConfig) { if (lockFlags.applyNixConfig) {
flake.config.apply(settings); flake.config.apply(settings);
@ -631,7 +632,12 @@ LockedFlake lockFlake(
if (auto resolvedPath = resolveRelativePath()) { if (auto resolvedPath = resolveRelativePath()) {
return readFlake(state, ref, ref, ref, *resolvedPath, inputAttrPath); return readFlake(state, ref, ref, ref, *resolvedPath, inputAttrPath);
} else { } else {
return getFlake(state, ref, useRegistries, flakeCache, inputAttrPath); return getFlake(
state,
ref,
useRegistriesInputs,
flakeCache,
inputAttrPath);
} }
}; };
@ -780,7 +786,7 @@ LockedFlake lockFlake(
return {*resolvedPath, *input.ref}; return {*resolvedPath, *input.ref};
} else { } else {
auto [accessor, resolvedRef, lockedRef] = fetchOrSubstituteTree( auto [accessor, resolvedRef, lockedRef] = fetchOrSubstituteTree(
state, *input.ref, useRegistries, flakeCache); state, *input.ref, useRegistriesInputs, flakeCache);
// FIXME: allow input to be lazy. // FIXME: allow input to be lazy.
auto storePath = copyInputToStore(state, lockedRef.input, input.ref->input, accessor); auto storePath = copyInputToStore(state, lockedRef.input, input.ref->input, accessor);
@ -895,7 +901,10 @@ LockedFlake lockFlake(
repo, so we should re-read it. FIXME: we could repo, so we should re-read it. FIXME: we could
also just clear the 'rev' field... */ also just clear the 'rev' field... */
auto prevLockedRef = flake.lockedRef; auto prevLockedRef = flake.lockedRef;
flake = getFlake(state, topRef, useRegistries); flake = getFlake(
state,
topRef,
useRegistriesTop);
if (lockFlags.commitLockFile && if (lockFlags.commitLockFile &&
flake.lockedRef.input.getRev() && flake.lockedRef.input.getRev() &&

View file

@ -37,9 +37,9 @@ std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
FlakeRef FlakeRef::resolve( FlakeRef FlakeRef::resolve(
ref<Store> store, ref<Store> store,
const fetchers::RegistryFilter & filter) const fetchers::UseRegistries useRegistries) const
{ {
auto [input2, extraAttrs] = lookupInRegistries(store, input); auto [input2, extraAttrs] = lookupInRegistries(store, input, useRegistries);
return FlakeRef(std::move(input2), fetchers::maybeGetStrAttr(extraAttrs, "dir").value_or(subdir)); return FlakeRef(std::move(input2), fetchers::maybeGetStrAttr(extraAttrs, "dir").value_or(subdir));
} }

View file

@ -115,7 +115,7 @@ struct Flake
} }
}; };
Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool useRegistries); Flake getFlake(EvalState & state, const FlakeRef & flakeRef, fetchers::UseRegistries useRegistries);
/** /**
* Fingerprint of a locked flake; used as a cache key. * Fingerprint of a locked flake; used as a cache key.

View file

@ -65,7 +65,7 @@ struct FlakeRef
FlakeRef resolve( FlakeRef resolve(
ref<Store> store, ref<Store> store,
const fetchers::RegistryFilter & filter = {}) const; fetchers::UseRegistries useRegistries = fetchers::UseRegistries::All) const;
static FlakeRef fromAttrs( static FlakeRef fromAttrs(
const fetchers::Settings & fetchSettings, const fetchers::Settings & fetchSettings,

View file

@ -220,6 +220,13 @@ nix store gc
nix registry list --flake-registry "file://$registry" --refresh | grepQuiet flake3 nix registry list --flake-registry "file://$registry" --refresh | grepQuiet flake3
mv "$registry.tmp" "$registry" mv "$registry.tmp" "$registry"
# Ensure that locking ignores the user registry.
mkdir -p "$TEST_HOME/.config/nix"
ln -sfn "$registry" "$TEST_HOME/.config/nix/registry.json"
nix flake metadata --flake-registry '' flake1
expectStderr 1 nix flake update --flake-registry '' --flake "$flake3Dir" | grepQuiet "cannot find flake 'flake:flake1' in the flake registries"
rm "$TEST_HOME/.config/nix/registry.json"
# Test whether flakes are registered as GC roots for offline use. # Test whether flakes are registered as GC roots for offline use.
# FIXME: use tarballs rather than git. # FIXME: use tarballs rather than git.
rm -rf "$TEST_HOME/.cache" rm -rf "$TEST_HOME/.cache"