Skip to content

Commit a0d4640

Browse files
lheckemannjoachifm
authored andcommittedJul 31, 2017
nixos/timezone: support imperative timezone configuration (#26608)
Fixes #26469.
1 parent 449946e commit a0d4640

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed
 

‎nixos/modules/config/timezone.nix

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ in
1414
time = {
1515

1616
timeZone = mkOption {
17-
default = "UTC";
18-
type = types.str;
17+
default = null;
18+
type = types.nullOr types.str;
1919
example = "America/New_York";
2020
description = ''
2121
The time zone used when displaying times and dates. See <link
2222
xlink:href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"/>
2323
for a comprehensive list of possible values for this setting.
24+
25+
If null, the timezone will default to UTC and can be set imperatively
26+
using timedatectl.
2427
'';
2528
};
2629

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

43-
environment.etc.localtime =
44-
{ source = "/etc/zoneinfo/${config.time.timeZone}";
45-
mode = "direct-symlink";
46-
};
47-
48-
environment.etc.zoneinfo.source = tzdir;
46+
systemd.services.systemd-timedated.environment = lib.optionalAttrs (config.time.timeZone != null) { NIXOS_STATIC_TIMEZONE = "1"; };
4947

48+
environment.etc = {
49+
zoneinfo.source = tzdir;
50+
} // lib.optionalAttrs (config.time.timeZone == null) {
51+
localtime.source = "/etc/zoneinfo/${config.time.timeZone}";
52+
localtime.mode = "direct-symlink";
53+
};
5054
};
5155

5256
}

‎nixos/tests/timezone.nix

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
timezone-static = import ./make-test.nix ({ pkgs, ... }: {
3+
name = "timezone-static";
4+
meta.maintainers = with pkgs.lib.maintainers; [ lheckemann ];
5+
6+
machine.time.timeZone = "Europe/Amsterdam";
7+
8+
testScript = ''
9+
$machine->waitForUnit("dbus.socket");
10+
$machine->fail("timedatectl set-timezone Asia/Tokyo");
11+
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
12+
$dateResult[1] eq "1970-01-01 01:00:00\n" or die "Timezone seems to be wrong";
13+
'';
14+
});
15+
16+
timezone-imperative = import ./make-test.nix ({ pkgs, ... }: {
17+
name = "timezone-imperative";
18+
meta.maintainers = with pkgs.lib.maintainers; [ lheckemann ];
19+
20+
machine.time.timeZone = null;
21+
22+
testScript = ''
23+
$machine->waitForUnit("dbus.socket");
24+
25+
# Should default to UTC
26+
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
27+
print $dateResult[1];
28+
$dateResult[1] eq "1970-01-01 00:00:00\n" or die "Timezone seems to be wrong";
29+
30+
$machine->succeed("timedatectl set-timezone Asia/Tokyo");
31+
32+
# Adjustment should be taken into account
33+
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
34+
print $dateResult[1];
35+
$dateResult[1] eq "1970-01-01 09:00:00\n" or die "Timezone was not adjusted";
36+
37+
# Adjustment should persist across a reboot
38+
$machine->shutdown;
39+
$machine->waitForUnit("dbus.socket");
40+
my @dateResult = $machine->execute('date -d @0 "+%Y-%m-%d %H:%M:%S"');
41+
print $dateResult[1];
42+
$dateResult[1] eq "1970-01-01 09:00:00\n" or die "Timezone adjustment was not persisted";
43+
'';
44+
});
45+
}

0 commit comments

Comments
 (0)
Please sign in to comment.