1{
2 pkgs,
3 lib,
4 config,
5 ...
6}:
7let
8 cfg = config.networking.getaddrinfo;
9
10 formatTableEntries =
11 tableName: table:
12 if table == null then
13 [ ]
14 else
15 lib.mapAttrsToList (cidr: val: "${tableName} ${cidr} ${toString val}") table;
16
17 gaiConfText = lib.concatStringsSep "\n" (
18 [
19 "# Generated by NixOS module networking.getaddrinfo"
20 "# Do not edit manually!"
21 "reload ${if cfg.reload then "yes" else "no"}"
22 ]
23 ++ formatTableEntries "label" cfg.label
24 ++ formatTableEntries "precedence" cfg.precedence
25 ++ formatTableEntries "scopev4" cfg.scopev4
26 );
27in
28{
29 options.networking.getaddrinfo = {
30 enable = lib.mkOption {
31 type = lib.types.bool;
32 default = pkgs.stdenv.hostPlatform.libc == "glibc";
33 defaultText = lib.literalExpression ''
34 pkgs.stdenv.hostPlatform.libc == "glibc"
35 '';
36 description = ''
37 Enables custom address sorting configuration for {manpage}`getaddrinfo(3)` according to RFC 3484.
38
39 This option generates a {file}`/etc/gai.conf` file to override the default address sorting tables,
40 as described in {manpage}`gai.conf(5)`.
41
42 This setting is only applicable when using the GNU C Library (glibc).
43 It has no effect with other libc implementations.
44 '';
45 };
46
47 reload = lib.mkOption {
48 type = lib.types.bool;
49 default = false;
50 description = ''
51 Determines whether a process should detect changes to the configuration file since it was last read.
52
53 If enabled, the file is re-read automatically. This may cause issues in multithreaded applications
54 and is generally discouraged.
55 '';
56 };
57
58 label = lib.mkOption {
59 type = lib.types.nullOr (lib.types.attrsOf lib.types.int);
60 default = null;
61 description = ''
62 Adds entries to the label table, as described in section 2.1 of RFC 3484.
63
64 If any label entries are provided, the glibc’s default label table is ignored.
65 '';
66 example = {
67 "::/0" = 1;
68 "2002::/16" = 2;
69 "::/96" = 3;
70 "::ffff:0:0/96" = 4;
71 "fec0::/10" = 5;
72 "fc00::/7" = 6;
73 "2001:0::/32" = 7;
74 };
75 };
76
77 precedence = lib.mkOption {
78 type = lib.types.nullOr (lib.types.attrsOf lib.types.int);
79 default = null;
80 description = ''
81 Similar to {option}`networking.getaddrinfo.label`, but this option
82 defines entries for the precedence table instead.
83
84 See sections 2.1 and 10.3 of RFC 3484 for details.
85
86 Providing any value will disable the glibc's default precedence table.
87 '';
88 example = {
89 "::1/128" = 50;
90 "::/0" = 40;
91 "2002::/16" = 30;
92 "::/96" = 20;
93 "::ffff:0:0/96" = 10;
94 };
95 };
96
97 scopev4 = lib.mkOption {
98 type = lib.types.nullOr (lib.types.attrsOf lib.types.int);
99 default = null;
100 description = ''
101 Adds custom rules to the IPv4 scope table.
102
103 By default, the scope IDs described in section 3.2 of RFC 6724 are used.
104
105 Modifying these values is rarely necessary.
106 '';
107 example = {
108 "::ffff:169.254.0.0/112" = 2;
109 "::ffff:127.0.0.0/104" = 2;
110 "::ffff:0.0.0.0/96" = 14;
111 };
112 };
113 };
114
115 config = lib.mkIf cfg.enable {
116 environment.etc."gai.conf".text = gaiConfText;
117 };
118
119 meta.maintainers = with lib.maintainers; [ moraxyc ];
120}