Skip to content

Commit 96a1740

Browse files
authored
Merge pull request #13088 from NixOS/fix-ignore-local-registries
Fix ignore local registries
2 parents 94edfb1 + 6405d68 commit 96a1740

File tree

11 files changed

+60
-25
lines changed

11 files changed

+60
-25
lines changed

src/libcmd/installables.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void completeFlakeInputAttrPath(
4040
std::string_view prefix)
4141
{
4242
for (auto & flakeRef : flakeRefs) {
43-
auto flake = flake::getFlake(*evalState, flakeRef, true);
43+
auto flake = flake::getFlake(*evalState, flakeRef, fetchers::UseRegistries::All);
4444
for (auto & input : flake.inputs)
4545
if (hasPrefix(input.first, prefix))
4646
completions.add(input.first);

src/libexpr/primops/fetchTree.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static void fetchTree(
181181
}
182182

183183
if (!state.settings.pureEval && !input.isDirect() && experimentalFeatureSettings.isEnabled(Xp::Flakes))
184-
input = lookupInRegistries(state.store, input).first;
184+
input = lookupInRegistries(state.store, input, fetchers::UseRegistries::Limited).first;
185185

186186
if (state.settings.pureEval && !input.isLocked()) {
187187
if (input.getNarHash())

src/libfetchers/include/nix/fetchers/input-cache.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace nix::fetchers {
44

5+
enum class UseRegistries : int;
6+
57
struct InputCache
68
{
79
struct CachedResult
@@ -11,7 +13,7 @@ struct InputCache
1113
Input lockedInput;
1214
};
1315

14-
CachedResult getAccessor(ref<Store> store, const Input & originalInput, bool useRegistries);
16+
CachedResult getAccessor(ref<Store> store, const Input & originalInput, UseRegistries useRegistries);
1517

1618
struct CachedInput
1719
{

src/libfetchers/include/nix/fetchers/registry.hh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ void overrideRegistry(
6565
const Input & to,
6666
const Attrs & extraAttrs);
6767

68-
using RegistryFilter = std::function<bool(Registry::RegistryType)>;
68+
enum class UseRegistries : int {
69+
No,
70+
All,
71+
Limited, // global and flag registry only
72+
};
6973

7074
/**
7175
* Rewrite a flakeref using the registries. If `filter` is set, only
@@ -74,6 +78,6 @@ using RegistryFilter = std::function<bool(Registry::RegistryType)>;
7478
std::pair<Input, Attrs> lookupInRegistries(
7579
ref<Store> store,
7680
const Input & input,
77-
const RegistryFilter & filter = {});
81+
UseRegistries useRegistries);
7882

7983
}

src/libfetchers/input-cache.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace nix::fetchers {
77

8-
InputCache::CachedResult InputCache::getAccessor(ref<Store> store, const Input & originalInput, bool useRegistries)
8+
InputCache::CachedResult
9+
InputCache::getAccessor(ref<Store> store, const Input & originalInput, UseRegistries useRegistries)
910
{
1011
auto fetched = lookup(originalInput);
1112
Input resolvedInput = originalInput;
@@ -15,13 +16,8 @@ InputCache::CachedResult InputCache::getAccessor(ref<Store> store, const Input &
1516
auto [accessor, lockedInput] = originalInput.getAccessor(store);
1617
fetched.emplace(CachedInput{.lockedInput = lockedInput, .accessor = accessor});
1718
} else {
18-
if (useRegistries) {
19-
auto [res, extraAttrs] =
20-
lookupInRegistries(store, originalInput, [](fetchers::Registry::RegistryType type) {
21-
/* Only use the global registry and CLI flags
22-
to resolve indirect flakerefs. */
23-
return type == fetchers::Registry::Flag || type == fetchers::Registry::Global;
24-
});
19+
if (useRegistries != UseRegistries::No) {
20+
auto [res, extraAttrs] = lookupInRegistries(store, originalInput, useRegistries);
2521
resolvedInput = std::move(res);
2622
fetched = lookup(resolvedInput);
2723
if (!fetched) {

src/libfetchers/registry.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ std::shared_ptr<Registry> Registry::read(
1414
const Settings & settings,
1515
const Path & path, RegistryType type)
1616
{
17+
debug("reading registry '%s'", path);
18+
1719
auto registry = std::make_shared<Registry>(settings, type);
1820

1921
if (!pathExists(path))
@@ -179,29 +181,36 @@ Registries getRegistries(const Settings & settings, ref<Store> store)
179181
std::pair<Input, Attrs> lookupInRegistries(
180182
ref<Store> store,
181183
const Input & _input,
182-
const RegistryFilter & filter)
184+
UseRegistries useRegistries)
183185
{
184186
Attrs extraAttrs;
185187
int n = 0;
186188
Input input(_input);
187189

190+
if (useRegistries == UseRegistries::No)
191+
return {input, extraAttrs};
192+
188193
restart:
189194

190195
n++;
191196
if (n > 100) throw Error("cycle detected in flake registry for '%s'", input.to_string());
192197

193198
for (auto & registry : getRegistries(*input.settings, store)) {
194-
if (filter && !filter(registry->type)) continue;
199+
if (useRegistries == UseRegistries::Limited
200+
&& !(registry->type == fetchers::Registry::Flag || registry->type == fetchers::Registry::Global))
201+
continue;
195202
// FIXME: O(n)
196203
for (auto & entry : registry->entries) {
197204
if (entry.exact) {
198205
if (entry.from == input) {
206+
debug("resolved flakeref '%s' against registry %d exactly", input.to_string(), registry->type);
199207
input = entry.to;
200208
extraAttrs = entry.extraAttrs;
201209
goto restart;
202210
}
203211
} else {
204212
if (entry.from.contains(input)) {
213+
debug("resolved flakeref '%s' against registry %d", input.to_string(), registry->type);
205214
input = entry.to.applyOverrides(
206215
!entry.from.getRef() && input.getRef() ? input.getRef() : std::optional<std::string>(),
207216
!entry.from.getRev() && input.getRev() ? input.getRev() : std::optional<Hash>());

src/libflake/flake.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static FlakeRef applySelfAttrs(
337337
static Flake getFlake(
338338
EvalState & state,
339339
const FlakeRef & originalRef,
340-
bool useRegistries,
340+
fetchers::UseRegistries useRegistries,
341341
const InputAttrPath & lockRootAttrPath)
342342
{
343343
// Fetch a lazy tree first.
@@ -368,7 +368,7 @@ static Flake getFlake(
368368
return readFlake(state, originalRef, resolvedRef, lockedRef, state.storePath(storePath), lockRootAttrPath);
369369
}
370370

371-
Flake getFlake(EvalState & state, const FlakeRef & originalRef, bool useRegistries)
371+
Flake getFlake(EvalState & state, const FlakeRef & originalRef, fetchers::UseRegistries useRegistries)
372372
{
373373
return getFlake(state, originalRef, useRegistries, {});
374374
}
@@ -393,8 +393,14 @@ LockedFlake lockFlake(
393393
experimentalFeatureSettings.require(Xp::Flakes);
394394

395395
auto useRegistries = lockFlags.useRegistries.value_or(settings.useRegistries);
396+
auto useRegistriesTop = useRegistries ? fetchers::UseRegistries::All : fetchers::UseRegistries::No;
397+
auto useRegistriesInputs = useRegistries ? fetchers::UseRegistries::Limited : fetchers::UseRegistries::No;
396398

397-
auto flake = getFlake(state, topRef, useRegistries, {});
399+
auto flake = getFlake(
400+
state,
401+
topRef,
402+
useRegistriesTop,
403+
{});
398404

399405
if (lockFlags.applyNixConfig) {
400406
flake.config.apply(settings);
@@ -569,7 +575,11 @@ LockedFlake lockFlake(
569575
if (auto resolvedPath = resolveRelativePath()) {
570576
return readFlake(state, ref, ref, ref, *resolvedPath, inputAttrPath);
571577
} else {
572-
return getFlake(state, ref, useRegistries, inputAttrPath);
578+
return getFlake(
579+
state,
580+
ref,
581+
useRegistriesInputs,
582+
inputAttrPath);
573583
}
574584
};
575585

@@ -717,7 +727,10 @@ LockedFlake lockFlake(
717727
if (auto resolvedPath = resolveRelativePath()) {
718728
return {*resolvedPath, *input.ref};
719729
} else {
720-
auto cachedInput = state.inputCache->getAccessor(state.store, input.ref->input, useRegistries);
730+
auto cachedInput = state.inputCache->getAccessor(
731+
state.store,
732+
input.ref->input,
733+
useRegistriesInputs);
721734

722735
auto lockedRef = FlakeRef(std::move(cachedInput.lockedInput), input.ref->subdir);
723736

@@ -834,7 +847,10 @@ LockedFlake lockFlake(
834847
repo, so we should re-read it. FIXME: we could
835848
also just clear the 'rev' field... */
836849
auto prevLockedRef = flake.lockedRef;
837-
flake = getFlake(state, topRef, useRegistries);
850+
flake = getFlake(
851+
state,
852+
topRef,
853+
useRegistriesTop);
838854

839855
if (lockFlags.commitLockFile &&
840856
flake.lockedRef.input.getRev() &&

src/libflake/flakeref.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
3737

3838
FlakeRef FlakeRef::resolve(
3939
ref<Store> store,
40-
const fetchers::RegistryFilter & filter) const
40+
fetchers::UseRegistries useRegistries) const
4141
{
42-
auto [input2, extraAttrs] = lookupInRegistries(store, input);
42+
auto [input2, extraAttrs] = lookupInRegistries(store, input, useRegistries);
4343
return FlakeRef(std::move(input2), fetchers::maybeGetStrAttr(extraAttrs, "dir").value_or(subdir));
4444
}
4545

src/libflake/include/nix/flake/flake.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct Flake
115115
}
116116
};
117117

118-
Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool useRegistries);
118+
Flake getFlake(EvalState & state, const FlakeRef & flakeRef, fetchers::UseRegistries useRegistries);
119119

120120
/**
121121
* Fingerprint of a locked flake; used as a cache key.

src/libflake/include/nix/flake/flakeref.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct FlakeRef
6565

6666
FlakeRef resolve(
6767
ref<Store> store,
68-
const fetchers::RegistryFilter & filter = {}) const;
68+
fetchers::UseRegistries useRegistries = fetchers::UseRegistries::All) const;
6969

7070
static FlakeRef fromAttrs(
7171
const fetchers::Settings & fetchSettings,

0 commit comments

Comments
 (0)