{ config, lib, mylib, pkgs, ... }: let cfg = config.hrm.continuum; format = pkgs.formats.toml {}; # Where an instance keeps the files it writes, unless its settings say so. # Only those that apply: continuum refuses the others. stateFiles = settings: let dir = "\${env:STATE_DIRECTORY}"; in lib.optionalAttrs (!(settings.relay-only or false)) { state-file = "${dir}/state.json"; document-file = "${dir}/document.automerge"; } // lib.optionalAttrs (settings ? store-id || settings ? store-root) { store-file = "${dir}/store.json"; } // lib.optionalAttrs (settings ? store-vote-key) { store-vote-record = "${dir}/party-record.json"; } // lib.optionalAttrs (settings.tor.enable or false) { tor.state-dir = "${dir}/tor"; }; in { options.hrm.continuum.instances = lib.mkOption { default = {}; description = '' continuum instances. Each is a service continuum- with the state directory /var/lib/continuum-. ''; type = lib.types.attrsOf (lib.types.submodule { options.credentials = lib.mkOption { default = {}; description = '' Secret files by name, like an htpasswd file or a key. The instance alone can read each as ''${env:CREDENTIALS_DIRECTORY}/, which is how settings refer to them. It restarts when one changes. ''; example = { htpasswd = "/var/src/secrets/continuum.htpasswd"; }; type = lib.types.attrsOf mylib.types.absolute-pathname; }; options.settings = lib.mkOption { default = {}; description = '' The instance's configuration: continuum's options by their long names, see docs/configuration.md and docs/example.toml in continuum. Text may hold placeholders like ''${env:STATE_DIRECTORY}, which continuum fills in when it starts. In a Nix string, escape them: "\''${env:STATE_DIRECTORY}/node.key". Unescaped, Nix takes env:STATE_DIRECTORY for a URL and drops the braces. The files continuum writes default to the state directory: state-file, document-file, and where they apply store-file, store-vote-record and tor.state-dir. Keys have no default. ''; type = lib.types.submodule { freeformType = format.type; }; }; }); }; config.hrm.systemd.services = lib.mapAttrs' (name: instance: lib.nameValuePair "continuum-${name}" { restartIfCredentialsChange = true; } ) (lib.filterAttrs (_: instance: instance.credentials != {}) cfg.instances); config.systemd.services = lib.mapAttrs' (name: instance: lib.nameValuePair "continuum-${name}" { description = "continuum ${name}"; wantedBy = [ "multi-user.target" ]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; serviceConfig = { ExecStart = "${pkgs.continuum}/bin/continuum --config ${ format.generate "continuum-${name}.toml" (lib.recursiveUpdate (stateFiles instance.settings) instance.settings) }"; # continuum exits with status 0 when a transport stops, too. Restart = "always"; RestartSec = "5s"; DynamicUser = true; StateDirectory = "continuum-${name}"; StateDirectoryMode = "0700"; LoadCredential = lib.mapAttrsToList (name: path: "${name}:${path}") instance.credentials; # It only needs the network and its state directory. ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; PrivateDevices = true; NoNewPrivileges = true; RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ]; CapabilityBoundingSet = ""; SystemCallFilter = [ "@system-service" ]; }; } ) cfg.instances; }