summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorLinus Heckemann <git@sphalerite.org>2017-07-31 15:55:24 +0100
committerJoachim F <joachifm@users.noreply.github.com>2017-07-31 15:55:24 +0100
commita0d464033c2bacffa9c05d27903fb03cd09ace46 (patch)
tree65344218179a86b9b244e2c6760a87bc1ea6a721 /nixos
parent449946ef94edbffb352bf7bcac934af3b2cebf57 (diff)
downloadnixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar.gz
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar.bz2
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar.lz
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar.xz
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.tar.zst
nixlib-a0d464033c2bacffa9c05d27903fb03cd09ace46.zip
nixos/timezone: support imperative timezone configuration (#26608)
Fixes #26469.
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/config/timezone.nix20
-rw-r--r--nixos/tests/timezone.nix45
2 files changed, 57 insertions, 8 deletions
diff --git a/nixos/modules/config/timezone.nix b/nixos/modules/config/timezone.nix
index 39a45042c6cc..9f849c24c113 100644
--- a/nixos/modules/config/timezone.nix
+++ b/nixos/modules/config/timezone.nix
@@ -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.
         '';
       };
 
@@ -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";
+      };
   };
 
 }
diff --git a/nixos/tests/timezone.nix b/nixos/tests/timezone.nix
new file mode 100644
index 000000000000..2204649a3fc4
--- /dev/null
+++ b/nixos/tests/timezone.nix
@@ -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";
+    '';
+  });
+}