forked from
lass.pds.lassul.us/superconfig
This repository has no description
2.3 kB
79 lines
1# Host data from the kartei flake input, rendered straight into NixOS
2# options; stockholm's krebs.hosts (and its hosts/ssh/build modules) is not
3# used.
4{
5 config,
6 lib,
7 self,
8 ...
9}:
10let
11 inherit (self.inputs) kartei;
12
13 inherit (kartei) hosts;
14
15 # Name suffixes that go into /etc/hosts; ".r" names also get a bare short
16 # form (barnacle.r -> barnacle), as stockholm's krebs.dns did.
17 domains = [
18 "r"
19 "i"
20 "w"
21 "shack"
22 ];
23 hostDomain = alias: lib.any (d: lib.hasSuffix ".${d}" alias) domains;
24 shortsOf = aliases: map (lib.removeSuffix ".r") (lib.filter (lib.hasSuffix ".r") aliases);
25
26 netAddrs = net: net.addrs;
27 netAliases = net: net.aliases or [ ];
28 sshPort = net: net.ssh.port or 22;
29
30 allNets = lib.concatMap (host: lib.attrValues (host.nets or { })) (lib.attrValues hosts);
31
32in
33{
34 config = {
35 networking.hosts = lib.filterAttrs (_: names: names != [ ]) (
36 lib.zipAttrsWith (_: lib.concatLists) (
37 lib.concatMap (
38 net:
39 let
40 longs = lib.filter hostDomain (netAliases net);
41 names = longs ++ shortsOf longs;
42 in
43 map (addr: { ${addr} = names; }) (netAddrs net)
44 ) allNets
45 )
46 );
47
48 services.openssh.knownHosts =
49 lib.filterAttrs (_: known: known.publicKey != null && known.hostNames != [ ])
50 (
51 lib.mapAttrs (_: host: {
52 publicKey = host.ssh.pubkey or null;
53 hostNames = lib.concatMap (
54 net:
55 map (name: if sshPort net != 22 then "[${name}]:${toString (sshPort net)}" else name) (
56 shortsOf (netAliases net) ++ netAliases net ++ netAddrs net
57 )
58 ) (lib.attrValues (host.nets or { }));
59 }) hosts
60 // {
61 localhost = {
62 publicKey = hosts.${config.networking.hostName}.ssh.pubkey or null;
63 hostNames = [
64 "localhost"
65 "127.0.0.1"
66 "::1"
67 ];
68 };
69 }
70 );
71
72 programs.ssh.extraConfig = lib.mkBefore (
73 lib.concatMapStrings (net: ''
74 Host ${toString (netAliases net ++ netAddrs net)}
75 Port ${toString (sshPort net)}
76 '') (lib.filter (net: sshPort net != 22) allNets)
77 );
78 };
79}