summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/iana-etc.nix
blob: f6d47f27e3267f6852cc06816df091bd11434d5b (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
with import <stockholm/lib>;
{ config, pkgs, ... }: {

  options.krebs.iana-etc.services = mkOption {
    default = {};
    type = types.attrsOf (types.submodule ({ config, ... }: {
      options = {
        port = mkOption {
          default = config._module.args.name;
          type = types.addCheck types.str (test "[1-9][0-9]*");
        };
      } // genAttrs ["tcp" "udp"] (protocol: mkOption {
        default = null;
        type = types.nullOr (types.submodule {
          options = {
            name = mkOption {
              type = types.str;
            };
          };
        });
      });
    }));
  };

  config.environment.etc = mkIf (config.krebs.iana-etc.services != {})  {
    services.source = mkForce (pkgs.runCommand "krebs-iana-etc" {} ''
      exec < ${pkgs.iana_etc}/etc/services
      exec > $out
      awk -F '[ /]+' '
        BEGIN {
          port=0
        }
        ${concatMapStringsSep "\n" (entry: ''
          $2 == ${entry.port} {
            port=$2
            next
          }
          port == ${entry.port} {
            ${concatMapStringsSep "\n"
              (proto: let
                s = "${entry.${proto}.name} ${entry.port}/${proto}";
              in
                "print ${toJSON s}")
              (filter (proto: entry.${proto} != null) ["tcp" "udp"])}
            port=0
          }
        '') (attrValues config.krebs.iana-etc.services)}
        {
          print $0
        }
      '
    '');
  };

}