Dynamic-DNS Systemd Timer

Hello

I have an installation of Nixos that I am trying to add a Systemd timer to update my dynamic-dns on an hourly basis. Below is what I have gotten so far:

  systemd.services = {
    dynamic-dns-updater = {
      path = [
        pkgs.curl
      ];
      script = "curl https://freedns.afraid.org/dynamic/update.php?token";
      startAt = "hourly";
    };

This seems to work, but there are two things I don’t seem smart enough to be able to figure out on my own.

  1. How do I get this setup to run as a non-root user? Or am I misunderstanding something and this is already running as a non-root user.

  2. How do I add a randomized delay?

Thank you

  1. If you don’t specify a user it will run as root, you need to set:
systemd.services.dynamic-dns-updater = {
  serviceConfig.User = "username";
};
  1. Uhm, it seems there is a way add a random delay, but I’ve never used it. Try adding something like this:
systemd.timers.dynamic-dns-updater = {
  timerConfig.RandomizedDelaySec = 30;
};
3 Likes

The randomised delay thing gets used by a number of nixos things, one example that comes to mind is the nix.gc.* service, and it works there

That worked, below is my final configuration

Thank you for your help

  #Dynamic-DNS
  users.users.dynamicdns = {
     isSystemUser = true;
     group = "dynamicdns";
  };
  users.groups.dynamicdns = {};
  systemd.services.dynamic-dns-updater = {
      serviceConfig.User = "dynamicdns";
      path = [
        pkgs.curl
      ];
      script = "curl https://freedns.afraid.org/dynamic/update.php?token";
      startAt = "hourly";   
  };
  systemd.timers.dynamic-dns-updater.timerConfig.RandomizedDelaySec = "15m";

Thank you

2 Likes

For people having their own dns updater, one can also use nsupdate: https://github.com/Mic92/dotfiles/blob/7c2127a12461d1607ecc3cf33eec47b0a5803116/nixos/modules/ip-update.nix https://github.com/Mic92/dotfiles/blob/7c2127a12461d1607ecc3cf33eec47b0a5803116/nixos/eve/modules/knot/default.nix#L130