From 18152406ce9db9491385f2e67da6f7f78e8d98d5 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sun, 12 Jul 2020 15:26:30 +0000 Subject: [PATCH] String .drv suffix to create derivation name --- src/libexpr/primops.cc | 2 +- src/libstore/daemon.cc | 2 +- src/libstore/derivations.cc | 11 ++++++++++- src/libstore/derivations.hh | 2 ++ src/nix-store/nix-store.cc | 2 +- src/nix/repl.cc | 2 +- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 14a7deed5..21bd4fb52 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -113,7 +113,7 @@ static void prim_scopedImport(EvalState & state, const Pos & pos, Value * * args }; if (auto optStorePath = isValidDerivationInStore()) { auto storePath = *optStorePath; - Derivation drv = readDerivation(*state.store, realPath, std::string(storePath.name())); + Derivation drv = readDerivation(*state.store, realPath, Derivation::nameFromPath(storePath)); Value & w = *state.allocValue(); state.mkAttrs(w, 3 + drv.outputs.size()); Value * v2 = state.allocAttr(w, state.sDrvPath); diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 4cf67064a..2ff53c964 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -474,7 +474,7 @@ static void performOp(TunnelLogger * logger, ref store, case wopBuildDerivation: { auto drvPath = store->parseStorePath(readString(from)); BasicDerivation drv; - readDerivation(from, *store, drv, std::string(drvPath.name())); + readDerivation(from, *store, drv, Derivation::nameFromPath(drvPath)); BuildMode buildMode = (BuildMode) readInt(from); logger->startWork(); if (!trusted) diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index 97fd21885..a85a70c0a 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -218,7 +218,7 @@ Derivation Store::readDerivation(const StorePath & drvPath) { auto accessor = getFSAccessor(); try { - return parseDerivation(*this, accessor->readFile(printStorePath(drvPath)), std::string(drvPath.name())); + return parseDerivation(*this, accessor->readFile(printStorePath(drvPath)), Derivation::nameFromPath(drvPath)); } catch (FormatError & e) { throw Error("error parsing derivation '%s': %s", printStorePath(drvPath), e.msg()); } @@ -465,6 +465,15 @@ StringSet BasicDerivation::outputNames() const } +std::string_view BasicDerivation::nameFromPath(const StorePath & drvPath) { + auto nameWithSuffix = drvPath.name(); + constexpr std::string_view extension = ".drv"; + assert(hasSuffix(nameWithSuffix, extension)); + nameWithSuffix.remove_suffix(extension.size()); + return nameWithSuffix; +} + + Source & readDerivation(Source & in, const Store & store, BasicDerivation & drv, std::string_view name) { drv.name = name; diff --git a/src/libstore/derivations.hh b/src/libstore/derivations.hh index 81c196ff7..6d3b54d3f 100644 --- a/src/libstore/derivations.hh +++ b/src/libstore/derivations.hh @@ -64,6 +64,8 @@ struct BasicDerivation /* Return the output names of a derivation. */ StringSet outputNames() const; + + static std::string_view nameFromPath(const StorePath & storePath); }; struct Derivation : BasicDerivation diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 39a47e19a..9605e4b32 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -916,7 +916,7 @@ static void opServe(Strings opFlags, Strings opArgs) auto drvPath = store->parseStorePath(readString(in)); BasicDerivation drv; - readDerivation(in, *store, drv, std::string(drvPath.name())); + readDerivation(in, *store, drv, Derivation::nameFromPath(drvPath)); getBuildSettings(); diff --git a/src/nix/repl.cc b/src/nix/repl.cc index bd72a3b19..74f0b3ec8 100644 --- a/src/nix/repl.cc +++ b/src/nix/repl.cc @@ -483,7 +483,7 @@ bool NixRepl::processLine(string line) but doing it in a child makes it easier to recover from problems / SIGINT. */ if (runProgram(settings.nixBinDir + "/nix", Strings{"build", "--no-link", drvPath}) == 0) { - auto drv = readDerivation(*state->store, drvPath, std::string(state->store->parseStorePath(drvPath).name())); + auto drv = readDerivation(*state->store, drvPath, Derivation::nameFromPath(state->store->parseStorePath(drvPath))); std::cout << std::endl << "this derivation produced the following outputs:" << std::endl; for (auto & i : drv.outputs) std::cout << fmt(" %s -> %s\n", i.first, state->store->printStorePath(i.second.path(*state->store, drv.name)));