2019-06-27 16:56:10 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
2020-05-03 11:30:31 +01:00
|
|
|
keysPath = "/var/lib/yggdrasil/keys.json";
|
|
|
|
|
2019-06-27 16:56:10 +01:00
|
|
|
cfg = config.services.yggdrasil;
|
2022-08-07 19:21:22 +01:00
|
|
|
settingsProvided = cfg.settings != { };
|
2020-05-03 11:30:31 +01:00
|
|
|
configFileProvided = cfg.configFile != null;
|
2019-06-27 16:56:10 +01:00
|
|
|
|
2022-08-07 19:21:22 +01:00
|
|
|
format = pkgs.formats.json { };
|
2019-06-27 16:56:10 +01:00
|
|
|
in {
|
2022-08-07 19:21:22 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule
|
|
|
|
[ "services" "yggdrasil" "config" ]
|
|
|
|
[ "services" "yggdrasil" "settings" ])
|
|
|
|
];
|
|
|
|
|
2019-06-27 16:56:10 +01:00
|
|
|
options = with types; {
|
|
|
|
services.yggdrasil = {
|
|
|
|
enable = mkEnableOption "the yggdrasil system service";
|
|
|
|
|
2022-08-07 19:21:22 +01:00
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
2019-06-27 16:56:10 +01:00
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
Peers = [
|
|
|
|
"tcp://aa.bb.cc.dd:eeeee"
|
|
|
|
"tcp://[aaaa:bbbb:cccc:dddd::eeee]:fffff"
|
|
|
|
];
|
|
|
|
Listen = [
|
|
|
|
"tcp://0.0.0.0:xxxxx"
|
|
|
|
];
|
|
|
|
};
|
2022-08-15 06:16:25 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-06-27 16:56:10 +01:00
|
|
|
Configuration for yggdrasil, as a Nix attribute set.
|
|
|
|
|
|
|
|
Warning: this is stored in the WORLD-READABLE Nix store!
|
2020-05-03 11:30:31 +01:00
|
|
|
Therefore, it is not appropriate for private keys. If you
|
2022-08-15 06:16:25 +01:00
|
|
|
wish to specify the keys, use {option}`configFile`.
|
2020-05-03 11:30:31 +01:00
|
|
|
|
2022-08-15 06:16:25 +01:00
|
|
|
If the {option}`persistentKeys` is enabled then the
|
2020-05-03 11:30:31 +01:00
|
|
|
keys that are generated during activation will override
|
2022-08-15 06:16:25 +01:00
|
|
|
those in {option}`config` or
|
|
|
|
{option}`configFile`.
|
2020-05-03 11:30:31 +01:00
|
|
|
|
|
|
|
If no keys are specified then ephemeral keys are generated
|
|
|
|
and the Yggdrasil interface will have a random IPv6 address
|
|
|
|
each time the service is started, this is the default.
|
2019-06-27 16:56:10 +01:00
|
|
|
|
2022-08-15 06:16:25 +01:00
|
|
|
If both {option}`configFile` and {option}`config`
|
2020-05-03 11:30:31 +01:00
|
|
|
are supplied, they will be combined, with values from
|
2022-08-15 06:16:25 +01:00
|
|
|
{option}`configFile` taking precedence.
|
2019-06-27 16:56:10 +01:00
|
|
|
|
2022-08-15 06:16:25 +01:00
|
|
|
You can use the command `nix-shell -p yggdrasil --run "yggdrasil -genconf"`
|
2022-08-13 10:35:46 +01:00
|
|
|
to generate default configuration values with documentation.
|
2019-06-27 16:56:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-05-03 11:30:31 +01:00
|
|
|
configFile = mkOption {
|
|
|
|
type = nullOr path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/yggdrasil.conf";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-05-03 11:30:31 +01:00
|
|
|
A file which contains JSON configuration for yggdrasil.
|
2022-07-28 22:19:15 +01:00
|
|
|
See the {option}`config` option for more information.
|
2020-05-03 11:30:31 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-07-02 17:20:27 +01:00
|
|
|
group = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "wheel";
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc "Group to grant access to the Yggdrasil control socket. If `null`, only root can access the socket.";
|
2022-07-02 17:20:27 +01:00
|
|
|
};
|
|
|
|
|
2019-06-27 16:56:10 +01:00
|
|
|
openMulticastPort = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = false;
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-06-27 16:56:10 +01:00
|
|
|
Whether to open the UDP port used for multicast peer
|
|
|
|
discovery. The NixOS firewall blocks link-local
|
|
|
|
communication, so in order to make local peering work you
|
2022-08-03 21:46:41 +01:00
|
|
|
will also need to set `LinkLocalTCPPort` in your
|
|
|
|
yggdrasil configuration ({option}`config` or
|
|
|
|
{option}`configFile`) to a port number other than 0,
|
2019-06-27 16:56:10 +01:00
|
|
|
and then add that port to
|
2022-08-03 21:46:41 +01:00
|
|
|
{option}`networking.firewall.allowedTCPPorts`.
|
2019-06-27 16:56:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
denyDhcpcdInterfaces = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "tap*" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-06-27 16:56:10 +01:00
|
|
|
Disable the DHCP client for any interface whose name matches
|
|
|
|
any of the shell glob patterns in this list. Use this
|
|
|
|
option to prevent the DHCP client from broadcasting requests
|
|
|
|
on the yggdrasil network. It is only necessary to do so
|
|
|
|
when yggdrasil is running in TAP mode, because TUN
|
|
|
|
interfaces do not support broadcasting.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = package;
|
|
|
|
default = pkgs.yggdrasil;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.yggdrasil";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Yggdrasil package to use.";
|
2019-06-27 16:56:10 +01:00
|
|
|
};
|
2020-05-03 11:30:31 +01:00
|
|
|
|
|
|
|
persistentKeys = mkEnableOption ''
|
|
|
|
If enabled then keys will be generated once and Yggdrasil
|
|
|
|
will retain the same IPv6 address when the service is
|
|
|
|
restarted. Keys are stored at ${keysPath}.
|
|
|
|
'';
|
|
|
|
|
2019-06-27 16:56:10 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-03 11:30:31 +01:00
|
|
|
config = mkIf cfg.enable (let binYggdrasil = cfg.package + "/bin/yggdrasil";
|
|
|
|
in {
|
|
|
|
assertions = [{
|
|
|
|
assertion = config.networking.enableIPv6;
|
|
|
|
message = "networking.enableIPv6 must be true for yggdrasil to work";
|
|
|
|
}];
|
|
|
|
|
|
|
|
system.activationScripts.yggdrasil = mkIf cfg.persistentKeys ''
|
|
|
|
if [ ! -e ${keysPath} ]
|
|
|
|
then
|
2021-05-07 01:14:03 +01:00
|
|
|
mkdir --mode=700 -p ${builtins.dirOf keysPath}
|
2020-05-03 11:30:31 +01:00
|
|
|
${binYggdrasil} -genconf -json \
|
|
|
|
| ${pkgs.jq}/bin/jq \
|
|
|
|
'to_entries|map(select(.key|endswith("Key")))|from_entries' \
|
|
|
|
> ${keysPath}
|
|
|
|
fi
|
|
|
|
'';
|
2019-06-27 16:56:10 +01:00
|
|
|
|
|
|
|
systemd.services.yggdrasil = {
|
|
|
|
description = "Yggdrasil Network Service";
|
2022-07-27 17:01:18 +01:00
|
|
|
after = [ "network-pre.target" ];
|
|
|
|
wants = [ "network.target" ];
|
|
|
|
before = [ "network.target" ];
|
2019-06-27 16:56:10 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2020-05-03 11:30:31 +01:00
|
|
|
preStart =
|
2022-08-07 19:21:22 +01:00
|
|
|
(if settingsProvided || configFileProvided || cfg.persistentKeys then
|
2020-05-03 11:30:31 +01:00
|
|
|
"echo "
|
|
|
|
|
2022-08-07 19:21:22 +01:00
|
|
|
+ (lib.optionalString settingsProvided
|
|
|
|
"'${builtins.toJSON cfg.settings}'")
|
2020-05-03 11:30:31 +01:00
|
|
|
+ (lib.optionalString configFileProvided "$(cat ${cfg.configFile})")
|
|
|
|
+ (lib.optionalString cfg.persistentKeys "$(cat ${keysPath})")
|
|
|
|
+ " | ${pkgs.jq}/bin/jq -s add | ${binYggdrasil} -normaliseconf -useconf"
|
|
|
|
else
|
|
|
|
"${binYggdrasil} -genconf") + " > /run/yggdrasil/yggdrasil.conf";
|
2019-06-27 16:56:10 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
2020-05-03 11:30:31 +01:00
|
|
|
ExecStart =
|
|
|
|
"${binYggdrasil} -useconffile /run/yggdrasil/yggdrasil.conf";
|
2019-06-27 16:56:10 +01:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
Restart = "always";
|
|
|
|
|
2022-07-02 15:34:59 +01:00
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = "yggdrasil";
|
2019-06-27 16:56:10 +01:00
|
|
|
RuntimeDirectory = "yggdrasil";
|
2020-05-01 19:46:52 +01:00
|
|
|
RuntimeDirectoryMode = "0750";
|
2020-05-03 11:30:31 +01:00
|
|
|
BindReadOnlyPaths = lib.optional configFileProvided cfg.configFile
|
|
|
|
++ lib.optional cfg.persistentKeys keysPath;
|
2022-07-02 15:34:59 +01:00
|
|
|
ReadWritePaths = "/run/yggdrasil";
|
2019-06-27 16:56:10 +01:00
|
|
|
|
2022-07-02 15:34:59 +01:00
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
2019-06-27 16:56:10 +01:00
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = "tmpfs";
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = "~@clock @cpu-emulation @debug @keyring @module @mount @obsolete @raw-io @resources";
|
2022-07-02 17:20:27 +01:00
|
|
|
} // (if (cfg.group != null) then {
|
|
|
|
Group = cfg.group;
|
|
|
|
} else {});
|
2019-06-27 16:56:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
networking.dhcpcd.denyInterfaces = cfg.denyDhcpcdInterfaces;
|
|
|
|
networking.firewall.allowedUDPPorts = mkIf cfg.openMulticastPort [ 9001 ];
|
|
|
|
|
|
|
|
# Make yggdrasilctl available on the command line.
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
2020-05-03 11:30:31 +01:00
|
|
|
});
|
2020-05-30 11:05:48 +01:00
|
|
|
meta = {
|
|
|
|
doc = ./yggdrasil.xml;
|
|
|
|
maintainers = with lib.maintainers; [ gazally ehmry ];
|
|
|
|
};
|
2019-06-27 16:56:10 +01:00
|
|
|
}
|