ctucx.git: dns.nix

fork of https://github.com/kirelagin/dns.nix

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 
#
# SPDX-FileCopyrightText: 2019 Kirill Elagin <https://kir.elagin.me/>
# SPDX-FileCopyrightText: 2021 Naïm Favier <n@monade.li>
#
# SPDX-License-Identifier: MPL-2.0 or MIT
#

{ lib }:

let
  inherit (lib) isString mkOption types;

  recordType = rsubt:
    let
      submodule = types.submodule {
        options = {
          class = mkOption {
            type = types.enum [ "IN" ];
            default = "IN";
            example = "IN";
            description = "Resource record class. Only IN is supported";
          };
          ttl = mkOption {
            type = types.nullOr types.ints.unsigned;  # TODO: u32
            default = null;
            example = 300;
            description = "Record caching duration (in seconds)";
          };
        } // rsubt.options;
      };
    in
      (if rsubt ? fromString then types.either types.str else lib.id) submodule;

  writeRecord = name: rsubt: data:
    let
      data' =
        if isString data && rsubt ? fromString then
          # add default values for the record type
          (recordType rsubt).merge [] [ { file = ""; value = rsubt.fromString data; } ]
        else data;
      name' = rsubt.nameFixup or (n: _: n) name data';
      rtype = rsubt.rtype;
    in lib.concatStringsSep " " (with data'; [
        "${name'}."
      ] ++ lib.optionals (ttl != null) [
        (toString ttl)
      ] ++ [
        class
        rtype
        (rsubt.dataToString data')
      ]);

in

{
  inherit recordType;
  inherit writeRecord;
}