Skip to content

Commit

Permalink
DerivationOptions now creatable from just Env
Browse files Browse the repository at this point in the history
This means that the functionality of `ParsedDerivation` is now approaching
zero (it still provides get*Attr/s).
  • Loading branch information
haenoe committed May 24, 2024
1 parent 94e13bf commit 16f323f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,14 @@ Derivation parseDerivation(

expect(str, ")");

drv.options = DerivationOptions::fromEnv(/*env*/ drv);
drv.options = DerivationOptions::fromEnv(drv.env);

return drv;
}

DerivationOptions DerivationOptions::fromEnv(const BasicDerivation /*StringPairs*/ & drv)
DerivationOptions DerivationOptions::fromEnv(const StringPairs & env)
{
ParsedDerivation parsed = {drv};
ParsedDerivation parsed = {env};

DerivationOptions defaults = {};

Expand Down Expand Up @@ -985,7 +985,7 @@ Source & readDerivation(Source & in, const StoreDirConfig & store, BasicDerivati
drv.env[key] = value;
}

drv.options = DerivationOptions::fromEnv(/*drv.env*/drv);
drv.options = DerivationOptions::fromEnv(drv.env);

return in;
}
Expand Down
16 changes: 4 additions & 12 deletions src/libstore/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ struct DerivationOptions
* but this should become possible again once we shrink down
* `ParsedDerivation` so it just as the `get*Attr` methods.
*/
static DerivationOptions fromEnv(const BasicDerivation /*StringPairs*/ & env);
static DerivationOptions fromEnv(const StringPairs & env);
};

struct BasicDerivation
Expand Down Expand Up @@ -486,17 +486,9 @@ struct Derivation : BasicDerivation
const nlohmann::json & json,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);

bool operator ==(const Derivation &) const = default;

/* TODO: Fix
* src/libstore/derivations.hh:429:10: warning: explicitly defaulted three-way comparison operator is implicitly deleted [-Wdefaulted-function-deleted]
* auto operator <=>(const Derivation &) const = default;
* ^
* src/libstore/derivations.hh:380:42: note: defaulted 'operator<=>' is implicitly deleted because there is no viable three-way comparison function for member 'inputDrvs'
* DerivedPathMap<std::set<OutputName>> inputDrvs;
* candidate template ignored: could not match 'forward_list' against 'DerivedPathMap'
*/
auto operator <=>(const Derivation &) const = default;
GENERATE_CMP(Derivation,
static_cast<const BasicDerivation &>(*me),
me->inputDrvs);
};


Expand Down
2 changes: 1 addition & 1 deletion src/libstore/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void Store::queryMissing(const std::vector<DerivedPath> & targets,
if (knownOutputPaths && invalid.empty()) return;

auto drv = make_ref<Derivation>(derivationFromPath(drvPath));
ParsedDerivation parsedDrv(*drv);
ParsedDerivation parsedDrv(drv->env);

if (!knownOutputPaths && settings.useSubstitutes && drv->substitutesAllowed()) {
experimentalFeatureSettings.require(Xp::CaDerivations);
Expand Down
23 changes: 12 additions & 11 deletions src/libstore/parsed-derivations.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "parsed-derivations.hh"
#include "derivations.hh"

#include <nlohmann/json.hpp>
#include <regex>

namespace nix {

ParsedDerivation::ParsedDerivation(const BasicDerivation & drv)
: drv(drv)
ParsedDerivation::ParsedDerivation(const StringPairs & env)
: env(env)
{
/* Parse the __json attribute, if any. */
auto jsonAttr = drv.env.find("__json");
if (jsonAttr != drv.env.end()) {
auto jsonAttr = env.find("__json");
if (jsonAttr != env.end()) {
try {
structuredAttrs = std::make_unique<nlohmann::json>(nlohmann::json::parse(jsonAttr->second));
} catch (std::exception & e) {
Expand All @@ -33,8 +34,8 @@ std::optional<std::string> ParsedDerivation::getStringAttr(const std::string & n
return i->get<std::string>();
}
} else {
auto i = drv.env.find(name);
if (i == drv.env.end())
auto i = env.find(name);
if (i == env.end())
return {};
else
return i->second;
Expand All @@ -53,8 +54,8 @@ bool ParsedDerivation::getBoolAttr(const std::string & name, bool def) const
return i->get<bool>();
}
} else {
auto i = drv.env.find(name);
if (i == drv.env.end())
auto i = env.find(name);
if (i == env.end())
return def;
else
return i->second == "1";
Expand All @@ -79,8 +80,8 @@ std::optional<Strings> ParsedDerivation::getStringsAttr(const std::string & name
return res;
}
} else {
auto i = drv.env.find(name);
if (i == drv.env.end())
auto i = env.find(name);
if (i == env.end())
return {};
else
return tokenizeString<Strings>(i->second);
Expand Down Expand Up @@ -124,7 +125,7 @@ static nlohmann::json pathInfoToJSON(
return jsonList;
}

std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths)
std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths, const BasicDerivation & drv)
{
auto structuredAttrs = getStructuredAttrs();
if (!structuredAttrs) return std::nullopt;
Expand Down
6 changes: 3 additions & 3 deletions src/libstore/parsed-derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace nix {

class ParsedDerivation
{
const BasicDerivation & drv;
const StringPairs & env;
std::unique_ptr<nlohmann::json> structuredAttrs;

public:

ParsedDerivation(const BasicDerivation & drv);
ParsedDerivation(const StringPairs & env);

~ParsedDerivation();

Expand All @@ -30,7 +30,7 @@ public:

std::optional<Strings> getStringsAttr(const std::string & name) const;

std::optional<nlohmann::json> prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths);
std::optional<nlohmann::json> prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths, const BasicDerivation & drv);
};

std::string writeStructuredAttrsShell(const nlohmann::json & json);
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/unix/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void DerivationGoal::haveDerivation()
{
trace("have derivation");

parsedDrv = std::make_unique<ParsedDerivation>(*drv);
parsedDrv = std::make_unique<ParsedDerivation>(drv->env);

if (!drv->type().hasKnownOutputPaths())
experimentalFeatureSettings.require(Xp::CaDerivations);
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ void LocalDerivationGoal::initEnv()

void LocalDerivationGoal::writeStructuredAttrs()
{
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, inputPaths)) {
if (auto structAttrsJson = parsedDrv->prepareStructuredAttrs(worker.store, inputPaths, *drv)) {
auto json = structAttrsJson.value();
nlohmann::json rewritten;
for (auto & [i, v] : json["outputs"].get<nlohmann::json::object_t>()) {
Expand Down
4 changes: 2 additions & 2 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,9 @@ static void main_nix_build(int argc, char * * argv)
for (const auto & [inputDrv, inputNode] : drv.inputDrvs.map)
accumInputClosure(inputDrv, inputNode);

ParsedDerivation parsedDrv(drv);
ParsedDerivation parsedDrv(drv.env);

if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, inputs)) {
if (auto structAttrs = parsedDrv.prepareStructuredAttrs(*store, inputs, drv)) {
auto json = structAttrs.value();
structuredAttrsRC = writeStructuredAttrsShell(json);

Expand Down

0 comments on commit 16f323f

Please sign in to comment.