From 3e57e3480b372ed9c5dffc1025eae5773b8691e7 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Fri, 2 Jul 2021 15:36:14 +0300 Subject: [PATCH] Add use-registries config option (and deprecate --no-registries flag) Some people want to avoid using registries at all on their system; Instead of having to add --no-registries to every command, this commit allows to set use-registries = false in the config. --no-registries is still allowed everywhere it was allowed previously, but is now deprecated. Co-authored-by: Eelco Dolstra --- src/libcmd/installables.cc | 5 ++++- src/libexpr/flake/flake.cc | 13 ++++++++----- src/libexpr/flake/flake.hh | 2 +- src/libstore/globals.hh | 3 +++ tests/flakes.sh | 4 ++++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index 658b415f3..0ffb97a5e 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -60,7 +60,10 @@ MixFlakeOptions::MixFlakeOptions() .longName = "no-registries", .description = "Don't allow lookups in the flake registries.", .category = category, - .handler = {&lockFlags.useRegistries, false} + .handler = {[&]() { + lockFlags.useRegistries = false; + warn("--no-registries is deprecated; use --no-use-registries (a.k.a --option use-registries false)"); + }} }); addFlag({ diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index e266bc36d..e7013b241 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -1,4 +1,5 @@ #include "flake.hh" +#include "eval.hh" #include "lockfile.hh" #include "primops.hh" #include "eval-inline.hh" @@ -296,7 +297,9 @@ LockedFlake lockFlake( FlakeCache flakeCache; - auto flake = getFlake(state, topRef, lockFlags.useRegistries, flakeCache); + auto useRegistries = lockFlags.useRegistries.value_or(settings.useRegistries); + + auto flake = getFlake(state, topRef, useRegistries, flakeCache); if (lockFlags.applyNixConfig) { flake.config.apply(); @@ -464,7 +467,7 @@ LockedFlake lockFlake( throw Error("cannot update flake input '%s' in pure mode", inputPathS); if (input.isFlake) { - auto inputFlake = getFlake(state, *input.ref, lockFlags.useRegistries, flakeCache); + auto inputFlake = getFlake(state, *input.ref, useRegistries, flakeCache); /* Note: in case of an --override-input, we use the *original* ref (input2.ref) for the @@ -499,7 +502,7 @@ LockedFlake lockFlake( else { auto [sourceInfo, resolvedRef, lockedRef] = fetchOrSubstituteTree( - state, *input.ref, lockFlags.useRegistries, flakeCache); + state, *input.ref, useRegistries, flakeCache); node->inputs.insert_or_assign(id, std::make_shared(lockedRef, *input.ref, false)); } @@ -573,7 +576,7 @@ LockedFlake lockFlake( also just clear the 'rev' field... */ auto prevLockedRef = flake.lockedRef; FlakeCache dummyCache; - flake = getFlake(state, topRef, lockFlags.useRegistries, dummyCache); + flake = getFlake(state, topRef, useRegistries, dummyCache); if (lockFlags.commitLockFile && flake.lockedRef.input.getRev() && @@ -643,7 +646,7 @@ static void prim_getFlake(EvalState & state, const Pos & pos, Value * * args, Va lockFlake(state, flakeRef, LockFlags { .updateLockFile = false, - .useRegistries = !evalSettings.pureEval, + .useRegistries = !evalSettings.pureEval && !settings.useRegistries, .allowMutable = !evalSettings.pureEval, }), v); diff --git a/src/libexpr/flake/flake.hh b/src/libexpr/flake/flake.hh index 4479e95db..15fd394f8 100644 --- a/src/libexpr/flake/flake.hh +++ b/src/libexpr/flake/flake.hh @@ -102,7 +102,7 @@ struct LockFlags /* Whether to use the registries to lookup indirect flake references like 'nixpkgs'. */ - bool useRegistries = true; + std::optional useRegistries = std::nullopt; /* Whether to apply flake's nixConfig attribute to the configuration */ diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index dd570cd63..a52d20376 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -956,6 +956,9 @@ public: resolves to a different location from that of the build machine. You can enable this setting if you are sure you're not going to do that. )"}; + + Setting useRegistries{this, true, "use-registries", + "Whether to use flake registries for reference resolution"}; }; diff --git a/tests/flakes.sh b/tests/flakes.sh index a4e657980..9e1b5b508 100644 --- a/tests/flakes.sh +++ b/tests/flakes.sh @@ -147,6 +147,7 @@ nix build -o $TEST_ROOT/result --expr "(builtins.getFlake \"git+file://$flake1Di # Building a flake with an unlocked dependency should fail in pure mode. (! nix build -o $TEST_ROOT/result flake2#bar --no-registries) +(! nix build -o $TEST_ROOT/result flake2#bar --no-use-registries) (! nix eval --expr "builtins.getFlake \"$flake2Dir\"") # But should succeed in impure mode. @@ -170,6 +171,7 @@ nix build -o $TEST_ROOT/result $flake2Dir#bar # Building with a lockfile should not require a fetch of the registry. nix build -o $TEST_ROOT/result --flake-registry file:///no-registry.json $flake2Dir#bar --refresh nix build -o $TEST_ROOT/result --no-registries $flake2Dir#bar --refresh +nix build -o $TEST_ROOT/result --no-use-registries $flake2Dir#bar --refresh # Updating the flake should not change the lockfile. nix flake lock $flake2Dir @@ -180,6 +182,7 @@ nix build -o $TEST_ROOT/result flake2#bar # Or without a registry. nix build -o $TEST_ROOT/result --no-registries git+file://$flake2Dir#bar --refresh +nix build -o $TEST_ROOT/result --no-use-registries git+file://$flake2Dir#bar --refresh # Test whether indirect dependencies work. nix build -o $TEST_ROOT/result $flake3Dir#xyzzy @@ -603,6 +606,7 @@ nix flake metadata --json hg+file://$flake5Dir [[ $(nix flake metadata --json hg+file://$flake5Dir | jq -e -r .revCount) = 1 ]] nix build -o $TEST_ROOT/result hg+file://$flake5Dir --no-registries --no-allow-dirty +nix build -o $TEST_ROOT/result hg+file://$flake5Dir --no-use-registries --no-allow-dirty # Test tarball flakes tar cfz $TEST_ROOT/flake.tar.gz -C $TEST_ROOT --exclude .hg flake5