{ config, lib, pkgs, ... }: with lib; { options.services.syndicate = mkOption { default = { }; example = { tty1 = { enable = true; user = "jane"; config = [ "/etc/syndicate" ]; }; }; description = '' Syndicate dataspace server instances. If the key is in the form of "tty''${N}" it will be attached to the appropriate teletypewriter. ''; type = types.attrsOf (types.submodule { options = { enable = mkEnableOption "this Syndicate dataspace server instance"; runInDbusSession = mkEnableOption "dbus-run-session wrapper"; user = mkOption { type = types.str; example = "jane"; description = "User account under which the Syndicate server runs."; }; package = mkOption { default = pkgs.syndicate-server; defaultText = "pkgs.syndicate-server"; type = types.package; description = "The package to use for the Syndicate dataspace server."; }; config = mkOption { type = types.listOf types.path; description = "Configurations to load."; example = [ "/etc/syndicate" ]; }; }; }); }; config = { systemd.services = let serverCfgs = lib.attrsets.filterAttrs (_: cfg: cfg.enable) config.services.syndicate; in builtins.listToAttrs (lib.lists.flatten (lib.attrsets.mapAttrsToList (name: cfg: let configFileName = "syndicate-nixos-config.pr"; runtimeConfig = "\${RUNTIME_DIRECTORY}/${configFileName}"; configFile = pkgs.writeText configFileName (lib.strings.concatMapStrings (dir: '' > '') cfg.config); in [{ name = "syndicate-${name}"; value = let serviceConfig = let loadConfig = "${pkgs.coreutils}/bin/cp ${configFile} ${runtimeConfig}"; in { RuntimeDirectory = name; ExecStartPre = loadConfig; ExecStart = "${ lib.optionalString cfg.runInDbusSession "${pkgs.dbus}/bin/dbus-run-session " }${cfg.package}/bin/syndicate-server --no-banner --config ${runtimeConfig}"; ExecReload = loadConfig; User = cfg.user; }; in { description = "Syndicate dataspace server"; restartIfChanged = false; reloadTriggers = [ configFile ]; wantedBy = [ "multi-user.target" ]; } // (if builtins.match "tty[0-9]" name == null then { inherit serviceConfig; } else { after = [ "systemd-user-sessions.service" "systemd-logind.service" "getty@${name}.service" ]; wants = [ "dbus.socket" "systemd-logind.service" ]; conflicts = [ "getty@${name}.service" ]; unitConfig.ConditionPathExists = "/dev/${name}"; serviceConfig = serviceConfig // { PAMName = "login"; StandardError = "journal"; StandardInput = "tty-fail"; StandardOutput = "journal"; TTYPath = "/dev/${name}"; TTYReset = "yes"; TTYVHangup = "yes"; TTYVTDisallocate = "yes"; UtmpIdentifier = "%n"; UtmpMode = "user"; WorkingDirectory = "~"; }; }); }]) (lib.attrsets.filterAttrs (_: cfg: cfg.enable) config.services.syndicate))); systemd.targets.multi-user.wants = lib.lists.flatten (lib.attrsets.mapAttrsToList (name: cfg: lib.optional cfg.enable "syndicate-${name}.service") config.services.syndicate); }; }