about summary refs log tree commit diff
path: root/nixos/modules/services/networking/oink.nix
blob: 3497ca9220a8032d510997d99d0cd5e0af9e377a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.oink;
  makeOinkConfig = attrs: (pkgs.formats.json { }).generate
    "oink.json" (mapAttrs' (k: v: nameValuePair (toLower k) v) attrs);
  oinkConfig = makeOinkConfig {
    global = cfg.settings;
    domains = cfg.domains;
  };
in
{
  options.services.oink = {
    enable = mkEnableOption "Oink, a dynamic DNS client for Porkbun";
    package = mkPackageOption pkgs "oink" { };
    settings = {
      apiKey = mkOption {
        type = types.str;
        description = "API key to use when modifying DNS records.";
      };
      secretApiKey = mkOption {
        type = types.str;
        description = "Secret API key to use when modifying DNS records.";
      };
      interval = mkOption {
        # https://github.com/rlado/oink/blob/v1.1.1/src/main.go#L364
        type = types.ints.between 60 172800; # 48 hours
        default = 900;
        description = "Seconds to wait before sending another request.";
      };
      ttl = mkOption {
        type = types.ints.between 600 172800;
        default = 600;
        description = ''
          The TTL ("Time to Live") value to set for your DNS records.

          The TTL controls how long in seconds your records will be cached
          for. A smaller value will allow the record to update quicker.
        '';
      };
    };
    domains = mkOption {
      type = with types; listOf (attrsOf anything);
      default = [];
      example = [
        {
          domain = "nixos.org";
          subdomain = "";
          ttl = 1200;
        }
        {
          domain = "nixos.org";
          subdomain = "hydra";
        }
      ];
      description = ''
        List of attribute sets containing configuration for each domain.

        Each attribute set must have two attributes, one named *domain*
        and another named *subdomain*. The domain attribute must specify
        the root domain that you want to configure, and the subdomain
        attribute must specify its subdomain if any. If you want to
        configure the root domain rather than a subdomain, leave the
        subdomain attribute as an empty string.

        Additionally, you can use attributes from *services.oink.settings*
        to override settings per-domain.

        Every domain listed here *must* have API access enabled in
        Porkbun's control panel.
      '';
    };
  };

  config = mkIf cfg.enable {
    systemd.services.oink = {
      description = "Dynamic DNS client for Porkbun";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      script = "${cfg.package}/bin/oink -c ${oinkConfig}";
    };
  };
}