Skip to content

Commit bccd6a4

Browse files
Various review changes for meta in provenance
1 parent bb832c0 commit bccd6a4

File tree

7 files changed

+77
-21
lines changed

7 files changed

+77
-21
lines changed

src/libexpr/include/nix/expr/eval.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ struct StaticEvalSymbols
228228
line, column, functor, toString, right, wrong, structuredAttrs, json, allowedReferences, allowedRequisites,
229229
disallowedReferences, disallowedRequisites, maxSize, maxClosureSize, builder, args, contentAddressed, impure,
230230
outputHash, outputHashAlgo, outputHashMode, recurseForDerivations, description, self, epsilon, startSet,
231-
operator_, key, path, prefix, outputSpecified;
231+
operator_, key, path, prefix, outputSpecified, __meta;
232232

233233
Expr::AstSymbols exprSymbols;
234234

@@ -281,6 +281,7 @@ struct StaticEvalSymbols
281281
.path = alloc.create("path"),
282282
.prefix = alloc.create("prefix"),
283283
.outputSpecified = alloc.create("outputSpecified"),
284+
.__meta = alloc.create("__meta"),
284285
.exprSymbols = {
285286
.sub = alloc.create("__sub"),
286287
.lessThan = alloc.create("__lessThan"),

src/libexpr/include/nix/expr/provenance.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace nix {
88
* Provenance indicating that this store path was instantiated by the `derivation` builtin function. Its main purpose is
99
* to record `meta` fields.
1010
*/
11-
struct MetaProvenance : Provenance
11+
struct DerivationProvenance : Provenance
1212
{
1313
std::shared_ptr<const Provenance> next;
1414
ref<nlohmann::json> meta;
1515

16-
MetaProvenance(std::shared_ptr<const Provenance> next, ref<nlohmann::json> meta)
16+
DerivationProvenance(std::shared_ptr<const Provenance> next, ref<nlohmann::json> meta)
1717
: next(std::move(next))
1818
, meta(std::move(meta)) {};
1919

src/libexpr/primops.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,10 +1574,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
15741574
experimentalFeatureSettings.require(Xp::ImpureDerivations);
15751575
}
15761576
break;
1577-
case EvalState::s.meta.getId():
1578-
experimentalFeatureSettings.require(Xp::Provenance);
1579-
1580-
{
1577+
case EvalState::s.__meta.getId():
1578+
if (experimentalFeatureSettings.isEnabled(Xp::Provenance)) {
15811579
auto meta = printValueAsJSON(state, true, *i->value, pos, context);
15821580

15831581
for (auto it = meta.begin(); it != meta.end();) {
@@ -1589,7 +1587,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
15891587
it = meta.erase(it);
15901588
}
15911589

1592-
provenance = std::make_shared<const MetaProvenance>(provenance, make_ref<nlohmann::json>(meta));
1590+
provenance =
1591+
std::make_shared<const DerivationProvenance>(provenance, make_ref<nlohmann::json>(meta));
15931592
}
15941593
break;
15951594
/* The `args' attribute is special: it supplies the

src/libexpr/provenance.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
namespace nix {
77

8-
nlohmann::json MetaProvenance::to_json() const
8+
nlohmann::json DerivationProvenance::to_json() const
99
{
1010
return nlohmann::json{
11-
{"type", "meta"},
11+
{"type", "derivation"},
1212
{"meta", *meta},
1313
{"next", next ? next->to_json() : nlohmann::json(nullptr)},
1414
};
1515
}
1616

17-
Provenance::Register registerMetaProvenance("meta", [](nlohmann::json json) {
17+
Provenance::Register registerDerivationProvenance("derivation", [](nlohmann::json json) {
1818
auto & obj = getObject(json);
1919
std::shared_ptr<const Provenance> next;
2020
if (auto p = optionalValueAt(obj, "next"); p && !p->is_null())
2121
next = Provenance::from_json(*p);
22-
return make_ref<MetaProvenance>(next, make_ref<nlohmann::json>(valueAt(obj, "meta")));
22+
return make_ref<DerivationProvenance>(next, make_ref<nlohmann::json>(valueAt(obj, "meta")));
2323
});
2424

2525
} // namespace nix

src/nix/provenance.cc

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nix/flake/provenance.hh"
66
#include "nix/fetchers/provenance.hh"
77
#include "nix/util/provenance.hh"
8+
#include "nix/util/json-utils.hh"
89

910
#include <memory>
1011
#include <nlohmann/json.hpp>
@@ -93,9 +94,60 @@ struct CmdProvenanceShow : StorePathsCommand
9394
} else if (auto subpath = std::dynamic_pointer_cast<const SubpathProvenance>(provenance)) {
9495
logger->cout("← from file " ANSI_BOLD "%s" ANSI_NORMAL, subpath->subpath.abs());
9596
provenance = subpath->next;
96-
} else if (auto meta = std::dynamic_pointer_cast<const MetaProvenance>(provenance)) {
97-
logger->cout("← with metadata");
98-
provenance = meta->next;
97+
} else if (auto drv = std::dynamic_pointer_cast<const DerivationProvenance>(provenance)) {
98+
logger->cout("← with derivation metadata");
99+
#define TAB " "
100+
auto json = getObject(*(drv->meta));
101+
if (auto identifiers = optionalValueAt(json, "identifiers")) {
102+
auto ident = getObject(*identifiers);
103+
if (auto cpeParts = optionalValueAt(ident, "cpeParts")) {
104+
auto parts = getObject(*cpeParts);
105+
106+
auto vendor = parts["vendor"];
107+
auto product = parts["product"];
108+
auto version = parts["version"];
109+
auto update = parts["update"];
110+
111+
logger->cout(
112+
TAB "" ANSI_BOLD "CPE:" ANSI_NORMAL " cpe:2.3:a:%s:%s:%s:%s:*:*:*:*:*:*",
113+
vendor.is_null() ? "*" : vendor.get<std::string>(),
114+
product.is_null() ? "*" : product.get<std::string>(),
115+
version.is_null() ? "*" : version.get<std::string>(),
116+
update.is_null() ? "*" : update.get<std::string>());
117+
}
118+
}
119+
if (auto license = optionalValueAt(json, "license")) {
120+
if (license->is_array()) {
121+
logger->cout(TAB "" ANSI_BOLD "Licenses:" ANSI_NORMAL);
122+
auto licenses = getArray(*license);
123+
for (auto it = licenses.begin(); it != licenses.end(); it++) {
124+
auto license = getObject(*it);
125+
auto shortName = license["shortName"];
126+
logger->cout(TAB "" TAB "- %s", shortName.get<std::string>());
127+
}
128+
} else {
129+
auto obj = getObject(*license);
130+
auto shortName = obj["shortName"];
131+
logger->cout(TAB "" ANSI_BOLD "License:" ANSI_NORMAL " %s", shortName.get<std::string>());
132+
}
133+
}
134+
if (auto licenses = optionalValueAt(json, "licenses")) {
135+
if (licenses->is_array()) {
136+
logger->cout(TAB "" ANSI_BOLD "Licenses:" ANSI_NORMAL);
137+
auto licensesArray = getArray(*licenses);
138+
for (auto it = licensesArray.begin(); it != licensesArray.end(); it++) {
139+
auto license = getObject(*it);
140+
auto shortName = license["shortName"];
141+
logger->cout(TAB "" TAB "- %s", shortName.get<std::string>());
142+
}
143+
} else {
144+
auto license = getObject(*licenses);
145+
auto shortName = license["shortName"];
146+
logger->cout(TAB "" ANSI_BOLD "License:" ANSI_NORMAL " %s", shortName.get<std::string>());
147+
}
148+
}
149+
#undef TAB
150+
provenance = drv->next;
99151
} else {
100152
// Unknown or unhandled provenance type
101153
auto json = provenance->to_json();

tests/functional/config.nix.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ rec {
2828
eval "$buildCommand"
2929
'')];
3030
PATH = path;
31-
} // caArgs // removeAttrs args (["builder"] ++ optional (builtins.langVersion < 7) "meta"))
32-
// optionalAttrs (builtins.langVersion < 7) { meta = args.meta or {}; };
31+
} // caArgs // optionalAttrs (builtins.langVersion >= 7) {
32+
__meta = args.meta or {};
33+
} // removeAttrs args ["builder" "meta"])
34+
// { meta = args.meta or {}; };
3335
}

tests/functional/flakes/provenance.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ builder=$(nix eval --raw "$flake1Dir#packages.$system.default._builder")
5353
},
5454
"type": "flake"
5555
},
56-
"type": "meta"
56+
"type": "derivation"
5757
},
5858
"output": "out",
5959
"system": "$system",
@@ -145,7 +145,7 @@ nix copy --from "file://$binaryCache" "$outPath" --no-check-sigs
145145
},
146146
"type": "flake"
147147
},
148-
"type": "meta"
148+
"type": "derivation"
149149
},
150150
"output": "out",
151151
"system": "$system",
@@ -161,7 +161,9 @@ EOF
161161
$outPath
162162
← copied from file://$binaryCache
163163
← built from derivation $drvPath (output out) on test-host for $system
164-
← with metadata
164+
← with derivation metadata
165+
Licenses:
166+
- lgpl21
165167
← instantiated from flake output git+file://$flake1Dir?ref=refs/heads/master&rev=$rev#packages.$system.default
166168
EOF
167169
) ]]
@@ -185,7 +187,7 @@ nix build --impure --print-out-paths --no-link "$flake1Dir#packages.$system.defa
185187
]
186188
},
187189
"next": null,
188-
"type": "meta"
190+
"type": "derivation"
189191
}
190192
EOF
191193
)" ]]

0 commit comments

Comments
 (0)