self: { config, lib, pkgs, ... }: let cfg = config.services.allmende; exe = lib.getExe cfg.package; # `allmende init` and `recover` run as the service's user and need a terminal # (they show and check the recovery phrase), so they are not part of the unit. admin = pkgs.writeShellScriptBin "allmende-admin" '' exec sudo -u allmende env ALLMENDE_DATA=${lib.escapeShellArg cfg.dataDir} ${exe} "$@" ''; args = [ "run" "--http" cfg.httpAddress "--listen" "/ip4/0.0.0.0/tcp/${toString cfg.p2pPort}" "--listen" "/ip4/0.0.0.0/udp/${toString cfg.p2pPort}/quic-v1" ] ++ lib.optional cfg.public "--public" ++ lib.optional (!cfg.defaultBootstrap) "--no-default-bootstrap" ++ lib.concatMap (r: [ "--region" r ]) cfg.regions ++ lib.concatMap (b: [ "--bootstrap" b ]) cfg.bootstrap ++ cfg.extraArgs; in { options.services.allmende = { enable = lib.mkEnableOption "an Allmende node (a free, distributed marketplace for homes)"; package = lib.mkOption { type = lib.types.package; default = self.packages.${pkgs.stdenv.hostPlatform.system}.default; defaultText = lib.literalExpression "allmende.packages.\${system}.default"; description = "The allmende package to run."; }; regions = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; example = [ "u1j" ]; description = '' Regions to replicate: 3-character geohashes, each about 150 km wide. `u1j` is Bonn, `u1h` Cologne, `u1x` Hamburg. May be empty only for a bootstrap node ({option}`public`). ''; }; bootstrap = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; example = [ "/ip4/203.0.113.5/tcp/4001/p2p/12D3KooW..." ]; description = "Extra peers to connect to: /p2p/ multiaddrs or /dnsaddr/."; }; defaultBootstrap = lib.mkOption { type = lib.types.bool; default = true; description = "Also use the built-in bootstrap name (allmende.stinnesbeck.com)."; }; public = lib.mkOption { type = lib.types.bool; default = false; description = '' This machine is reachable from the internet: serve the DHT and relay for nodes behind a NAT. Turn it on for a bootstrap node, and publish the TXT records the node prints (see `journalctl -u allmende`) in DNS. ''; }; httpAddress = lib.mkOption { type = lib.types.str; default = "127.0.0.1:8080"; description = '' Where the web UI and API listen. The same port serves the owner API, which the admin token protects, so keep it on loopback and put a TLS reverse proxy in front for public use. ''; }; p2pPort = lib.mkOption { type = lib.types.port; default = 4001; description = "Port other nodes connect to, over TCP and over UDP (QUIC)."; }; openFirewall = lib.mkOption { type = lib.types.bool; default = false; description = "Open the peer-to-peer port ({option}`p2pPort`, TCP and UDP) in the firewall."; }; dataDir = lib.mkOption { type = lib.types.path; default = "/var/lib/allmende"; description = "Where keys, the admin token and the database live."; }; extraArgs = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; example = [ "--no-mdns" ]; description = "Further arguments for `allmende run`."; }; }; config = lib.mkIf cfg.enable { assertions = [ { assertion = cfg.regions != [ ] || cfg.public; message = "services.allmende.regions needs at least one region, for example [ \"u1j\" ] (only a public bootstrap node may have none)."; } ]; users.users.allmende = { isSystemUser = true; group = "allmende"; home = cfg.dataDir; description = "Allmende node"; }; users.groups.allmende = { }; systemd.tmpfiles.rules = [ "d ${cfg.dataDir} 0700 allmende allmende -" ]; # One-time setup, because it shows the recovery phrase: # sudo allmende-admin init && sudo systemctl start allmende # The unit is skipped (not failed) until that has happened. systemd.services.allmende = { description = "Allmende node"; wantedBy = [ "multi-user.target" ]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; unitConfig.ConditionPathExists = "${cfg.dataDir}/admin.token"; environment.ALLMENDE_DATA = cfg.dataDir; serviceConfig = { ExecStart = "${exe} ${lib.escapeShellArgs args}"; User = "allmende"; Group = "allmende"; Restart = "on-failure"; RestartSec = "5s"; NoNewPrivileges = true; PrivateTmp = true; PrivateDevices = true; ProtectSystem = "strict"; ProtectHome = true; ReadWritePaths = [ cfg.dataDir ]; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectControlGroups = true; RestrictSUIDSGID = true; LockPersonality = true; # libp2p reads the network interfaces over netlink to find its addresses. RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ]; }; }; networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ]; networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ]; # QUIC environment.systemPackages = [ admin ]; }; }