Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions nixos/modules/config/timezone.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ in
time = {

timeZone = mkOption {
default = "UTC";
type = types.str;
default = null;
type = types.nullOr types.str;
example = "America/New_York";
description = ''
The time zone used when displaying times and dates. See <link
xlink:href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"/>
for a comprehensive list of possible values for this setting.

If null, the timezone will default to UTC and can be set imperatively
using timedatectl.
'';
};

Expand All @@ -40,13 +43,14 @@ in
# This way services are restarted when tzdata changes.
systemd.globalEnvironment.TZDIR = tzdir;

environment.etc.localtime =
{ source = "/etc/zoneinfo/${config.time.timeZone}";
mode = "direct-symlink";
};

environment.etc.zoneinfo.source = 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.mode = "direct-symlink";
};
};

}
45 changes: 45 additions & 0 deletions nixos/tests/timezone.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
timezone-static = import ./make-test.nix ({ pkgs, ... }: {
name = "timezone-static";
meta.maintainers = with pkgs.lib.maintainers; [ lheckemann ];

machine.time.timeZone = "Europe/Amsterdam";

testScript = ''
$machine->waitForUnit("dbus.socket");
$machine->fail("timedatectl set-timezone Asia/Tokyo");
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
$dateResult[1] eq "1970-01-01 01:00:00\n" or die "Timezone seems to be wrong";
'';
});

timezone-imperative = import ./make-test.nix ({ pkgs, ... }: {
name = "timezone-imperative";
meta.maintainers = with pkgs.lib.maintainers; [ lheckemann ];

machine.time.timeZone = null;

testScript = ''
$machine->waitForUnit("dbus.socket");

# Should default to UTC
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
print $dateResult[1];
$dateResult[1] eq "1970-01-01 00:00:00\n" or die "Timezone seems to be wrong";

$machine->succeed("timedatectl set-timezone Asia/Tokyo");

# Adjustment should be taken into account
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
print $dateResult[1];
$dateResult[1] eq "1970-01-01 09:00:00\n" or die "Timezone was not adjusted";

# Adjustment should persist across a reboot
$machine->shutdown;
$machine->waitForUnit("dbus.socket");
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
print $dateResult[1];
$dateResult[1] eq "1970-01-01 09:00:00\n" or die "Timezone adjustment was not persisted";
'';
});
}