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

Add builtins.dateFormat function #8163

Open
doronbehar opened this issue Apr 4, 2023 · 10 comments
Open

Add builtins.dateFormat function #8163

doronbehar opened this issue Apr 4, 2023 · 10 comments
Labels
feature Feature request or proposal flakes

Comments

@doronbehar
Copy link
Contributor

Is your feature request related to a problem? Please describe.

When composing a flake.nix, often we need to build a dependency from a flake input that has flake = false;. Defining a Nix expression for such a package requires setting pname and version, as always. If one fetches a non release version of this flake input, you may want to set the version to unstable-<last-commit-date>, as we do in Nixpkgs. It's natural to take this date from the lastModified json key of the input in flake.lock. However, this json key is an epoch number, and not human readable.

Describe the solution you'd like
I think ideally, I could use a pure Nix function as so:

lockFile = builtins.fromJSON (builtins.readFile ./flake.lock);
version = builtins.dateFormat lockFile.nodes.${myDep}.locked.lastModified;

Describe alternatives you've considered

Get this date via:

epochDate2human = epochDate: builtins.readFile (pkgs.runCommand "epochDateConvertion" {} ''
  printf '%(%F)T' ${builtins.toString epochDate} > $out
'');

But this is of course IFD, which is forbidden in restricted mode, and when evaluating packages for commands like nix search.

@doronbehar doronbehar added the feature Feature request or proposal label Apr 4, 2023
@roberth roberth added the flakes label Apr 9, 2023
@roberth
Copy link
Member

roberth commented Apr 9, 2023

Perhaps we should return the whole sourceInfo instead of just a path when flake = false.

@doronbehar
Copy link
Contributor Author

Cannot find documentation for sourceInfo... Will that help me regarding reading flake.lock? or also include a kind of date attribute?

@roberth
Copy link
Member

roberth commented Apr 10, 2023

It's an attribute of the flake = true flake inputs containing the source path and limited metadata like lastModified. Basically a fetchTree result iiuc.

@Kranzes
Copy link
Member

Kranzes commented Apr 21, 2023

You can also do something like this for now builtins.concatStringsSep "-" (builtins.match "(.{4})(.{2})(.{2}).*" inputs.self.lastModifiedDate)

@doronbehar
Copy link
Contributor Author

You can also do something like this for now builtins.concatStringsSep "-" (builtins.match "(.{4})(.{2})(.{2}).*" inputs.self.lastModifiedDate)

Hmm not bad, though not very explanatory:

"1549-46-64"

@roberth
Copy link
Member

roberth commented Apr 21, 2023

Another time related "issue" is

We might say that time logic is creeping in and we need a better story for it. However, time handling libraries are notoriously hard to implement, suggesting that we'd be better off pinning a nix library such as the Nixpkgs lib, so that we don't risk tying reproducibility to our inevitable time handling bugs.

@roberth
Copy link
Member

roberth commented Apr 21, 2023

"1549-46-64"

lastModified or lastModifiedDate?

@doronbehar
Copy link
Contributor Author

"1549-46-64"

lastModified or lastModifiedDate?

Oh I see, it's not the lastModified json key from flake.lock - it's a key of flake input argument. Thanks for noticing, indeed it works! Thanks a lot @Kranzes 💚 .

@Kranzes
Copy link
Member

Kranzes commented Apr 21, 2023

lastModified/lastModifiedDate/rev/shortRev are just coming from an input's sourceInfo attr

@balsoft
Copy link
Member

balsoft commented May 12, 2023

Here's my take on this in pure Nix: https://github.com/balsoft/Flockenzeit

  version = with flockenzeit.lib.splitSecondsSinceEpoch {} lastModified; "unstable-${F}"; # %F from `man date'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request or proposal flakes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants