retiolum/modules/retiolum/default.nix

125 lines
3.4 KiB
Nix
Raw Normal View History

2020-08-02 21:06:41 +00:00
{ config, pkgs, lib, ... }:
with lib;
let
netname = "retiolum";
cfg = config.networking.retiolum;
2021-11-19 11:02:16 +00:00
hosts = ../../hosts;
2022-01-19 20:32:59 +00:00
genipv6 = import ./genipv6.nix { inherit lib; };
2020-08-02 21:06:41 +00:00
in {
options = {
networking.retiolum.ipv4 = mkOption {
2021-11-26 09:29:21 +00:00
type = types.nullOr types.str;
default = null;
2020-08-02 21:06:41 +00:00
description = ''
own ipv4 address
'';
};
networking.retiolum.ipv6 = mkOption {
type = types.str;
2022-01-19 20:32:59 +00:00
default = (genipv6 "retiolum" "external" {
2022-05-11 13:51:14 +00:00
hostName = cfg.nodename;
2022-01-19 20:32:59 +00:00
}).address;
2020-08-02 21:06:41 +00:00
description = ''
own ipv6 address
'';
};
networking.retiolum.nodename = mkOption {
type = types.str;
default = config.networking.hostName;
description = ''
tinc network name
'';
};
2023-01-31 13:48:51 +00:00
networking.retiolum.port = mkOption {
type = types.int;
default = 655;
description = ''
port tinc is listen
'';
};
2020-08-02 21:06:41 +00:00
};
config = {
services.tinc.networks.${netname} = {
name = cfg.nodename;
2021-11-09 06:29:42 +00:00
# allow resolving dns
chroot = false;
2020-08-02 21:06:41 +00:00
extraConfig = ''
2023-01-31 13:48:51 +00:00
Port = ${toString cfg.port}
2020-08-02 21:06:41 +00:00
LocalDiscovery = yes
2021-11-19 11:02:16 +00:00
ConnectTo = eva
ConnectTo = eve
2020-08-02 21:06:41 +00:00
ConnectTo = ni
ConnectTo = prism
AutoConnect = yes
'';
};
networking.extraHosts = if (cfg.ipv4 == null) then
builtins.readFile ../../etc.hosts-v6only
else
builtins.readFile ../../etc.hosts;
2020-08-02 21:06:41 +00:00
2020-11-01 08:30:04 +00:00
environment.systemPackages = [
config.services.tinc.networks.${netname}.package
];
2020-08-02 21:06:41 +00:00
2021-11-19 11:02:16 +00:00
systemd.services."tinc.${netname}-host-keys" = let
install-keys = pkgs.writeShellScript "install-keys" ''
2020-11-01 08:30:04 +00:00
rm -rf /etc/tinc/${netname}/hosts
2021-11-19 11:02:16 +00:00
cp -R ${hosts} /etc/tinc/${netname}/hosts
2020-11-01 08:30:04 +00:00
chown -R tinc.${netname} /etc/tinc/${netname}/hosts
chmod -R u+w /etc/tinc/${netname}/hosts
'';
2021-11-19 11:02:16 +00:00
in {
description = "Install tinc.${netname} host keys";
wantedBy = [ "multi-user.target" ];
before = [ "tinc.${netname}.service" ];
# we reload here to be reloaded before tinc reloads
reloadIfChanged = true;
serviceConfig = {
Type = "oneshot";
2021-11-19 11:02:16 +00:00
ExecStart = install-keys;
ExecReload = install-keys;
RemainAfterExit = true;
};
2020-11-01 08:30:04 +00:00
};
2020-12-12 06:45:02 +00:00
systemd.services."tinc.${netname}" = {
2021-11-19 11:02:16 +00:00
restartTriggers = [ hosts ];
2022-12-29 12:34:29 +00:00
# upstream defines this, but since we also set reloadIfChanged, we get a warning.
reloadTriggers = lib.mkForce [ ];
2020-12-12 06:45:02 +00:00
# Some hosts require VPN for nixos-rebuild, so we don't want to restart it on update
reloadIfChanged = true;
# also in https://github.com/NixOS/nixpkgs/pull/106715
serviceConfig.ExecReload = "${config.services.tinc.networks.${netname}.package}/bin/tinc -n ${netname} reload";
};
2020-08-02 21:06:41 +00:00
2023-01-31 13:48:51 +00:00
networking.firewall.allowedTCPPorts = [ cfg.port ];
networking.firewall.allowedUDPPorts = [ cfg.port ];
2020-08-02 21:06:41 +00:00
2021-11-26 09:29:21 +00:00
warnings = lib.optional (cfg.ipv6 == null) ''
`networking.retiolum.ipv6` is not set
'';
2020-08-02 21:06:41 +00:00
systemd.network.enable = true;
systemd.network.networks."${netname}".extraConfig = ''
[Match]
Name = tinc.${netname}
2020-08-02 21:06:41 +00:00
2020-08-16 15:53:33 +00:00
[Link]
2020-08-16 14:56:03 +00:00
# tested with `ping -6 turingmachine.r -s 1378`, not sure how low it must be
2020-08-16 15:53:33 +00:00
MTUBytes=1377
[Network]
2021-11-26 09:29:21 +00:00
${optionalString (cfg.ipv4 != null) "Address=${cfg.ipv4}/12"}
2022-09-28 08:14:46 +00:00
${optionalString (cfg.ipv6 != null) "Address=${cfg.ipv6}/16"}
RequiredForOnline = no
LinkLocalAddressing = no
2020-08-02 21:06:41 +00:00
'';
};
}