{ 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"; 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 = lib.attrsets.mapAttrs' (name: cfg: { name = "syndicate-${name}"; value = let flags = lib.strings.concatMapStrings (c: " --config ${c}") cfg.config; serviceConfig = { ExecStart = "${cfg.package}/bin/syndicate-server --no-banner ${flags}"; User = cfg.user; }; in if builtins.match "tty[0-9]" name != null then { enable = true; description = "Syndicate dataspace server"; after = [ "systemd-user-sessions.service" "systemd-logind.service" "getty@${name}.service" ]; before = [ "graphical.target" ]; wants = [ "dbus.socket" "systemd-logind.service" ]; wantedBy = [ "graphical.target" ]; conflicts = [ "getty@${name}.service" ]; restartIfChanged = false; 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 = "~"; }; } else { description = "Syndicate dataspace server"; wantedBy = [ "multi-user.target" ]; inherit serviceConfig; }; }) (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); }; }