Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse TOML timestamps as strings #8120

Merged

Conversation

andreabedini
Copy link
Contributor

Motivation

Writing foliage I have choosen TOML knowing that nix would be able to read it natively. As it turns out, I cannot parse foliage input metadata because it includes timestamps, which nix does not support. 💔

Context

Currently fromTOML throws an exception when encountering a timestamp since the nix language lacks a way to represent them. This patch changes this beaviour and makes fromTOML parse timestamps as their were strings.

I believe this is the right thing to do here, given that a string is better than nothing.

Note that, ifwe had toTOML, ensuring fromTOML (toTOML x) == x might be impossible anyway because nix values can be expressed in multiple ways in TOML.

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

Priorities

Add 👍 to pull requests you find important.

@andreabedini andreabedini requested a review from edolstra as a code owner March 28, 2023 04:52
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Mar 28, 2023
@thufschmitt thufschmitt added the idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. label Apr 7, 2023
@Ericson2314
Copy link
Member

Ericson2314 commented Apr 10, 2023

Discussed at last Nix team meeting.

Verdict: Let's do this, but in a way that round-trips properly, and as an experimental feature.

Expand details for an idea of how round-tripping could work.

- @edolstra: Probably the best we can do. The alternative would be encoding it in a structured way, which would be fairly complicated.
  • @roberth: Parsing it in a structured way might actually be cool. In particular for NixOS use-case.
    Middle-ground: parse it as { _type = "timestamp"; value = "${theRawTSString}"; }

  • @Ericson2314: Should probably make it experimental, then we can refine it over time, and unblock the person without stabilizing anything rashly.

  • @roberth: Sure, but I don't mind stabilizing it sooner.

@andreabedini
Copy link
Contributor Author

👍 I think we should follow the TOML's definitions (Offset Date-Time, Local Date-Time, Local Date, Local Date). These types are already available from toml++. AFAIK they are all subsets of RFC 3339 anyway.

With some luck I'll find some time to work on this week or the next.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-04-10-nix-team-meeting-minutes-47/27357/1

throw std::runtime_error("Dates and times are not supported");
case toml::value_t::local_time: {
auto attrs = state.buildBindings(2);
attrs.alloc("_type").mkString("timestamp");
Copy link
Member

@edolstra edolstra May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"timestamp" is a bit too generic, maybe it should be "toml-datetime" or something like that.

Also, maybe the specific TOML type (toml::value_t::{local_datetime,...}) should be stored in the attrset (either encoded in _type, or as a tomlType attribute). If we ever add a toTOML builtin, that should make it easier to round-trip.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #3929

@fricklerhandwerk
Copy link
Contributor

Reviewed in Nix team meeting 2023-05-08:

We still want this, but we also want to get it right. As @edolstra pointed out we have to take care of not occupying the _type namespace haphazardly, and also look out for round-tripping TOML correctly. We didn't come to a conclusion, and still owe a clear decision. Suggestions how to proceed are welcome.

Complete discussion
  • @edolstra: the attrset type should say rawTOML
    • since _type is in a global namespace, no one else will be able to implement timestamps on top of that
    • @roberth: agreed. calling it timestamp comes with expectations we may have to maintain code around
      • the only objection may be that it's a scary name
      • @edolstra: all we have to express that it will be returned verbatim when serialising TOML again
  • @edolstra: on the other hand, our TOML library can represent the different TOML time types, so maybe we can express this in the output
  • @roberth: not having a toTOML is a bit of a liability
    • we wanted round-tripping, but currently have no way to test correctness
  • needs more discussion about how to proceed

@fricklerhandwerk
Copy link
Contributor

My take on this is, since it's supposed to be guarded by an experimental feature, we could merge it in whatever form, and then adapt as needed once we have a toTOML implementation. @andreabedini in any case we'd need the experimental guard.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-05-08-nix-team-meeting-minutes-53/27967/1

@andreabedini andreabedini force-pushed the toml-timestamps-as-strings branch from 3eef831 to a20c53a Compare May 22, 2023 08:02
@andreabedini
Copy link
Contributor Author

Thank you @fricklerhandwerk. I followed the rest of the code w.r.t. the experimental flag. Let me know if this is ok. I believe the CI failure is unrelated to the PR.

Currently fromTOML throws an exception when encountering a timestamp
since the nix language lacks a way to represent them.

This patch changes this beaviour and makes fromTOML parse timestamps as
attrsets of the format

  { _type = "timestamp"; value = "1979-05-27T07:32:00Z"; }
@andreabedini andreabedini force-pushed the toml-timestamps-as-strings branch from a20c53a to b258353 Compare May 25, 2023 01:58
.tag = Xp::ParseTomlTimestamps,
.name = "parse-toml-timestamps",
.description = R"(
Allow parsing of timestamps in builtins.fromTOML.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to make a suggestion to link to builtins.fromTOML documentation, but that's not even documented at all.

That made me realise that we seem to have been talking about making all of builtins.fromTOML experimental in order to have some time to figure out the roundtrip design. @nixos/nix-team is that correct? @andreabedini sorry that whatever misunderstanding was unearthed here may produce even more work for you...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem ☺️

@polykernel polykernel mentioned this pull request May 31, 2023
8 tasks
@fricklerhandwerk fricklerhandwerk self-assigned this Jun 5, 2023
@fricklerhandwerk
Copy link
Contributor

Discussed in the Nix team meeting 2023-06-05:

  • @fricklerhandwerk: shall we indeed make only the timestamp parsing experimental?
    • @Ericson2314: can we deprecate the builtin altogether?
    • @edolstra: no does not make sense write parsers as Nix language libraries
  • decision:
    • keep only the timestamp parsing experimental
    • merge

@andreabedini We'd very much welcome a follow up where timestamps are represented as structured Nix values. That would be the precondition to stabilise the feature.

@fricklerhandwerk fricklerhandwerk merged commit 3c78920 into NixOS:master Jun 9, 2023
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-06-05-nix-team-meeting-minutes-60/28933/1

@roberth
Copy link
Member

roberth commented Sep 18, 2024

An update of the TOML library has changed the returned timestamp's string representation, either to remove the T or to return the original string; needs to be looked into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. with-tests Issues related to testing. PRs with tests have some priority
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants