Skip to content

Commit

Permalink
nixos.timezone: set TZ to :/etc/localtime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wizeman committed Oct 26, 2017
1 parent 9837b40 commit cf03a4a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions nixos/modules/config/timezone.nix
Expand Up @@ -6,6 +6,8 @@ let

tzdir = "${pkgs.tzdata}/share/zoneinfo";

timeZone = "/etc/zoneinfo/${config.time.timeZone}";

in

{
Expand Down Expand Up @@ -38,17 +40,33 @@ in

config = {

environment.sessionVariables.TZDIR = "/etc/zoneinfo";
environment.sessionVariables = {
# TZ is set to prevent glibc from calling stat() every time localtime() is called,
# which can be a significant performance problem if /etc is on an NFS filesystem.
#
# We point to the /etc/localtime symlink so that we don't have to re-login to pick
# up a time zone change.
TZ = ":/etc/localtime";
TZDIR = "/etc/zoneinfo";
};

systemd.globalEnvironment = {
# See comment above about TZ.
# Here, we point to the actual time zone file, instead of the /etc/localtime
# symlink, so that systemd services are restarted automatically when the time
# zone changes.
TZ = ":${timeZone}";

# This way services are restarted when tzdata changes.
systemd.globalEnvironment.TZDIR = tzdir;
# This way services are restarted when tzdata changes.
TZDIR = tzdir;
};

systemd.services.systemd-timedated.environment = lib.optionalAttrs (config.time.timeZone != null) { NIXOS_STATIC_TIMEZONE = "1"; };

environment.etc = {
zoneinfo.source = tzdir;
} // lib.optionalAttrs (config.time.timeZone != null) {
localtime.source = "/etc/zoneinfo/${config.time.timeZone}";
localtime.source = timeZone;
localtime.mode = "direct-symlink";
};
};
Expand Down

0 comments on commit cf03a4a

Please sign in to comment.