nixpkgs-suyu/nixos/modules/services/networking/ntpd.nix

99 lines
1.9 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
inherit (pkgs) ntp;
2015-09-10 10:56:18 +02:00
cfg = config.services.ntp;
stateDir = "/var/lib/ntp";
ntpUser = "ntp";
configFile = pkgs.writeText "ntp.conf" ''
driftfile ${stateDir}/ntp.drift
2014-03-06 11:54:02 +01:00
restrict 127.0.0.1
restrict -6 ::1
2015-09-10 10:56:18 +02:00
${toString (map (server: "server " + server + " iburst\n") cfg.servers)}
'';
2015-09-10 10:56:18 +02:00
ntpFlags = "-c ${configFile} -u ${ntpUser}:nogroup ${toString cfg.extraFlags}";
in
{
###### interface
options = {
services.ntp = {
enable = mkOption {
default = false;
description = ''
Whether to synchronise your machine's time using the NTP
protocol.
'';
};
servers = mkOption {
default = config.networking.timeServers;
description = ''
The set of NTP servers from which to synchronise.
'';
};
2015-09-10 10:56:18 +02:00
extraFlags = mkOption {
type = types.listOf types.str;
description = "Extra flags passed to the ntpd command.";
default = [];
};
};
};
###### implementation
config = mkIf config.services.ntp.enable {
# Make tools such as ntpq available in the system path.
environment.systemPackages = [ pkgs.ntp ];
services.timesyncd.enable = mkForce false;
users.users = singleton
{ name = ntpUser;
uid = config.ids.uids.ntp;
description = "NTP daemon user";
home = stateDir;
};
systemd.services.ntpd =
2013-01-10 13:59:41 +01:00
{ description = "NTP Daemon";
2014-11-26 20:19:31 +01:00
wantedBy = [ "multi-user.target" ];
wants = [ "time-sync.target" ];
before = [ "time-sync.target" ];
preStart =
''
mkdir -m 0755 -p ${stateDir}
chown ${ntpUser} ${stateDir}
'';
serviceConfig = {
ExecStart = "@${ntp}/bin/ntpd ntpd -g ${ntpFlags}";
Type = "forking";
};
};
};
}