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

nixos.timezone: set TZ to :/etc/localtime #23078

Closed
wants to merge 1 commit into from

Conversation

wizeman
Copy link
Member

@wizeman wizeman commented Feb 22, 2017

Motivation for this change

This prevents glibc from calling stat() every time localtime() is called.
This could be a significant performance problem if /etc is on an NFS filesystem.

For systemd services, we point TZ to the actual time zone file instead of the /etc/localtime symlink so that systemd services are restarted if the time zone is changed.

Fixes #23053

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@mention-bot
Copy link

@wizeman, thanks for your PR! By analyzing the history of the files in this pull request, we identified @edolstra, @cpages and @abbradar to be potential reviewers.

@wizeman
Copy link
Member Author

wizeman commented Feb 22, 2017

Please wait just a few minutes as I'm about to push a better version of this PR.

This prevents glibc from calling stat() every time localtime() is
called.
This could be a significant performance problem if /etc is on an NFS
filesystem.

For systemd services, we point TZ to the actual time zone file instead
of the /etc/localtime symlink so that systemd services are restarted
if the time zone is changed.

Fixes NixOS#23053
@wizeman
Copy link
Member Author

wizeman commented Feb 22, 2017

Ok, with this updated version, systemd services are now restarted if the time zone changes.

I don't know if it still makes sense to keep the TZDIR variable now that TZ is defined. I was hoping that someone who better understands the implications of removing TZDIR would comment.

@wizeman
Copy link
Member Author

wizeman commented Feb 22, 2017

Ah, I believe TZDIR still needs to be set, because someone (either in their user environment or in a systemd service) may override TZ to be something like Europe/London, and in that case glibc must able to locate the time zone data correctly in NixOS, rather than assume the time zone data is in /usr/share (the default). This is issue #2447.

So I think we should keep the TZDIR definition (which this PR currently does).

@samueldr
Copy link
Member

Anything blocking this from either an in-extremis merge for 18.09 or an early merge for 19.03?

I'm don't have the required knowledge about glibc to confirm the speed assertions, neither the setup to test, but reading the appropriate manual section confirms this approach seems implemented right

The third format looks like this:

  • :characters

Each operating system interprets this format differently; in the GNU C Library, characters is the name of a file which describes the time zone.

If the TZ environment variable does not have a value, the operation chooses a time zone by default. In the GNU C Library, the default time zone is like the specification ‘TZ=:/etc/localtime’ (or ‘TZ=:/usr/local/etc/localtime’, depending on how the GNU C Library was configured; see Installation). Other C libraries use their own rule for choosing the default time zone, so there is little we can say about them.

@edolstra
Copy link
Member

Looks good to me.

@samueldr
Copy link
Member

Rebased locally to test.

It breaks the timezone-imperative test, which works on the previous commit (current nixpkgs-unstable).

error: cannot coerce null to a string, at /.../nixpkgs-PR23078/nixos/modules/config/timezone.nix:12:15
(use '--show-trace' to show detailed location information)

This comes from the previous change where timeZone now default to null instead of "UTC".


I have added a WIP commit I'd like reviewed; the tests pass, and it seems semantically correct, but I'm not entirely confident (but pretty sure) it implements the change while not breaking the current assumptions.

cc @wizeman @vcunat @edolstra

@wizeman
Copy link
Member Author

wizeman commented Aug 28, 2018

@samueldr: Looks good to me!

@samueldr
Copy link
Member

Pushed on @wizeman's repo after the approval; fixing the merge conflict, now ready to merge.

@rasendubi
Copy link
Member

(triage) @wizeman @samueldr anyone willing to fix the merge conflict?

@mmahut
Copy link
Member

mmahut commented Aug 12, 2019

Are there any updates on this pull request, please?

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@wizeman
Copy link
Member Author

wizeman commented Jun 2, 2020

I think I must have force-pushed my git branch by mistake and undid @samueldr's merge conflict fixes...

Note that while I was using these changes, I was running into a bug where Go programs couldn't parse the TZ=:/etc/localtime environment variable correctly and defaulted to the UTC timezone.

Perhaps golang/go#27570 would fix that (but I'm not sure).

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 2, 2020
@flokli
Copy link
Contributor

flokli commented Jun 18, 2020

I'm don't think we can do this, at least until this isn't supported in major language runtimes (like Go).

@omasanori
Copy link
Contributor

The commit golang/go@58fe2cd was landed, so Go 1.16+ will be ready for this change.

@flokli
Copy link
Contributor

flokli commented Oct 12, 2020

Cool! Did someone check if python supports this, too?

Edit: It seems at least pytz does.

@omasanori
Copy link
Contributor

@flokli It appearently works with Python 3.7 on NixOS 20.03:

[nix-shell:~]$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 24 Oct 12 06:33 /etc/localtime -> /etc/zoneinfo/Asia/Tokyo

[nix-shell:~]$ TZ=UTC python
Python 3.7.6 (default, Dec 18 2019, 19:23:55)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import time
>>> os.environ['TZ']
'UTC'
>>> time.tzset()
>>> time.tzname
('UTC', 'UTC')
>>> time.strftime('%X %x %Z')
'12:08:22 10/12/20 UTC'
>>> os.environ['TZ'] = ':/etc/localtime'
>>> time.tzset()
>>> time.tzname
('JST', 'JST')
>>> time.strftime('%X %x %Z')
'21:08:45 10/12/20 JST'
>>>

(JST: Japan Standard Time; same as Asia/Tokyo and its offset from UTC is +09:00)

@SuperSandro2000
Copy link
Member

@wizeman Can you rebase this?

@Ekleog
Copy link
Member

Ekleog commented Jan 23, 2021

@wizeman Are you still interested in pushing this through the last mile? :)

@omasanori
Copy link
Contributor

Here is a good news: Go 1.16 is released 🎉
I can take over if @wizeman @samueldr won't have enough time to work on this PR in near future.

@wizeman
Copy link
Member Author

wizeman commented Feb 17, 2021

@Ekleog I'm interested but I don't have enough time at the moment.

@omasanori Please, feel free to take over. Thanks!

@omasanori
Copy link
Contributor

@wizeman I see, Thank you for prompt response!

@Mic92
Copy link
Member

Mic92 commented Feb 22, 2021

newer version in #113555

@Mic92 Mic92 closed this Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set TZ environment variable?