aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-03-26 11:50:22 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-03-26 11:50:22 +0100
commitedb3836696fba5826c8716c1358fa977276a7753 (patch)
tree9a544c86dbed2ed38708712b34e1cdfdc085d0e6 /src
parentb5565a70812ce3c4dbf40a1e1bfe98ffd0ef45ed (diff)
parente007f367bd605ad14ddf84d1d5ad611aa427d338 (diff)
Merge remote-tracking branch 'tweag/auto-update-flake' into flakes
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/primops/flake.cc87
-rw-r--r--src/libexpr/primops/flake.hh23
-rw-r--r--src/libexpr/primops/flakeref.hh3
-rw-r--r--src/libutil/util.cc1
-rw-r--r--src/nix/build.cc14
-rw-r--r--src/nix/command.hh20
-rw-r--r--src/nix/flake.cc59
-rw-r--r--src/nix/installables.cc1
8 files changed, 188 insertions, 20 deletions
diff --git a/src/libexpr/primops/flake.cc b/src/libexpr/primops/flake.cc
index 3998f9ef9..48a036875 100644
--- a/src/libexpr/primops/flake.cc
+++ b/src/libexpr/primops/flake.cc
@@ -3,7 +3,9 @@
#include "eval-inline.hh"
#include "fetchGit.hh"
#include "download.hh"
+#include "args.hh"
+#include <iostream>
#include <queue>
#include <regex>
#include <nlohmann/json.hpp>
@@ -31,6 +33,18 @@ static std::unique_ptr<FlakeRegistry> readRegistry(const Path & path)
return registry;
}
+/* Write the registry or lock file to a file. */
+void writeRegistry(FlakeRegistry registry, Path path)
+{
+ nlohmann::json json = {};
+ json["version"] = 1;
+ json["flakes"] = {};
+ for (auto elem : registry.entries) {
+ json["flakes"][elem.first] = elem.second.ref.to_string();
+ }
+ writeFile(path, json.dump(4)); // The '4' is the number of spaces used in the indentation in the json file.
+}
+
const FlakeRegistry & EvalState::getFlakeRegistry()
{
std::call_once(_flakeRegistryInit, [&]()
@@ -95,9 +109,21 @@ struct FlakeSourceInfo
static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
{
- assert(flakeRef.isDirect());
+ FlakeRef directFlakeRef = FlakeRef(flakeRef);
+ if (!flakeRef.isDirect())
+ {
+ std::vector<const FlakeRegistry *> registries;
+ // 'pureEval' is a setting which cannot be changed in `nix flake`,
+ // but without flagging it off, we can't use any FlakeIds.
+ // if (!evalSettings.pureEval) {
+ registries.push_back(&state.getFlakeRegistry());
+ // }
+ directFlakeRef = lookupFlake(state, flakeRef, registries);
+ }
+ assert(directFlakeRef.isDirect());
+ // NOTE FROM NICK: I don't see why one wouldn't fetch FlakeId flakes..
- if (auto refData = std::get_if<FlakeRef::IsGitHub>(&flakeRef.data)) {
+ if (auto refData = std::get_if<FlakeRef::IsGitHub>(&directFlakeRef.data)) {
// FIXME: require hash in pure mode.
// FIXME: use regular /archive URLs instead? api.github.com
@@ -129,7 +155,7 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
return info;
}
- else if (auto refData = std::get_if<FlakeRef::IsGit>(&flakeRef.data)) {
+ else if (auto refData = std::get_if<FlakeRef::IsGit>(&directFlakeRef.data)) {
auto gitInfo = exportGit(state.store, refData->uri, refData->ref,
refData->rev ? refData->rev->to_string(Base16, false) : "", "source");
FlakeSourceInfo info;
@@ -141,20 +167,7 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
else abort();
}
-struct Flake
-{
- FlakeId id;
- std::string description;
- Path path;
- std::vector<FlakeRef> requires;
- std::unique_ptr<FlakeRegistry> lockFile;
- Value * vProvides; // FIXME: gc
- // commit hash
- // date
- // content hash
-};
-
-static Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
+Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
{
auto sourceInfo = fetchFlake(state, flakeRef);
debug("got flake source '%s' with revision %s",
@@ -166,7 +179,16 @@ static Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
if (state.allowedPaths)
state.allowedPaths->insert(flakePath);
- Flake flake;
+ FlakeRef newFlakeRef(flakeRef);
+ if (std::get_if<FlakeRef::IsGitHub>(&newFlakeRef.data)) {
+ FlakeSourceInfo srcInfo = fetchFlake(state, newFlakeRef);
+ if (srcInfo.rev) {
+ std::string uri = flakeRef.to_string();
+ newFlakeRef = FlakeRef(uri + "/" + srcInfo.rev->to_string());
+ }
+ }
+
+ Flake flake(newFlakeRef);
Value vInfo;
state.evalFile(flakePath + "/flake.nix", vInfo); // FIXME: symlink attack
@@ -259,6 +281,35 @@ static std::tuple<FlakeId, std::map<FlakeId, Flake>> resolveFlake(EvalState & st
return {*topFlakeId, std::move(done)};
}
+FlakeRegistry updateLockFile(EvalState & evalState, FlakeRef & flakeRef)
+{
+ FlakeRegistry newLockFile;
+ std::map<FlakeId, Flake> myDependencyMap = get<1>(resolveFlake(evalState, flakeRef, false));
+ // Nick assumed that "topRefPure" means that the Flake for flakeRef can be
+ // fetched purely.
+ for (auto const& require : myDependencyMap) {
+ FlakeRegistry::Entry entry = FlakeRegistry::Entry(require.second.ref);
+ // The FlakeRefs are immutable because they come out of the Flake objects,
+ // not from the requires.
+ newLockFile.entries.insert(std::pair<FlakeId, FlakeRegistry::Entry>(require.first, entry));
+ }
+ return newLockFile;
+}
+
+void updateLockFile(EvalState & state, std::string path)
+{
+ // 'path' is the path to the local flake repo.
+ FlakeRef flakeRef = FlakeRef("file://" + path);
+ if (std::get_if<FlakeRef::IsGit>(&flakeRef.data)) {
+ FlakeRegistry newLockFile = updateLockFile(state, flakeRef);
+ writeRegistry(newLockFile, path + "/flake.lock");
+ } else if (std::get_if<FlakeRef::IsGitHub>(&flakeRef.data)) {
+ throw UsageError("you can only update local flakes, not flakes on GitHub");
+ } else {
+ throw UsageError("you can only update local flakes, not flakes through their FlakeId");
+ }
+}
+
Value * makeFlakeValue(EvalState & state, std::string flakeUri, Value & v)
{
// FIXME: temporary hack to make the default installation source
diff --git a/src/libexpr/primops/flake.hh b/src/libexpr/primops/flake.hh
index e504dc196..b3a755311 100644
--- a/src/libexpr/primops/flake.hh
+++ b/src/libexpr/primops/flake.hh
@@ -13,6 +13,7 @@ struct FlakeRegistry
struct Entry
{
FlakeRef ref;
+ Entry(const FlakeRef & flakeRef) : ref(flakeRef) {};
};
std::map<FlakeId, Entry> entries;
};
@@ -21,4 +22,26 @@ Value * makeFlakeRegistryValue(EvalState & state);
Value * makeFlakeValue(EvalState & state, std::string flakeUri, Value & v);
+void writeRegistry(FlakeRegistry, Path);
+
+struct Flake
+{
+ FlakeId id;
+ FlakeRef ref;
+ std::string description;
+ Path path;
+ std::vector<FlakeRef> requires;
+ std::unique_ptr<FlakeRegistry> lockFile;
+ Value * vProvides; // FIXME: gc
+ // commit hash
+ // date
+ // content hash
+ Flake(FlakeRef & flakeRef) : ref(flakeRef) {};
+};
+
+Flake getFlake(EvalState &, const FlakeRef &);
+
+FlakeRegistry updateLockFile(EvalState &, Flake &);
+
+void updateLockFile(EvalState &, std::string);
}
diff --git a/src/libexpr/primops/flakeref.hh b/src/libexpr/primops/flakeref.hh
index ad0cf8630..4d1756b49 100644
--- a/src/libexpr/primops/flakeref.hh
+++ b/src/libexpr/primops/flakeref.hh
@@ -129,6 +129,9 @@ struct FlakeRef
// Parse a flake URI.
FlakeRef(const std::string & uri);
+ // Default constructor
+ FlakeRef(const FlakeRef & flakeRef) : data(flakeRef.data) {};
+
/* Unify two flake references so that the resulting reference
combines the information from both. For example,
"nixpkgs/<hash>" and "github:NixOS/nixpkgs" unifies to
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index e3dcd246c..b0a2b853e 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -344,7 +344,6 @@ void writeFile(const Path & path, Source & source, mode_t mode)
}
}
-
string readLine(int fd)
{
string s;
diff --git a/src/nix/build.cc b/src/nix/build.cc
index b329ac38a..5ab22e26c 100644
--- a/src/nix/build.cc
+++ b/src/nix/build.cc
@@ -1,3 +1,5 @@
+#include "primops/flake.hh"
+#include "eval.hh"
#include "command.hh"
#include "common-args.hh"
#include "shared.hh"
@@ -9,6 +11,8 @@ struct CmdBuild : MixDryRun, InstallablesCommand
{
Path outLink = "result";
+ std::optional<std::string> gitRepo = std::nullopt;
+
CmdBuild()
{
mkFlag()
@@ -22,6 +26,11 @@ struct CmdBuild : MixDryRun, InstallablesCommand
.longName("no-link")
.description("do not create a symlink to the build result")
.set(&outLink, Path(""));
+
+ mkFlag()
+ .longName("update-lock-file")
+ .description("update the lock file")
+ .dest(&gitRepo);
}
std::string name() override
@@ -52,6 +61,8 @@ struct CmdBuild : MixDryRun, InstallablesCommand
{
auto buildables = build(store, dryRun ? DryRun : Build, installables);
+ auto evalState = std::make_shared<EvalState>(searchPath, store);
+
if (dryRun) return;
for (size_t i = 0; i < buildables.size(); ++i) {
@@ -66,6 +77,9 @@ struct CmdBuild : MixDryRun, InstallablesCommand
store2->addPermRoot(output.second, absPath(symlink), true);
}
}
+
+ if(gitRepo)
+ updateLockFile(*evalState, *gitRepo);
}
};
diff --git a/src/nix/command.hh b/src/nix/command.hh
index a08347945..c58d5d56e 100644
--- a/src/nix/command.hh
+++ b/src/nix/command.hh
@@ -34,6 +34,26 @@ struct Buildable
typedef std::vector<Buildable> Buildables;
+struct GitRepoCommand : virtual Args
+{
+ std::string gitPath = absPath(".");
+
+ GitRepoCommand ()
+ {
+ expectArg("git-path", &gitPath, true);
+ }
+};
+
+struct FlakeCommand : virtual Args, StoreCommand, MixEvalArgs
+{
+ std::string flakeUri;
+
+ FlakeCommand()
+ {
+ expectArg("flake-uri", &flakeUri);
+ }
+};
+
struct Installable
{
virtual std::string what() = 0;
diff --git a/src/nix/flake.cc b/src/nix/flake.cc
index 9b36c3cbd..a5a1d34db 100644
--- a/src/nix/flake.cc
+++ b/src/nix/flake.cc
@@ -4,6 +4,7 @@
#include "shared.hh"
#include "progress-bar.hh"
#include "eval.hh"
+#include <nlohmann/json.hpp>
using namespace nix;
@@ -33,10 +34,66 @@ struct CmdFlakeList : StoreCommand, MixEvalArgs
}
};
+struct CmdFlakeUpdate : StoreCommand, GitRepoCommand, MixEvalArgs
+{
+ std::string name() override
+ {
+ return "update";
+ }
+
+ std::string description() override
+ {
+ return "update flake lock file";
+ }
+
+ void run(nix::ref<nix::Store> store) override
+ {
+ auto evalState = std::make_shared<EvalState>(searchPath, store);
+
+ if (flakeUri == "") flakeUri = absPath("./flake.nix");
+ int result = updateLockFile(*evalState, flakeUri);
+ if (result == 1) {
+ std::cout << "You can only update local flakes, not flakes on GitHub.\n";
+ } else if (result == 2) {
+ std::cout << "You can only update local flakes, not flakes through their FlakeId.\n";
+ }
+ }
+};
+
+struct CmdFlakeInfo : FlakeCommand, MixJSON
+{
+ std::string name() override
+ {
+ return "info";
+ }
+
+ std::string description() override
+ {
+ return "list info about a given flake";
+ }
+
+ void run(nix::ref<nix::Store> store) override
+ {
+ auto evalState = std::make_shared<EvalState>(searchPath, store);
+ nix::Flake flake = nix::getFlake(*evalState, FlakeRef(flakeUri));
+ if (json) {
+ nlohmann::json j;
+ j["location"] = flake.path;
+ j["description"] = flake.description;
+ std::cout << j.dump(4) << std::endl;
+ } else {
+ std::cout << "Description: " << flake.description << "\n";
+ std::cout << "Location: " << flake.path << "\n";
+ }
+ }
+};
+
struct CmdFlake : virtual MultiCommand, virtual Command
{
CmdFlake()
- : MultiCommand({make_ref<CmdFlakeList>()})
+ : MultiCommand({make_ref<CmdFlakeList>()
+ , make_ref<CmdFlakeInfo>()
+ , make_ref<CmdFlakeUpdate>()})
{
}
diff --git a/src/nix/installables.cc b/src/nix/installables.cc
index 0453c72c2..21e9e73b8 100644
--- a/src/nix/installables.cc
+++ b/src/nix/installables.cc
@@ -234,6 +234,7 @@ Buildables build(ref<Store> store, RealiseMode mode,
PathSet pathsToBuild;
for (auto & i : installables) {
+ std::cout << i->what() << std::endl;
for (auto & b : i->toBuildables()) {
if (b.drvPath != "") {
StringSet outputNames;