TL;DR: is it possible to use the ntp server provided by DHCP to network manager?
Context:
By default nixos sets the time using some ntp servers, like 0.nixos.pool.ntp.org
… However in many universities a firewall blocks the ntp protocol on these servers:
$ nix-shell -p ntp
$ ntpdate -q 0.nixos.pool.ntp.org
22 Nov 17:06:57 ntpdate[4020886]: no server suitable for synchronization found
Therefore the computer cannot get a proper time. Instead, the university provides their own ntp server, that is advertised during the DHCP connection. We can get/check the ntp provided to network manager as follows:
$ nmcli connection show 'cwi-gast' | grep ntp
DHCP4.OPTION[8]: ntp_servers = 192.168.x.y
DHCP4.OPTION[18]: requested_ntp_servers = 1
$ ntpdate -q 192.168.x.y
server 192.168.x.y, stratum 3, offset +2.493988, delay 0.02779
22 Nov 16:51:37 ntpdate[3923570]: step time server 192.168.128.1 offset +2.493988 sec
It is possible to use:
networking.timeServers = options.networking.timeServers.default ++ [ "ntp.example.com" ];
to add a given ntp… but this need to be done on every network I’m going to. Since network managers is aware of the right ntp to use, wouldn’t it be possible to allow the systemd-timesyncd
service to also get the address of the ntp server from DHCP/network manager?
EDIT
This wiki suggests that it’s impossible without using a trick involving a dispatcher restarting and reconfiguring systemd-timesyncd
… So it seems like a limitation of Network manager. If I were using networkd
instead it seems to be possible to configure it using:
systemd.network.config = {
dhcpV4Config = { UseNTP = true; };
dhcpV6Config = { UseNTP = true; };
}
even if it defaults to true already.
I’m not sure if the default dispatcher would work as-it on NixOs. I can see a dispatcher option for NM, but the above code will reconfigure/restart systemd-timesyncd
on every new connection. The good thing it that it writes it in a different file for each connection so it may not collide with the timesyncd
… It could work, I need to try it out! At least it’s good to know that it’s not a NixOs bug.