Merge pull request #3434 from Ericson2314/derivation-header-include-order

Revise division of labor in deserialization of derivations
This commit is contained in:
Eelco Dolstra 2020-08-27 16:39:28 +02:00 committed by GitHub
commit eb75282b8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 48 deletions

View file

@ -1,6 +1,6 @@
makefiles = local.mk makefiles = local.mk
GLOBAL_CXXFLAGS += -g -Wall GLOBAL_CXXFLAGS += -g -Wall -std=c++17
-include Makefile.config -include Makefile.config

View file

@ -101,7 +101,7 @@ static void prim_scopedImport(EvalState & state, const Pos & pos, Value * * args
}; };
if (auto optStorePath = isValidDerivationInStore()) { if (auto optStorePath = isValidDerivationInStore()) {
auto storePath = *optStorePath; auto storePath = *optStorePath;
Derivation drv = readDerivation(*state.store, realPath, Derivation::nameFromPath(storePath)); Derivation drv = state.store->readDerivation(storePath);
Value & w = *state.allocValue(); Value & w = *state.allocValue();
state.mkAttrs(w, 3 + drv.outputs.size()); state.mkAttrs(w, 3 + drv.outputs.size());
Value * v2 = state.allocAttr(w, state.sDrvPath); Value * v2 = state.allocAttr(w, state.sDrvPath);

View file

@ -185,7 +185,7 @@ static DerivationOutput parseDerivationOutput(const Store & store, std::istrings
} }
static Derivation parseDerivation(const Store & store, std::string && s, std::string_view name) Derivation parseDerivation(const Store & store, std::string && s, std::string_view name)
{ {
Derivation drv; Derivation drv;
drv.name = name; drv.name = name;
@ -233,34 +233,6 @@ static Derivation parseDerivation(const Store & store, std::string && s, std::st
} }
Derivation readDerivation(const Store & store, const Path & drvPath, std::string_view name)
{
try {
return parseDerivation(store, readFile(drvPath), name);
} catch (FormatError & e) {
throw Error("error parsing derivation '%1%': %2%", drvPath, e.msg());
}
}
Derivation Store::derivationFromPath(const StorePath & drvPath)
{
ensurePath(drvPath);
return readDerivation(drvPath);
}
Derivation Store::readDerivation(const StorePath & drvPath)
{
auto accessor = getFSAccessor();
try {
return parseDerivation(*this, accessor->readFile(printStorePath(drvPath)), Derivation::nameFromPath(drvPath));
} catch (FormatError & e) {
throw Error("error parsing derivation '%s': %s", printStorePath(drvPath), e.msg());
}
}
static void printString(string & res, std::string_view s) static void printString(string & res, std::string_view s)
{ {
char buf[s.size() * 2 + 2]; char buf[s.size() * 2 + 2];

View file

@ -150,7 +150,7 @@ StorePath writeDerivation(Store & store,
const Derivation & drv, RepairFlag repair = NoRepair); const Derivation & drv, RepairFlag repair = NoRepair);
/* Read a derivation from a file. */ /* Read a derivation from a file. */
Derivation readDerivation(const Store & store, const Path & drvPath, std::string_view name); Derivation parseDerivation(const Store & store, std::string && s, std::string_view name);
// FIXME: remove // FIXME: remove
bool isDerivation(const string & fileName); bool isDerivation(const string & fileName);

View file

@ -23,9 +23,6 @@ namespace nix {
const int nixSchemaVersion = 10; const int nixSchemaVersion = 10;
struct Derivation;
struct OptimiseStats struct OptimiseStats
{ {
unsigned long filesLinked = 0; unsigned long filesLinked = 0;

View file

@ -1,11 +1,11 @@
#include "crypto.hh" #include "crypto.hh"
#include "fs-accessor.hh"
#include "globals.hh" #include "globals.hh"
#include "store-api.hh" #include "store-api.hh"
#include "util.hh" #include "util.hh"
#include "nar-info-disk-cache.hh" #include "nar-info-disk-cache.hh"
#include "thread-pool.hh" #include "thread-pool.hh"
#include "json.hh" #include "json.hh"
#include "derivations.hh"
#include "url.hh" #include "url.hh"
#include "archive.hh" #include "archive.hh"
@ -983,6 +983,26 @@ Strings ValidPathInfo::shortRefs() const
} }
Derivation Store::derivationFromPath(const StorePath & drvPath)
{
ensurePath(drvPath);
return readDerivation(drvPath);
}
Derivation Store::readDerivation(const StorePath & drvPath)
{
auto accessor = getFSAccessor();
try {
return parseDerivation(*this,
accessor->readFile(printStorePath(drvPath)),
Derivation::nameFromPath(drvPath));
} catch (FormatError & e) {
throw Error("error parsing derivation '%s': %s", printStorePath(drvPath), e.msg());
}
}
} }

View file

@ -64,7 +64,7 @@ struct NixRepl
void mainLoop(const std::vector<std::string> & files); void mainLoop(const std::vector<std::string> & files);
StringSet completePrefix(string prefix); StringSet completePrefix(string prefix);
bool getLine(string & input, const std::string &prompt); bool getLine(string & input, const std::string &prompt);
Path getDerivationPath(Value & v); StorePath getDerivationPath(Value & v);
bool processLine(string line); bool processLine(string line);
void loadFile(const Path & path); void loadFile(const Path & path);
void initEnv(); void initEnv();
@ -375,13 +375,16 @@ bool isVarName(const string & s)
} }
Path NixRepl::getDerivationPath(Value & v) { StorePath NixRepl::getDerivationPath(Value & v) {
auto drvInfo = getDerivation(*state, v, false); auto drvInfo = getDerivation(*state, v, false);
if (!drvInfo) if (!drvInfo)
throw Error("expression does not evaluate to a derivation, so I can't build it"); throw Error("expression does not evaluate to a derivation, so I can't build it");
Path drvPath = drvInfo->queryDrvPath(); Path drvPathRaw = drvInfo->queryDrvPath();
if (drvPath == "" || !state->store->isValidPath(state->store->parseStorePath(drvPath))) if (drvPathRaw == "")
throw Error("expression did not evaluate to a valid derivation"); throw Error("expression did not evaluate to a valid derivation (no drv path)");
StorePath drvPath = state->store->parseStorePath(drvPathRaw);
if (!state->store->isValidPath(drvPath))
throw Error("expression did not evaluate to a valid derivation (invalid drv path)");
return drvPath; return drvPath;
} }
@ -474,29 +477,30 @@ bool NixRepl::processLine(string line)
evalString("drv: (import <nixpkgs> {}).runCommand \"shell\" { buildInputs = [ drv ]; } \"\"", f); evalString("drv: (import <nixpkgs> {}).runCommand \"shell\" { buildInputs = [ drv ]; } \"\"", f);
state->callFunction(f, v, result, Pos()); state->callFunction(f, v, result, Pos());
Path drvPath = getDerivationPath(result); StorePath drvPath = getDerivationPath(result);
runProgram(settings.nixBinDir + "/nix-shell", Strings{drvPath}); runProgram(settings.nixBinDir + "/nix-shell", Strings{state->store->printStorePath(drvPath)});
} }
else if (command == ":b" || command == ":i" || command == ":s") { else if (command == ":b" || command == ":i" || command == ":s") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
Path drvPath = getDerivationPath(v); StorePath drvPath = getDerivationPath(v);
Path drvPathRaw = state->store->printStorePath(drvPath);
if (command == ":b") { if (command == ":b") {
/* We could do the build in this process using buildPaths(), /* We could do the build in this process using buildPaths(),
but doing it in a child makes it easier to recover from but doing it in a child makes it easier to recover from
problems / SIGINT. */ problems / SIGINT. */
if (runProgram(settings.nixBinDir + "/nix", Strings{"build", "--no-link", drvPath}) == 0) { if (runProgram(settings.nixBinDir + "/nix", Strings{"build", "--no-link", drvPathRaw}) == 0) {
auto drv = readDerivation(*state->store, drvPath, Derivation::nameFromPath(state->store->parseStorePath(drvPath))); auto drv = state->store->readDerivation(drvPath);
std::cout << std::endl << "this derivation produced the following outputs:" << std::endl; std::cout << std::endl << "this derivation produced the following outputs:" << std::endl;
for (auto & i : drv.outputsAndPaths(*state->store)) for (auto & i : drv.outputsAndPaths(*state->store))
std::cout << fmt(" %s -> %s\n", i.first, state->store->printStorePath(i.second.second)); std::cout << fmt(" %s -> %s\n", i.first, state->store->printStorePath(i.second.second));
} }
} else if (command == ":i") { } else if (command == ":i") {
runProgram(settings.nixBinDir + "/nix-env", Strings{"-i", drvPath}); runProgram(settings.nixBinDir + "/nix-env", Strings{"-i", drvPathRaw});
} else { } else {
runProgram(settings.nixBinDir + "/nix-shell", Strings{drvPath}); runProgram(settings.nixBinDir + "/nix-shell", Strings{drvPathRaw});
} }
} }