at 25.11-pre 9.2 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8let 9 inherit (lib) mkIf mkOption; 10 inherit (lib.types) 11 nullOr 12 path 13 bool 14 listOf 15 str 16 ; 17 keysPath = "/var/lib/yggdrasil/keys.json"; 18 19 cfg = config.services.yggdrasil; 20 settingsProvided = cfg.settings != { }; 21 configFileProvided = cfg.configFile != null; 22 23 format = pkgs.formats.json { }; 24in 25{ 26 imports = [ 27 (lib.mkRenamedOptionModule 28 [ "services" "yggdrasil" "config" ] 29 [ "services" "yggdrasil" "settings" ] 30 ) 31 ]; 32 33 options = { 34 services.yggdrasil = { 35 enable = lib.mkEnableOption "the yggdrasil system service"; 36 37 settings = mkOption { 38 type = format.type; 39 default = { }; 40 example = { 41 Peers = [ 42 "tcp://aa.bb.cc.dd:eeeee" 43 "tcp://[aaaa:bbbb:cccc:dddd::eeee]:fffff" 44 ]; 45 Listen = [ 46 "tcp://0.0.0.0:xxxxx" 47 ]; 48 }; 49 description = '' 50 Configuration for yggdrasil, as a Nix attribute set. 51 52 Warning: this is stored in the WORLD-READABLE Nix store! 53 Therefore, it is not appropriate for private keys. If you 54 wish to specify the keys, use {option}`configFile`. 55 56 If the {option}`persistentKeys` is enabled then the 57 keys that are generated during activation will override 58 those in {option}`settings` or 59 {option}`configFile`. 60 61 If no keys are specified then ephemeral keys are generated 62 and the Yggdrasil interface will have a random IPv6 address 63 each time the service is started. This is the default. 64 65 If both {option}`configFile` and {option}`settings` 66 are supplied, they will be combined, with values from 67 {option}`configFile` taking precedence. 68 69 You can use the command `nix-shell -p yggdrasil --run "yggdrasil -genconf"` 70 to generate default configuration values with documentation. 71 ''; 72 }; 73 74 configFile = mkOption { 75 type = nullOr path; 76 default = null; 77 example = "/run/keys/yggdrasil.conf"; 78 description = '' 79 A file which contains JSON or HJSON configuration for yggdrasil. See 80 the {option}`settings` option for more information. 81 82 Note: This file must not be larger than 1 MB because it is passed to 83 the yggdrasil process via systemds LoadCredential mechanism. For 84 details, see <https://systemd.io/CREDENTIALS/> and `man 5 85 systemd.exec`. 86 ''; 87 }; 88 89 group = mkOption { 90 type = nullOr str; 91 default = null; 92 example = "wheel"; 93 description = "Group to grant access to the Yggdrasil control socket. If `null`, only root can access the socket."; 94 }; 95 96 openMulticastPort = mkOption { 97 type = bool; 98 default = false; 99 description = '' 100 Whether to open the UDP port used for multicast peer discovery. The 101 NixOS firewall blocks link-local communication, so in order to make 102 incoming local peering work you will also need to configure 103 `MulticastInterfaces` in your Yggdrasil configuration 104 ({option}`settings` or {option}`configFile`). You will then have to 105 add the ports that you configure there to your firewall configuration 106 ({option}`networking.firewall.allowedTCPPorts` or 107 {option}`networking.firewall.interfaces.<name>.allowedTCPPorts`). 108 ''; 109 }; 110 111 denyDhcpcdInterfaces = mkOption { 112 type = listOf str; 113 default = [ ]; 114 example = [ "tap*" ]; 115 description = '' 116 Disable the DHCP client for any interface whose name matches 117 any of the shell glob patterns in this list. Use this 118 option to prevent the DHCP client from broadcasting requests 119 on the yggdrasil network. It is only necessary to do so 120 when yggdrasil is running in TAP mode, because TUN 121 interfaces do not support broadcasting. 122 ''; 123 }; 124 125 package = lib.mkPackageOption pkgs "yggdrasil" { }; 126 127 persistentKeys = lib.mkEnableOption '' 128 persistent keys. If enabled then keys will be generated once and Yggdrasil 129 will retain the same IPv6 address when the service is 130 restarted. Keys are stored at ${keysPath} 131 ''; 132 133 extraArgs = mkOption { 134 type = listOf str; 135 default = [ ]; 136 example = [ 137 "-loglevel" 138 "info" 139 ]; 140 description = "Extra command line arguments."; 141 }; 142 143 }; 144 }; 145 146 config = mkIf cfg.enable ( 147 let 148 binYggdrasil = "${cfg.package}/bin/yggdrasil"; 149 binHjson = "${pkgs.hjson-go}/bin/hjson-cli"; 150 in 151 { 152 assertions = [ 153 { 154 assertion = config.networking.enableIPv6; 155 message = "networking.enableIPv6 must be true for yggdrasil to work"; 156 } 157 ]; 158 159 # This needs to be a separate service. The yggdrasil service fails if 160 # this is put into its preStart. 161 systemd.services.yggdrasil-persistent-keys = lib.mkIf cfg.persistentKeys { 162 wantedBy = [ "multi-user.target" ]; 163 before = [ "yggdrasil.service" ]; 164 serviceConfig.Type = "oneshot"; 165 serviceConfig.RemainAfterExit = true; 166 script = '' 167 if [ ! -e ${keysPath} ] 168 then 169 mkdir --mode=700 -p ${builtins.dirOf keysPath} 170 ${binYggdrasil} -genconf -json \ 171 | ${pkgs.jq}/bin/jq \ 172 'to_entries|map(select(.key|endswith("Key")))|from_entries' \ 173 > ${keysPath} 174 fi 175 ''; 176 }; 177 178 systemd.services.yggdrasil = { 179 description = "Yggdrasil Network Service"; 180 after = [ "network-pre.target" ]; 181 wants = [ "network.target" ]; 182 before = [ "network.target" ]; 183 wantedBy = [ "multi-user.target" ]; 184 185 # This script first prepares the config file, then it starts Yggdrasil. 186 # The preparation could also be done in ExecStartPre/preStart but only 187 # systemd versions >= v252 support reading credentials in ExecStartPre. As 188 # of February 2023, systemd v252 is not yet in the stable branch of NixOS. 189 # 190 # This could be changed in the future once systemd version v252 has 191 # reached NixOS but it does not have to be. Config file preparation is 192 # fast enough, it does not need elevated privileges, and `set -euo 193 # pipefail` should make sure that the service is not started if the 194 # preparation fails. Therefore, it is not necessary to move the 195 # preparation to ExecStartPre. 196 script = '' 197 set -euo pipefail 198 199 # prepare config file 200 ${ 201 ( 202 if settingsProvided || configFileProvided || cfg.persistentKeys then 203 "echo " 204 205 + (lib.optionalString settingsProvided "'${builtins.toJSON cfg.settings}'") 206 + (lib.optionalString configFileProvided "$(${binHjson} -c \"$CREDENTIALS_DIRECTORY/yggdrasil.conf\")") 207 + (lib.optionalString cfg.persistentKeys "$(cat ${keysPath})") 208 + " | ${pkgs.jq}/bin/jq -s add | ${binYggdrasil} -normaliseconf -useconf" 209 else 210 "${binYggdrasil} -genconf" 211 ) 212 + " > /run/yggdrasil/yggdrasil.conf" 213 } 214 215 # start yggdrasil 216 exec ${binYggdrasil} -useconffile /run/yggdrasil/yggdrasil.conf ${lib.strings.escapeShellArgs cfg.extraArgs} 217 ''; 218 219 serviceConfig = 220 { 221 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; 222 Restart = "always"; 223 224 DynamicUser = true; 225 StateDirectory = "yggdrasil"; 226 RuntimeDirectory = "yggdrasil"; 227 RuntimeDirectoryMode = "0750"; 228 BindReadOnlyPaths = lib.optional cfg.persistentKeys keysPath; 229 LoadCredential = mkIf configFileProvided "yggdrasil.conf:${cfg.configFile}"; 230 231 AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE"; 232 CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE"; 233 MemoryDenyWriteExecute = true; 234 ProtectControlGroups = true; 235 ProtectHome = "tmpfs"; 236 ProtectKernelModules = true; 237 ProtectKernelTunables = true; 238 RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK"; 239 RestrictNamespaces = true; 240 RestrictRealtime = true; 241 SystemCallArchitectures = "native"; 242 SystemCallFilter = [ 243 "@system-service" 244 "~@privileged @keyring" 245 ]; 246 } 247 // ( 248 if (cfg.group != null) then 249 { 250 Group = cfg.group; 251 } 252 else 253 { } 254 ); 255 }; 256 257 networking.dhcpcd.denyInterfaces = cfg.denyDhcpcdInterfaces; 258 networking.firewall.allowedUDPPorts = mkIf cfg.openMulticastPort [ 9001 ]; 259 260 # Make yggdrasilctl available on the command line. 261 environment.systemPackages = [ cfg.package ]; 262 } 263 ); 264 meta = { 265 doc = ./yggdrasil.md; 266 maintainers = with lib.maintainers; [ 267 gazally 268 ehmry 269 nagy 270 ]; 271 }; 272}