-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathips.nix
65 lines (58 loc) · 2 KB
/
ips.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
59
60
61
62
63
64
65
{ lib, ... }@inputs:
let
inherit (lib)
mkOption mapAttrsRecursive mapAttrsRecursiveCond concatStringsSep flatten
collect isList splitString head last replaceStrings setAttrByPath
mkEnableOption;
inherit (lib.types) submodule attrsOf anything;
inherit (import ./lib.nix inputs) recursiveMergeNoOverride mapAttrsFunc;
mapAttrsToPathValueList = set:
(mapAttrsRecursive (path: value:
let
spl = splitString ":" value;
service = splitString "." (replaceStrings [ "%" ] [ "." ] (head spl));
iface = last spl;
ip = concatStringsSep "." path;
in [{
inherit ip service;
setForIf = if iface == "" then false else true;
}]) set);
flatten_attrs_list = set:
flatten (collect isList (collect isList (mapAttrsToPathValueList set)));
flatten_attrs_rec = host_ports:
recursiveMergeNoOverride (map (eth_attrs:
setAttrByPath (eth_attrs.service) ({ inherit (eth_attrs) ip setForIf; }))
(flatten_attrs_list host_ports));
flatten_attrs_non_rec = host_ports:
recursiveMergeNoOverride (map (eth_attrs: {
"${concatStringsSep "." eth_attrs.service}" = {
inherit (eth_attrs) ip setForIf;
};
}) (flatten_attrs_list host_ports));
map_to_ips = set:
mapAttrsRecursiveCond (as:
!(as
? ip)) # this assumes that you dont use port as a path inside your key!
(_: x: x.ip) set;
in {
options.ips = mkOption {
type = (submodule {
options = {
ips = mkOption {
type = attrsOf (anything);
default = { };
description = "";
apply = old: rec {
raw = old;
#flattened = mapAttrsFunc flatten_attrs_list old;
flattened = mapAttrsFunc flatten_attrs_non_rec old;
full = mapAttrsFunc flatten_attrs_rec old;
ips = mapAttrsFunc map_to_ips full;
};
};
setForIf.enable = mkEnableOption
"Enables setting ips for interfaces, if specified for the ip";
};
});
};
}