2023-11-14 11:58:15 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2023-11-15 05:33:31 +00:00
|
|
|
let
|
2023-11-15 06:44:30 +00:00
|
|
|
cfg = config.clan.networking.meshnamed;
|
2023-11-15 05:33:31 +00:00
|
|
|
in
|
2023-11-14 11:58:15 +00:00
|
|
|
{
|
|
|
|
options.clan.networking.meshnamed = {
|
|
|
|
enable = (lib.mkEnableOption "meshnamed") // {
|
|
|
|
default = config.clan.networking.meshnamed.networks != { };
|
|
|
|
};
|
2023-11-15 06:44:30 +00:00
|
|
|
listenAddress = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "fd66:29e9:f422:8dfe:beba:68ec:bd09:7876";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The address to listen on.
|
|
|
|
'';
|
|
|
|
};
|
2023-11-14 11:58:15 +00:00
|
|
|
networks = lib.mkOption {
|
|
|
|
default = { };
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
|
|
|
|
options = {
|
|
|
|
name = lib.mkOption {
|
|
|
|
default = name;
|
|
|
|
type = lib.types.str;
|
|
|
|
example = "my-network";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The name of the network.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
subnet = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
example = "fd43:7def:4b50:28d0:4e99:9347:3035:17ef/88";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The subnet to use for the mesh network.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = lib.mkIf config.clan.networking.meshnamed.enable {
|
2023-11-15 05:33:31 +00:00
|
|
|
# we assign this random source address to bind meshnamed to.
|
2023-11-15 06:44:30 +00:00
|
|
|
systemd.network.netdevs."08-meshnamed" = {
|
|
|
|
netdevConfig = {
|
|
|
|
Name = "meshnamed";
|
|
|
|
Kind = "dummy";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.network.networks."08-meshnamed" = {
|
|
|
|
matchConfig.Name = "meshnamed";
|
|
|
|
networkConfig = {
|
|
|
|
Address = [ "${cfg.listenAddress}/128" ];
|
|
|
|
DNS = [ config.clan.networking.meshnamed.listenAddress ];
|
|
|
|
Domains = [ "~${lib.concatMapStringsSep "," (network: network.name) (builtins.attrValues config.clan.networking.meshnamed.networks)}" ];
|
|
|
|
};
|
2023-11-15 05:33:31 +00:00
|
|
|
};
|
|
|
|
|
2023-11-16 13:39:15 +00:00
|
|
|
# for convenience, so we can debug with dig
|
2023-11-15 05:33:31 +00:00
|
|
|
networking.extraHosts = ''
|
2023-11-15 06:44:30 +00:00
|
|
|
${cfg.listenAddress} meshnamed
|
2023-11-15 05:33:31 +00:00
|
|
|
'';
|
|
|
|
|
2024-01-11 11:16:59 +00:00
|
|
|
networking.networkmanager.unmanaged = [ "interface-name:meshnamed" ];
|
|
|
|
|
2023-11-14 11:58:15 +00:00
|
|
|
systemd.services.meshnamed =
|
|
|
|
let
|
|
|
|
networks = lib.concatMapStringsSep "," (network: "${network.name}=${network.subnet}")
|
|
|
|
(builtins.attrValues config.clan.networking.meshnamed.networks);
|
|
|
|
in
|
|
|
|
{
|
2023-11-15 10:20:56 +00:00
|
|
|
# fix container test
|
|
|
|
after = [ "network.target" ] ++ lib.optional config.boot.isContainer "sys-devices-virtual-net-meshnamed.device";
|
|
|
|
bindsTo = lib.optional (!config.boot.isContainer) "sys-devices-virtual-net-meshnamed.device";
|
2023-11-14 11:58:15 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
2023-11-15 06:44:30 +00:00
|
|
|
ExecStart = "${pkgs.callPackage ../../../pkgs/meshname/default.nix { }}/bin/meshnamed -networks ${networks} -listenaddr [${cfg.listenAddress}]:53";
|
2023-11-15 05:33:31 +00:00
|
|
|
|
|
|
|
# to bind port 53
|
|
|
|
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
2023-11-14 11:58:15 +00:00
|
|
|
DynamicUser = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|