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

localtimed fails to set time zone #68489

Open
l0b0 opened this issue Sep 11, 2019 · 36 comments
Open

localtimed fails to set time zone #68489

l0b0 opened this issue Sep 11, 2019 · 36 comments
Labels
0.kind: bug Something is broken

Comments

@l0b0
Copy link
Contributor

l0b0 commented Sep 11, 2019

Describe the bug
Enabling the localtime service is not enough to make time zone setting work. After starting it I get unhelpful messages like this in the journal:

localtimed: Failed to set timezone: Europe/Zurich

The time zone never changes.

To Reproduce
Steps to reproduce the behavior:

  1. Set services.localtime.enable = true;
  2. Set time.timeZone = "UTC"; to work around systemd-timedated can't start because of missing /etc/localtime #67673
  3. sudo nixos-rebuild switch

Expected behavior

systemd-timedated should Just Work™.

Additional context

Rebooting does not help.

Removing time.timeZone = "UTC";, rebuilding and rebooting also does not help.

Contributors: @dschrempf @deviant @xaverdh @rnhmjoj @volth @worldofpeace @domenkozar @lovesegfault Is it working for any of you? Any tips on what might be different? I've tried this on a few machines a few times now.

Metadata

  • system: "x86_64-linux"
  • host os: Linux 4.19.71, NixOS, 19.03.173481.8a30e242181 (Koi)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.2.2
  • channels(root): "nixos-19.03.173481.8a30e242181"
  • channels(username): ""
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos

and

  • system: "x86_64-linux"
  • host os: Linux 4.19.71, NixOS, 19.09pre192418.e19054ab3cd (Loris)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.3
  • channels(root): "nixos-19.09pre192418.e19054ab3cd"
  • channels(username): "nixpkgs-20.03pre193309.c4196cca9ac"
  • nixpkgs: /home/username/.nix-defexpr/channels/nixpkgs
@l0b0 l0b0 added the 0.kind: bug Something is broken label Sep 11, 2019
@l0b0 l0b0 closed this as completed Oct 23, 2019
@l0b0 l0b0 reopened this Oct 23, 2019
@stale

This comment was marked as duplicate.

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

I'm having the same issue. I've been told on IRC that the only way to set the timezone is by using the time.timeZone option in /etc/nixos/configuration.nix. This does work, but it would be great to actually be able to set the timezone based on location.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 19, 2020
@stale

This comment was marked as duplicate.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 16, 2021
@afics
Copy link

afics commented Feb 18, 2021

Same issue here.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Feb 18, 2021
@stale
Copy link

stale bot commented Aug 18, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Aug 18, 2021
@enricozb
Copy link

enricozb commented Jan 15, 2022

Still happening, however, for me I get the following logs:

$ journalctl -b -u localtime
-- Journal begins at Sat 2021-10-30 15:15:44 EDT, ends at Fri 2022-01-14 21:58:35 EST. --
Jan 14 17:17:13 frame systemd[1]: Started Timezone Updater Daemon.

I need to run sudo -u localtimed localtimed for the timezone to be set. Without using the localtimed user it fails with

localtimed: Failed to set timezone: <my timezone>

However after running sudo -u localtimed localtimed, localtimed as my personal user works without issue and reports:

2022/01/14 21:59:48 Updated timezone to: <my timezone>

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 15, 2022
luisholanda added a commit to luisholanda/dotfiles that referenced this issue Jul 13, 2022
For some unknown reason ([1] and [2]), `localtimed` is not
setting the time zone correctly if no previous set value
exists.

Workaround this issue by forcing a base value, which may or
may not be updated later by the service. There isn't much
we can do other than wait for the issue to be resolved.

[1]: NixOS/nixpkgs#67673
[2]: NixOS/nixpkgs#68489

Signed-off-by: Luis Holanda <[email protected]>
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 31, 2022
@l0b0
Copy link
Contributor Author

l0b0 commented Sep 9, 2022

Not stale; still happens in 22.05. journalctl --unit=localtime.service simply says "Started Timezone Updater Daemon." but never gets anywhere. Running sudo -u localtimed localtimed just hangs seemingly forever.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Sep 9, 2022
@l0b0
Copy link
Contributor Author

l0b0 commented Oct 22, 2022

Is it possible that services.localtime.enable = true; conflicts with time.hardwareClockInLocalTime = true;? I tried removing hardwareClockInLocalTime to no avail.

@l0b0
Copy link
Contributor Author

l0b0 commented Oct 22, 2022

Also, is it possible that this only works with specific network interfaces or something? I'm only connecting to Wi-Fi.

@lovesegfault
Copy link
Member

I've been using it successfully for a while now, you obviously need geoclue to be working, but other than that it should just work.

It can fail when geoclue doesn't provide a timezone, in which case you can just manually timedatectl. I've had this happen when on airplane wifi a couple times.

@l0b0
Copy link
Contributor Author

l0b0 commented Nov 16, 2022

@lovesegfault Would you be willing to share the relevant configuration? GeoClue2 is already enabled as part of setting services.localtime.enable = true, but clearly something is still missing for it to work out of the box. I've tried it on at least three machines now several times since I reported this two years ago. See also #67673.

Relevant logs after simplifying the settings even more, applying, and rebooting:

$ journalctl --boot --unit=geoclue.service
Nov 16 20:11:33 whenua systemd[1]: Starting Location Lookup Service...
Nov 16 20:11:34 whenua systemd[1]: Started Location Lookup Service.
$ journalctl --boot --unit=localtime.service
Nov 16 20:11:33 whenua systemd[1]: Started Timezone Updater Daemon.
$ timedatectl
               Local time: Wed 2022-11-16 20:20:34 UTC
           Universal time: Wed 2022-11-16 20:20:34 UTC
                 RTC time: Wed 2022-11-16 20:20:34
                Time zone: UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

@lovesegfault
Copy link
Member

@l0b0 Out of curiosity, is this on nixos-unstable or a release channel?

@l0b0
Copy link
Contributor Author

l0b0 commented Nov 16, 2022

@lovesegfault 22.05 channel.

@lovesegfault
Copy link
Member

@l0b0 Ah, yes, the fixes were not backported, it should work on unstable, and in the upcoming 22.11 release.

There were a few PRs:

#186369
#186336
#175831
#176904
#175675

@l0b0
Copy link
Contributor Author

l0b0 commented Nov 16, 2022

@lovesegfault Thank you, that's really useful! I'll leave this open until I have a chance to test on 22.11.

@l0b0
Copy link
Contributor Author

l0b0 commented Nov 30, 2022

@lovesegfault I just applied this change on 22.11, and it did not work. The relevant services just keep restarting:

$ journalctl --follow --output=cat --unit geoclue.service
Starting Location Lookup Service...
Started Location Lookup Service.
Service not used for 60 seconds. Shutting down..
geoclue.service: Deactivated successfully.
Starting Location Lookup Service...
Started Location Lookup Service.
Service not used for 60 seconds. Shutting down..
geoclue.service: Deactivated successfully.
Starting Location Lookup Service...
Started Location Lookup Service.
$ journalctl --follow --output=cat --unit localtimed-geoclue-agent.service
Starting localtimed-geoclue-agent.service...
Started localtimed-geoclue-agent.service.
Geolocation service not in use
Geolocation service not in use
[…]

@krav
Copy link
Contributor

krav commented Jan 6, 2023

On unstable this seems to conflict with having time.timeZone set. This disables setting timezone through timedatectl and localtimed reports "Failed to set timezone: ".

Removing time.timeZone fixes localtimed.

@l0b0
Copy link
Contributor Author

l0b0 commented Jan 8, 2023

On unstable this seems to conflict with having time.timeZone set. This disables setting timezone through timedatectl and localtimed reports "Failed to set timezone: ".

Removing time.timeZone fixes localtimed.

I'm not sure what you mean here. As you can see from my last comment I no longer have time.timeZone set, and I don't see "Failed to set timezone" anymore, but localtimed still doesn't do what it's supposed to. Did you mean to say that there are some new patches on unstable which have fixed the remaining issues?

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/localtimed-broken-since-2019/25039/1

@mimame
Copy link
Member

mimame commented Jan 29, 2023

Hi @l0b0
I think you are missing in your config:

  services.localtimed.enable = true;
  services.automatic-timezoned.enable = true;
  location.provider = "geoclue2";

@l0b0
Copy link
Contributor Author

l0b0 commented Jan 29, 2023

@mimame

services.localtime.enable = true; is equivalent to services.localtimed.enable = true;. (Presumably services.localtime is about to be deprecated for services.localtimed, so I've changed that; thanks!)

Based on the documentation it looks like localtimed and automatic-timezoned should be mutually exclusive, because both of them say they "[keep] the system timezone up-to-date based on the current location".

Both of them set up geoclue2, so I don't understand why location.provider = "geoclue2"; should be necessary. Also, localtimed and automatic-timezoned are setting up GeoClue2 in rather different ways, which should maybe be consolidated.

As a side note, all location.provider = "geoclue2"; does is set services.geoclue2.enable = true;, so I don't know why location.provider exists.

@l0b0
Copy link
Contributor Author

l0b0 commented Jan 30, 2023

I tried switching to automatic-timezoned (automatic-timezoned.enable = true; instead of localtime.enable = true;) with similar results:

$ journalctl --output=cat --follow 
Service not used for 60 seconds. Shutting down..
geoclue.service: Deactivated successfully.
Error: MethodError(OwnedErrorName(ErrorName(Str(Owned("org.freedesktop.DBus.Error.NoReply")))), Some("Message recipient disconnected from message bus without replying"), Msg { type: Error, sender: UniqueName(Str(Borrowed("org.freedesktop.DBus"))), reply-serial: 2, body: Signature("s") })
automatic-timezoned.service: Main process exited, code=exited, status=1/FAILURE
automatic-timezoned.service: Failed with result 'exit-code'.

Setting location.provider = "geoclue2"; in addition didn't seem to change anything.

@Enzime
Copy link
Member

Enzime commented Mar 26, 2023

I fixed my issues with automatic-timezoned (automatic-timezoned.enable = true;) by re-enabling the Geoclue2 demo agent:

{
  services.geoclue2.enableDemoAgent = lib.mkForce true;
}

Enabling GNOME or Pantheon disables the demo agent and removes it from the agent whitelist which is necessary for the NixOS automatic-timezoned service.

@l0b0
Copy link
Contributor Author

l0b0 commented Jun 1, 2023

@Enzime Can confirm your workaround on release-23.05, on two separate machines. @7c6f434c @mimame Would either of you be willing to look into this?

@7c6f434c
Copy link
Member

7c6f434c commented Jun 2, 2023

I don't have any NixOS environment around (nor do I have any DE) to try this easily… (I have only ever cared about geoclue as a library dependency of a few things, I am not even sure I have ever checked whether the data coming from it is reasonable)

@nosewings
Copy link
Contributor

nosewings commented Jul 31, 2023

I think I've at least partially figured out what's causing the "Failed to set timezone" error. It seems to be a permission error, or at least related to one. localtimed is being run as user localtimed, but it needs to be running as root. To confirm, the error message is coming from this file, when making a dbus call to org.freedesktop.timedate1.SetTimezone. You can emulate this call via the following line:

dbus-send --system --print-reply --type=method_call --dest='org.freedesktop.timedate1' '/org/freedesktop/timedate1' 'org.freedesktop.timedate1.SetTimezone' string:"America/Chicago" boolean:false

When run as root, this call succeeds for me. But if I run it as localtimed (via sudo -u localtimed), I get a permission error.

Weirdly, though, this only happens on one of my systems. On the other one, running the command as localtimed works just fine.

@99linesofcode
Copy link

99linesofcode commented Aug 29, 2024

While there are a few issues with the localtimed module (ie. #329522) the main problem here and in the automatic-timezoned module is related to #321121.

Finding an alternative to MLS could take a while so for those looking for a workaround and willing to incur some costs associated with the Geolocation API, override services.geoclue2.geoProviderUrl with your own Google Maps API key like so:

services.geoclue2.geoProviderUrl = "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_KEY"

edit: clarified that there are costs associated with using the Geolocation API

@99linesofcode
Copy link

99linesofcode commented Aug 29, 2024

While there are a few issues with the localtimed module (ie. #329522) the main problem here and in the automatic-timezoned module is related to #321121.

Finding an alternative to MLS could take a while so for those looking for a workaround and willing to incur some costs associated with the Geolocation API, override services.geoclue2.geoProviderUrl with your own Google Maps API key like so:

services.geoclue2.geoProviderUrl = "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_KEY"

edit: clarified that there are costs associated with using the Geolocation API

Decided I'd rather not incur any costs either. The Archlinux Wiki mentioned a couple of providers here: https://wiki.archlinux.org/title/System_time#Setting_based_on_geolocation. Seems like all of them are commercial parties. Most offer free access either with or without creating an account and obtaining an API key.

edit: https://beacondb.net seems like a worthy FOSS alternative

@peterhoeg
Copy link
Member

FWIW, I've been using beacondb for a while with the following config:

{
  services.geoclue2.geoProviderUrl = "https://beacondb.net/v1/geolocate";
}

@99linesofcode
Copy link

FWIW, I've been using beacondb for a while with the following config:

{
  services.geoclue2.geoProviderUrl = "https://beacondb.net/v1/geolocate";
}

Yeah that's what I settled on yesterday as well. It's important to note however that beacondb is still in it's infancy and the developer is working on scaling it. This URL actually points to his home server, not the VPS he is in the process of setting up.

See: https://codeberg.org/beacondb/beacondb/issues/5#issuecomment-2182940 for more info on this.

@yajo
Copy link
Contributor

yajo commented Oct 3, 2024

Probably the problem is that Mozilla shut down their geolocation service. But we use it:

default = "https://location.services.mozilla.com/v1/geolocate?key=geoclue";

I found this alternative:

{
  services.tzupdate.enable = true;
}

It queries many endpoints, so it seems to work here.

@l0b0
Copy link
Contributor Author

l0b0 commented Oct 3, 2024

It queries many endpoints, so it seems to work here.

Reference

@Jasper-Ben
Copy link

@99linesofcode, I am trying to consolidate the (for me) relevant information in this thread on using geoclue with beacondb. I think I have it correctly set up and the only reason why it is currently not working for me is that my area is currently not covered by the DB (I started uploading reports already) but I am not 100% sure. Also I am not sure whether I can still set a default timeZone with automatic-timezoned enabled. Maybe you (or someone else) could quickly confirm?

My config:

  # Set default timeZone to home time
  time.timeZone = lib.mkDefault "Europe/Berlin";
  # Enable automatic timezone updates 
  services.automatic-timezoned.enable = true;
  # Force enable required geoclue2 DemoAgent, since GNOME disables it: https://github.com/NixOS/nixpkgs/issues/68489#issuecomment-1484030107
  services.geoclue2.enableDemoAgent = lib.mkForce true;
  # Use beacondb.net since Mozilla Location Service is retired: https://github.com/NixOS/nixpkgs/issues/321121
  services.geoclue2.geoProviderUrl = "https://beacondb.net/v1/geolocate";

The log output from geoclue:

sudo journalctl -u geoclue --follow
Okt 24 13:34:17 2693-nixos systemd[1]: Stopped Location Lookup Service.
Okt 24 13:34:17 2693-nixos systemd[1]: geoclue.service: Consumed 691ms CPU time, 38.5M memory peak, 0B memory swap peak, no IO, received 5.6K IP traffic, sent 5.3K IP traffic.
Okt 24 13:34:17 2693-nixos systemd[1]: Starting Location Lookup Service...
Okt 24 13:34:17 2693-nixos .geoclue-wrappe[16924]: Failed to open /etc/geoclue/conf.d/: Error opening directory “/etc/geoclue/conf.d/”: No such file or directory
Okt 24 13:34:17 2693-nixos systemd[1]: Started Location Lookup Service.
Okt 24 13:34:17 2693-nixos .geoclue-wrappe[16924]: Failed to query location: No WiFi networks found
Okt 24 13:34:21 2693-nixos .geoclue-wrappe[16924]: Failed to query location: Query location SOUP error: Not Found
Okt 24 13:40:23 2693-nixos .geoclue-wrappe[16924]: Service not used for 60 seconds. Shutting down..
Okt 24 13:40:23 2693-nixos systemd[1]: geoclue.service: Deactivated successfully.
Okt 24 13:40:23 2693-nixos systemd[1]: geoclue.service: Consumed 613ms CPU time, 38.5M memory peak, 0B memory swap peak, no IO, received 5.7K IP traffic, sent 5.5K IP traffic.

@99linesofcode
Copy link

@Jasper-Ben hi, since last reporting on this I've switched time zones and haven't been able to get it working either. I haven't spent much time debugging this yet. I've resorted to manually updating my timezone through timedatectl for the time being:

sudo timedatectl set-timezone "Europe/Berlin" in your case.

@99linesofcode
Copy link

@Jasper-Ben services.tzupdate.enable = true seems to work for me. I'm on the following commits:

image

@yajo
Copy link
Contributor

yajo commented Dec 3, 2024

See #361373 for making tzupdate actually work out of the box when enabled.

RafDevX added a commit to RafDevX/dotfiles that referenced this issue Dec 15, 2024
The demo agent must be manually enabled because Gnome disables it.

See: NixOS/nixpkgs#68489 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken
Projects
None yet
Development

No branches or pull requests