It’s a bit cumbersome this way, so I’m wondering if this would be possible purely in Nix? These are what I found thus far:
builtins.currentTime
Unfortunately, it returns Unix Epoch time in seconds only (same as date +%s), and couldn’t find a way to convert it within Nix.
[Discourse] Adding date/time to a config option
Don’t understand the examples there yet, but based on what little I know I don’t think runCommand would be useful for me. (Or is it but I can’t see it yet?)
[Discourse] Run shell commands in nix expressions
My first thought was to call date from within the Nix expression, but apparently this is not really an option. (I couldn’t even find the documentation for builtins.exec, probably for a good reason.)
The value of builtins.currentTime will be put into an attribute set (env) whose attributes will be exported as environment variables when the derivation is built in its own Bash shell.
The build command for the derivation in this case simply converts builtins.currentTime from Unix time to a date, and saves it into the output file.
lib.readFile triggers the build of the derivation (the result of pkgs.runCommand), and reads the output file’s contents.
Notes
env.when → when
As soon as I used this Nix expression in a Nix shell expression, error: cannot coerce a set to a string was thrown with the cursor pointing to env.when, and removing env. did the trick. Can’t say why the env.when version works in nix repl but not in a Nix shell expression though.
The form I’m using for now is in the next item below.
Couldn’t find the documentation for lib.readFile in Nixpkgs lib section of the Nixpkgs manual so tried replacing it with builtins.readFile and it worked.
Also couldn’t find it in the manual, but apparently if a Nix derivation (i.e., a drv file) is read, it will be built first.