Skip to content

Commit 81c9402

Browse files
committed
Ensure backward compatibility for meta field in DerivationOptions JSON
Make the meta field optional in DerivationOptions JSON deserialization to maintain backward compatibility with JSON that predates the derivation-meta feature. Changes: - Use optionalValueAt instead of valueAt for meta field deserialization - Mark meta as optional in derivation-options-v1 schema - Add unit test for backward compatibility
1 parent 1cc946c commit 81c9402

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/manual/source/protocols/json/schema/derivation-options-v1.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ required:
2626
- requiredSystemFeatures
2727
- preferLocalBuild
2828
- allowSubstitutes
29-
- meta
29+
# Note: `meta` is not in the required list to maintain backward compatibility
30+
# with existing JSON that predates the derivation-meta feature
3031
properties:
3132
outputChecks:
3233
type: object

src/libstore-tests/derivation-advanced-attrs.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,17 @@ TEST_JSON_OPTIONS(CaDerivationAdvancedAttrsTest, structuredAttrs_all_set, struct
523523

524524
#undef TEST_JSON_OPTIONS
525525

526+
// Test backward compatibility: JSON without 'meta' field should be ingestible
527+
TEST_F(DerivationAdvancedAttrsTest, DerivationOptions_backward_compat_no_meta)
528+
{
529+
// Read existing JSON and remove the 'meta' field to simulate old format
530+
this->readTest("defaults.json", [&](auto encoded) {
531+
auto j = json::parse(encoded);
532+
j.erase("meta"); // Remove meta field to simulate old JSON
533+
auto got = j.template get<DerivationOptions<SingleDerivedPath>>();
534+
// Should successfully deserialize with meta = std::nullopt
535+
EXPECT_EQ(got.meta, std::nullopt);
536+
});
537+
}
538+
526539
} // namespace nix

src/libstore/derivation-options.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ DerivationOptions<SingleDerivedPath> adl_serializer<DerivationOptions<SingleDeri
628628
.requiredSystemFeatures = getStringSet(valueAt(json, "requiredSystemFeatures")),
629629
.preferLocalBuild = getBoolean(valueAt(json, "preferLocalBuild")),
630630
.allowSubstitutes = getBoolean(valueAt(json, "allowSubstitutes")),
631-
.meta = valueAt(json, "meta").get<std::optional<nlohmann::json::object_t>>(),
631+
.meta = [&]() -> std::optional<nlohmann::json::object_t> {
632+
if (auto * metaPtr = optionalValueAt(json, "meta"))
633+
return metaPtr->get<std::optional<nlohmann::json::object_t>>();
634+
return std::nullopt;
635+
}(),
632636
};
633637
}
634638

0 commit comments

Comments
 (0)