summaryrefslogtreecommitdiff
path: root/config/networking/services/dns.nix
blob: c20c473063469fb6c3e98ad82d39ca4a7eafa737 (plain)
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
86
87
88
89
90
91
{
  config,
  lib,
  pkgs,
  ...
}:
let
  subnets = builtins.catAttrs "subnet" (
    builtins.attrValues config.personal.networking.interfaces.all
  );
in
{
  services.unbound = {
    enable = true;
    settings = {
      server = {
        module-config = ''"respip validator iterator"'';
        interface = [
          "127.0.0.1"
        ]
        ++ builtins.map ({ prefix, ... }: "${prefix}.1") subnets;
        access-control = [
          "0.0.0.0/0 refuse"
          "127.0.0.0/8 allow"
        ]
        ++ builtins.map (
          {
            prefix,
            prefixLength,
          }:
          "${prefix}.0/${builtins.toString prefixLength} allow"
        ) subnets;
        local-zone = [ ''"aristote.fr." transparent'' ];
        local-data =
          builtins.map
            (
              prefix:
              ''"${prefix}.aristote.fr. IN A ${config.personal.networking.interfaces.all.enp2s0.machines.hermaphroditos.ip}"''
            )
            [
              "dav"
              "git"
              "hermes"
              "hephaistos"
              "mesh"
              "openpgpkey"
              "quentin"
              "rss"
            ];
      };
      rpz.name = "rpz.oisd.nl";
    };
  };

  systemd.services.dns-update =
    let
      domainName = "hermes.aristote.fr";
      api = name: ip: "https://dns.eu.ovhapis.com/nic/update?system=dyndns&hostname=${name}&myip=${ip}";
      dns = "9.9.9.9";
    in
    lib.mkMerge [
      (pkgs.lib.personal.services.checkNetwork {
        hosts = [ dns ];
        restart = false;
      })
      {
        after = [ "network-online.target" ];
        wants = [ "network-online.target" ];
        path = with pkgs; [
          dig
          jq
          curl
          iproute2
        ];
        script = ''
          old=$(dig ${domainName} +short @${dns} A)
          new=$(ip -json -brief -4 address show dev ppp0 \
              | jq --raw-output '.[0].addr_info[0].local')
          if [ ! -z $old ] && [ "$new" != null ] && [ "$old" != "$new" ]
          then
            curl --max-time 5                      \
                 --location --location-trusted     \
                 --config /etc/dynamic-dns/secrets \
                 "${api domainName "$new"}"
          fi
        '';
        serviceConfig.Type = "oneshot";
        startAt = "*:0/5";
      }
    ];
}