Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flags to override the lockfile #2943

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -84,6 +84,7 @@ perl/Makefile.config
/tests/restricted-innocent
/tests/shell
/tests/shell.drv
/tests/config.nix

# /tests/lang/
/tests/lang/*.out
Expand Down
10 changes: 1 addition & 9 deletions src/libexpr/common-eval-args.cc
Expand Up @@ -3,6 +3,7 @@
#include "download.hh"
#include "util.hh"
#include "eval.hh"
#include "flake/flakeref.hh"

namespace nix {

Expand Down Expand Up @@ -33,15 +34,6 @@ MixEvalArgs::MixEvalArgs()
.handler([&](std::vector<std::string> ss) {
evalSettings.pureEval = false;
});

mkFlag()
.longName("override-flake")
.labels({"original-ref", "resolved-ref"})
.description("override a flake registry value")
.arity(2)
.handler([&](std::vector<std::string> ss) {
registryOverrides.push_back(std::make_pair(ss[0], ss[1]));
});
}

Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
Expand Down
2 changes: 0 additions & 2 deletions src/libexpr/common-eval-args.hh
Expand Up @@ -16,8 +16,6 @@ struct MixEvalArgs : virtual Args

Strings searchPath;

std::vector<std::pair<std::string, std::string>> registryOverrides;

private:

std::map<std::string, std::string> autoArgs;
Expand Down
7 changes: 6 additions & 1 deletion src/libexpr/eval.hh
Expand Up @@ -6,6 +6,7 @@
#include "symbol-table.hh"
#include "hash.hh"
#include "config.hh"
#include "flake/flakeref.hh"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is necessary here, the FlakeRef concept is used now because registryOverrides are stored in the EvalState.


#include <map>
#include <unordered_map>
Expand Down Expand Up @@ -66,7 +67,7 @@ typedef std::list<SearchPathElem> SearchPath;
/* Initialise the Boehm GC, if applicable. */
void initGC();

typedef std::vector<std::pair<std::string, std::string>> RegistryOverrides;
typedef std::map<FlakeRef, FlakeRef> RegistryOverrides;


class EvalState
Expand Down Expand Up @@ -96,6 +97,8 @@ public:

RegistryOverrides registryOverrides;

RegistryOverrides lockFileOverrides;


private:
SrcToStore srcToStore;
Expand Down Expand Up @@ -221,6 +224,8 @@ public:

void addRegistryOverrides(RegistryOverrides overrides) { registryOverrides = overrides; }

void addLockFileOverrides(RegistryOverrides overrides) { lockFileOverrides = overrides; }

public:

/* The base environment, containing the builtin functions and
Expand Down
22 changes: 17 additions & 5 deletions src/libexpr/flake/flake.cc
Expand Up @@ -64,9 +64,7 @@ std::shared_ptr<FlakeRegistry> getUserRegistry()
std::shared_ptr<FlakeRegistry> getFlagRegistry(RegistryOverrides registryOverrides)
{
auto flagRegistry = std::make_shared<FlakeRegistry>();
for (auto const & x : registryOverrides) {
flagRegistry->entries.insert_or_assign(FlakeRef(x.first), FlakeRef(x.second));
}
flagRegistry->entries = registryOverrides;
return flagRegistry;
}

Expand Down Expand Up @@ -339,8 +337,15 @@ static std::pair<Flake, FlakeInput> updateLocks(
for (auto & input : flake.nonFlakeInputs) {
auto & id = input.first;
auto & ref = input.second;
auto j = state.lockFileOverrides.find(FlakeRef(id)); // The LockFile can be overrides by flags.
auto i = oldEntry.nonFlakeInputs.find(id);
if (i != oldEntry.nonFlakeInputs.end()) {
if (j != state.lockFileOverrides.end()) {
auto nonFlake = getNonFlake(state, j->second, allowedToUseRegistries(handleLockFile, false));
newEntry.nonFlakeInputs.insert_or_assign(id,
NonFlakeInput(
nonFlake.sourceInfo.resolvedRef,
nonFlake.sourceInfo.narHash));
} else if (i != oldEntry.nonFlakeInputs.end()) {
newEntry.nonFlakeInputs.insert_or_assign(i->first, i->second);
} else {
if (handleLockFile == AllPure || handleLockFile == TopRefUsesRegistries)
Expand All @@ -354,8 +359,15 @@ static std::pair<Flake, FlakeInput> updateLocks(
}

for (auto & inputRef : flake.inputs) {
auto j = state.lockFileOverrides.find(inputRef); // The LockFile can be overrides by flags.
auto i = oldEntry.flakeInputs.find(inputRef);
if (i != oldEntry.flakeInputs.end()) {
if (j != state.lockFileOverrides.end()) {
auto flake = getFlake(state, j->first, allowedToUseRegistries(handleLockFile, false));
newEntry.flakeInputs.insert_or_assign(inputRef,
updateLocks(state,
getFlake(state, j->second, allowedToUseRegistries(handleLockFile, false)),
handleLockFile, {}, false).second);
} else if (i != oldEntry.flakeInputs.end()) {
newEntry.flakeInputs.insert_or_assign(inputRef, i->second);
} else {
if (handleLockFile == AllPure || handleLockFile == TopRefUsesRegistries)
Expand Down
4 changes: 2 additions & 2 deletions src/nix/build.cc
Expand Up @@ -48,8 +48,8 @@ struct CmdBuild : MixDryRun, InstallablesCommand
{
auto buildables = build(store, dryRun ? DryRun : Build, installables);

auto evalState = std::make_shared<EvalState>(searchPath, store);
evalState->addRegistryOverrides(registryOverrides);
auto evalState = getEvalState();

if (dryRun) return;

for (size_t i = 0; i < buildables.size(); ++i) {
Expand Down
7 changes: 7 additions & 0 deletions src/nix/command.hh
Expand Up @@ -3,6 +3,7 @@
#include "args.hh"
#include "common-eval-args.hh"
#include <optional>
#include "flake/flakeref.hh"

namespace nix {

Expand Down Expand Up @@ -70,6 +71,12 @@ struct EvalCommand : virtual StoreCommand, MixEvalArgs
{
ref<EvalState> getEvalState();

std::map<FlakeRef, FlakeRef> registryOverrides;

std::map<FlakeRef, FlakeRef> lockFileOverrides;

EvalCommand();

private:

std::shared_ptr<EvalState> evalState;
Expand Down
1 change: 0 additions & 1 deletion src/nix/flake.cc
Expand Up @@ -136,7 +136,6 @@ struct CmdFlakeDeps : FlakeCommand
void run(nix::ref<nix::Store> store) override
{
auto evalState = getEvalState();
evalState->addRegistryOverrides(registryOverrides);

std::queue<ResolvedFlake> todo;
todo.push(resolveFlake());
Expand Down
23 changes: 23 additions & 0 deletions src/nix/installables.cc
Expand Up @@ -58,9 +58,32 @@ ref<EvalState> EvalCommand::getEvalState()
{
if (!evalState)
evalState = std::make_shared<EvalState>(searchPath, getStore());
evalState->addRegistryOverrides(registryOverrides);
evalState->addLockFileOverrides(lockFileOverrides);
return ref<EvalState>(evalState);
}

EvalCommand::EvalCommand()
{
mkFlag()
.longName("override-flake")
.labels({"original-ref", "resolved-ref"})
.description("override a flake registry value")
.arity(2)
.handler([&](std::vector<std::string> ss) {
registryOverrides.insert_or_assign(FlakeRef(ss[0]), FlakeRef(ss[1]));
});

mkFlag()
.longName("override-lock")
.labels({"original-ref", "resolved-ref"})
.description("override a lockfile value")
.arity(2)
.handler([&](std::vector<std::string> ss) {
lockFileOverrides.insert_or_assign(FlakeRef(ss[0]), FlakeRef(ss[1]));
});
}

Buildable Installable::toBuildable()
{
auto buildables = toBuildables();
Expand Down
5 changes: 5 additions & 0 deletions tests/flakes.sh
Expand Up @@ -328,3 +328,8 @@ git -C $flake3Dir checkout master
# Test whether fuzzy-matching works for IsGit
(! nix build -o $TEST_ROOT/result --flake-registry $registry flake4/removeXyzzy:xyzzy)
nix build -o $TEST_ROOT/result --flake-registry $registry flake4/removeXyzzy:sth

# Test overide-lockfile
nix build -o $TEST_ROOT/result --flake-registry $registry flake4:xyzzy --override-lock flake3 flake3/removeXyzzy
# Test that the flake registry is entirely used
[[ -z $(nix build -o $TEST_ROOT/result --flake-registry $registry flake3:xyzzy --override-lock flake2 flake1) ]]