stockholm/3modules/tv/identity.nix

83 lines
2.0 KiB
Nix
Raw Normal View History

2015-07-22 22:37:21 +00:00
{ config, lib, pkgs, ... }:
2015-07-11 14:55:22 +00:00
2015-07-22 22:37:21 +00:00
with import ../../4lib/tv { inherit lib pkgs; };
2015-07-11 14:55:22 +00:00
let
cfg = config.tv.identity;
out = {
options.tv.identity = api;
config = mkIf cfg.enable imp;
2015-07-11 14:55:22 +00:00
};
api = {
2015-07-13 15:36:31 +00:00
enable = mkEnableOption "tv.identity";
2015-07-11 14:55:22 +00:00
self = mkOption {
2015-07-22 22:37:21 +00:00
type = types.host;
2015-07-11 14:55:22 +00:00
};
2015-07-22 22:37:21 +00:00
#others = mkOption {
# type = types.host;
# default = filterAttrs (name: _host: name != cfg.self.name) cfg.hosts;
#};
2015-07-22 22:37:21 +00:00
2015-07-11 14:55:22 +00:00
hosts = mkOption {
2015-07-22 22:37:21 +00:00
type = with types; attrsOf host;
apply = mapAttrs (name: value: value // { inherit name; });
2015-07-11 14:55:22 +00:00
};
search = mkOption {
type = types.hostname;
};
2015-07-11 14:55:22 +00:00
};
imp = {
networking.extraHosts =
2015-07-22 22:37:21 +00:00
concatStringsSep "\n" (flatten (
# TODO deepMap ["hosts" "nets"] (hostname: host: netname: net:
mapAttrsToList (hostname: host:
mapAttrsToList (netname: net:
let
aliases = toString (unique (longs ++ shorts));
longs = (splitByProvider net.aliases).hosts;
shorts = map (removeSuffix ".${cfg.search}") longs;
2015-07-22 22:37:21 +00:00
in
map (addr: "${addr} ${aliases}") net.addrs
) host.nets
) cfg.hosts
));
};
# TODO move domain name providers to a dedicated module
# providers : tree label providername
providers = {
internet = "hosts";
retiolum = "hosts";
de.viljetic = "regfish";
de.krebsco = "ovh";
};
2015-07-11 14:55:22 +00:00
2015-07-22 22:37:21 +00:00
# splitByProvider : [alias] -> set providername [alias]
splitByProvider = foldl (acc: alias: insert (providerOf alias) alias acc) {};
# providerOf : alias -> providername
providerOf = alias:
tree-get (splitString "." alias) providers;
# insert : k -> v -> set k [v] -> set k [v]
insert = name: value: set:
set // { ${name} = set.${name} or [] ++ [value]; };
# tree k v = set k (either v (tree k v))
# tree-get : [k] -> tree k v -> v
tree-get = path: x:
let
y = x.${last path};
in
if typeOf y != "set"
then y
else tree-get (init path) y;
2015-07-11 14:55:22 +00:00
in
out