forked from lix-project/lix
Implemented --flake flag for nix build
Also fixed Eelco's PR comments
This commit is contained in:
parent
6542de98c2
commit
d4ee8afd59
|
@ -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>
|
||||
|
@ -32,10 +34,10 @@ static std::unique_ptr<FlakeRegistry> readRegistry(const Path & path)
|
|||
}
|
||||
|
||||
/* Write the registry or lock file to a file. */
|
||||
static void writeRegistry(FlakeRegistry registry, Path path = "./flake.lock")
|
||||
void writeRegistry(FlakeRegistry registry, Path path)
|
||||
{
|
||||
nlohmann::json json = {};
|
||||
json["value"] = 0; // Not sure whether this should be 0.
|
||||
json["version"] = 1;
|
||||
json["flakes"] = {};
|
||||
for (auto elem : registry.entries) {
|
||||
json["flakes"][elem.first] = elem.second.ref.to_string();
|
||||
|
@ -107,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
|
||||
|
@ -141,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;
|
||||
|
@ -165,7 +179,16 @@ 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
|
||||
|
@ -258,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(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
|
||||
|
|
|
@ -13,6 +13,7 @@ struct FlakeRegistry
|
|||
struct Entry
|
||||
{
|
||||
FlakeRef ref;
|
||||
Entry(const FlakeRef & flakeRef) : ref(flakeRef) {};
|
||||
};
|
||||
std::map<FlakeId, Entry> entries;
|
||||
};
|
||||
|
@ -21,9 +22,12 @@ 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;
|
||||
|
@ -32,9 +36,12 @@ struct Flake
|
|||
// commit hash
|
||||
// date
|
||||
// content hash
|
||||
Flake(FlakeRef & flakeRef) : ref(flakeRef) {};
|
||||
};
|
||||
|
||||
Flake getFlake(EvalState & state, const FlakeRef & flakeRef);
|
||||
Flake getFlake(EvalState &, const FlakeRef &);
|
||||
|
||||
void writeRegistry(FlakeRegistry);
|
||||
FlakeRegistry updateLockFile(EvalState &, Flake &);
|
||||
|
||||
void updateLockFile(EvalState &, std::string);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -344,7 +344,6 @@ void writeFile(const Path & path, Source & source, mode_t mode)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
string readLine(int fd)
|
||||
{
|
||||
string s;
|
||||
|
|
|
@ -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> flakeUri = 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("flake")
|
||||
.description("update lock file of given flake")
|
||||
.dest(&flakeUri);
|
||||
}
|
||||
|
||||
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,10 @@ struct CmdBuild : MixDryRun, InstallablesCommand
|
|||
store2->addPermRoot(output.second, absPath(symlink), true);
|
||||
}
|
||||
}
|
||||
|
||||
if (flakeUri) {
|
||||
updateLockFile(*evalState, *flakeUri);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -27,15 +27,6 @@ void StoreCommand::run()
|
|||
run(getStore());
|
||||
}
|
||||
|
||||
JsonFormattable::JsonFormattable()
|
||||
{
|
||||
mkFlag()
|
||||
.longName("json-formattable")
|
||||
.shortName('j')
|
||||
.description("output will be printed as json")
|
||||
.handler([&]() { jsonFormatting = true; });
|
||||
}
|
||||
|
||||
StorePathsCommand::StorePathsCommand(bool recursive)
|
||||
: recursive(recursive)
|
||||
{
|
||||
|
|
|
@ -26,13 +26,6 @@ private:
|
|||
std::shared_ptr<Store> _store;
|
||||
};
|
||||
|
||||
struct JsonFormattable : virtual Command
|
||||
{
|
||||
bool jsonFormatting = false;;
|
||||
|
||||
JsonFormattable();
|
||||
};
|
||||
|
||||
struct Buildable
|
||||
{
|
||||
Path drvPath; // may be empty
|
||||
|
|
|
@ -34,7 +34,28 @@ struct CmdFlakeList : StoreCommand, MixEvalArgs
|
|||
}
|
||||
};
|
||||
|
||||
struct CmdFlakeInfo : FlakeCommand, JsonFormattable
|
||||
struct CmdFlakeUpdate : FlakeCommand
|
||||
{
|
||||
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");
|
||||
updateLockFile(*evalState, flakeUri);
|
||||
}
|
||||
};
|
||||
|
||||
struct CmdFlakeInfo : FlakeCommand, MixJSON
|
||||
{
|
||||
std::string name() override
|
||||
{
|
||||
|
@ -50,7 +71,7 @@ struct CmdFlakeInfo : FlakeCommand, JsonFormattable
|
|||
{
|
||||
auto evalState = std::make_shared<EvalState>(searchPath, store);
|
||||
nix::Flake flake = nix::getFlake(*evalState, FlakeRef(flakeUri));
|
||||
if (jsonFormatting) {
|
||||
if (json) {
|
||||
nlohmann::json j;
|
||||
j["location"] = flake.path;
|
||||
j["description"] = flake.description;
|
||||
|
@ -65,7 +86,9 @@ struct CmdFlakeInfo : FlakeCommand, JsonFormattable
|
|||
struct CmdFlake : virtual MultiCommand, virtual Command
|
||||
{
|
||||
CmdFlake()
|
||||
: MultiCommand({make_ref<CmdFlakeList>(), make_ref<CmdFlakeInfo>()})
|
||||
: MultiCommand({make_ref<CmdFlakeList>()
|
||||
, make_ref<CmdFlakeInfo>()
|
||||
, make_ref<CmdFlakeUpdate>()})
|
||||
{
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue