Greetd auto-login doesn't work with sway after killing the session

I’m not actively searching for a solution anymore. I’m using services.getty.autologinUser instead of greetd in those cases which require auto-login.

A while back I was exclusively using services.getty.autologinUser which did the job in a less fancy way. Since I started using greetd I didn’t notice the problems it caused on my kiosk systems(1 shared NixOS flake for all systems: laptop, server, router, …)
There are 2 types of kiosk(display 4 ipcameras, slideshow), both run a systemd service with timer. When the service finds out there are only 3 ipcameras displaying or the slideshow crashed or new slideshow added through usb or …
In those cases it will trigger a sway reload:
${pkgs.procps}/bin/pkill -u "${config.ncfg.primaryUserName}"
After such a reload greetd could be stuck and show a black screen with a cursor top-left.
journal logs below

Below the current situation:

{ config, lib, ... }:
let
  cfg = config.ncfg.services.auto-login;
in
{
  options.ncfg.services.auto-login = {
    enable = lib.mkEnableOption "Enable auto login";
    username = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
    };
  };

  config = lib.mkIf (cfg.enable || cfg.username != null) {
    assertions = lib.singleton {
      assertion = config.ncfg.sway.greetd == false;
      message = "You can't have auto-login with greetd. It was buggy";
    };

    ## Auto-login without password
    services.getty.autologinUser =
      if cfg.username != null then cfg.username else config.ncfg.primaryUserName;
  };
}

{
  config,
  lib,
  pkgs,
  ...
}:
with lib;
let
  cfg = config.ncfg.sway;
in
{
  options.ncfg.sway = {
    greetd = lib.mkEnableOption "Enable greetd before sway, instead of launching sway in tty0's shell";
  };

  config = lib.mkIf (config.ncfg.graphical == "sway") {
    # Configure greetd, a lightweight session manager
    services.greetd = lib.mkIf cfg.greetd {
      enable = true;
      settings =
        let
          # unset __HM_SESS_VARS_SOURCED __NIXOS_SET_ENVIRONMENT_DONE # otherwise sessionVariables are not updated
          command_tuigreet = lib.concatStringsSep " " [
            "${pkgs.greetd.tuigreet}/bin/tuigreet"
            "--time"
            "--cmd"
            ''"env __HM_SESS_VARS_SOURCED= systemd-cat -t sway -- sway"''
          ];

          # NOTE: 20240807 greetd auto-login doesn't work: when killing the session it fails to reload sway and shows a black screen which seems stuck.
          # ''"env __HM_SESS_VARS_SOURCED= ${sway}"''
          # command_auto_login = "env __HM_SESS_VARS_SOURCED= systemd-cat -t sway -- sway";
          session = {
            command = command_tuigreet;
            # command = if cfg.auto-login then command_auto_login else command_tuigreet;
            user = config.ncfg.primaryUserName;
          };
        in
        {
          # Executed Automatically
          # initial_session = lib.mkIf cfg.auto-login session;
          # Asks for password and executed with the user
          default_session = session;
        };
    };


    home-manager.users.${config.ncfg.primaryUserName} =
      { pkgs, ... }:
      {
      }
      // optionalAttrs (!cfg.greetd) {
        # Start automatically on tty1
        # exec sway &> /dev/null
        programs.bash.profileExtra = lib.mkBefore ''
          if [[ "$(tty)" == "/dev/tty1" ]]; then
            export XDG_SESSION_TYPE="wayland" # otherwise set to tty
            unset __HM_SESS_VARS_SOURCED __NIXOS_SET_ENVIRONMENT_DONE # otherwise sessionVariables are not updated
            exec ${systemd-cat} --identifier=sway ${sway} # Use systemd-cat here to capture sway logs
          fi
        '';
        # set -x SWAYSOCK "/run/user/"(id -u)"/sway.sock"
        programs.fish.loginShellInit = lib.mkBefore ''
          if test -z "$DISPLAY"; and test -z "$WAYLAND_DISPLAY"; and test (tty) = "/dev/tty1"
            export XDG_SESSION_TYPE="wayland" # otherwise set to tty
            # Otherwise sessionVariables are not updated
            # If terminal is login, unset __HM_SESS_VARS_SOURCED, because the
            # graphical session will inherit this (which means child applications
            # will never re-source when necessary)
            set --erase __HM_SESS_VARS_SOURCED
            set --erase __NIXOS_SET_ENVIRONMENT_DONE
            exec ${systemd-cat} --identifier=sway ${sway} # Use systemd-cat here to capture sway logs
          end
        '';
        programs.zsh.loginExtra = lib.mkBefore ''
          if [[ -z "$DISPLAY" ]] && [[ -z $WAYLAND_DISPLAY ]] && [[ "$(tty)" == "/dev/tty1" ]]; then
            export XDG_SESSION_TYPE="wayland" # otherwise set to tty
            unset __HM_SESS_VARS_SOURCED __NIXOS_SET_ENVIRONMENT_DONE # otherwise sessionVariables are not updated
            exec ${systemd-cat} --identifier=sway ${sway} # Use systemd-cat here to capture sway logs
          fi
        '';
      };
  };
}

initial: (ok)

20:34:11 systemd[3647]: Started mako.service.
20:34:11 systemd[3647]: Reached target Current graphical user session.
20:34:11 systemd[3647]: Started lock-screen-hook.service.
20:34:11 systemd[3647]: Reached target Session services which should run early before the graphical session is brought up.
20:34:11 systemd[3647]: Started D-Bus User Message Bus.
20:34:11 systemd[3647]: Starting D-Bus User Message Bus...
20:34:11 systemd[3647]: Created slice User Core Session Slice.
20:34:11 sway[3670]: 2024-08-05 20:34:11 - [main.c:282] Found config * for output HDMI-A-1
20:34:11 systemd[3647]: Started Highly customizable Wayland bar for Sway and Wlroots based compositors..
20:34:10 systemd[3647]: Startup finished in 299ms.
20:34:10 systemd[3647]: Reached target Main User Target.
20:34:10 systemd[3647]: Finished Run user-specific NixOS activation.
20:34:10 systemd[1]: Started Session 4 of User myUser.
20:34:10 systemd[3647]: Starting Run user-specific NixOS activation...
20:34:10 systemd[1]: Started User Manager for UID 1000.
20:34:10 systemd[3647]: Reached target Basic System.
20:34:10 systemd[3647]: Reached target Sockets.
20:34:10 systemd[3647]: Listening on D-Bus User Message Bus Socket.
20:34:10 systemd[3647]: Listening on PipeWire Multimedia System Sockets.
20:34:10 systemd[3647]: Listening on PipeWire PulseAudio.
20:34:10 systemd[3647]: Starting D-Bus User Message Bus Socket...
20:34:10 systemd[3647]: Reached target Timers.
20:34:10 systemd[3647]: Reached target Paths.
20:34:10 systemd[3647]: Created slice User Application Slice.
20:34:10 systemd[3647]: Queued start job for default target Main User Target.
20:34:10 systemd-logind[1215]: New session 5 of user myUser.
20:34:10 (systemd)[3647]: pam_unix(systemd-user:session): session opened for user myUser(uid=1000) by (uid=0)
20:34:10 systemd[1]: Starting User Manager for UID 1000...
20:34:10 systemd-logind[1215]: New session 4 of user myUser.
20:34:10 greetd[3644]: pam_systemd(greetd:session): New sd-bus connection (system-bus-pam-systemd-3644) opened.
20:34:10 greetd[3644]: pam_unix(greetd:session): session opened for user myUser(uid=1000) by (uid=0)
20:34:10 systemd-logind[1215]: Session 1 logged out. Waiting for processes to exit.
20:34:10 systemd-logind[1215]: Removed session 3.
20:34:10 systemd-logind[1215]: Session 3 logged out. Waiting for processes to exit.
20:34:10 systemd[1]: user@1000.service: Consumed 19.602s CPU time, 94.5M memory peak.
20:34:10 systemd[1]: user@1000.service: Failed with result 'signal'.
20:34:10 systemd[1]: user@1000.service: Killing process 1772 (wireplumber-ust) with signal SIGKILL.
20:34:10 greetd[1539]: pam_systemd(greetd:session): New sd-bus connection (system-bus-pam-systemd-1539) opened.
20:34:10 greetd[1539]: pam_unix(greetd:session): session closed for user myUser
20:34:10 systemd[1]: user@1000.service: Main process exited, code=killed, status=9/KILL
20:34:10 systemd[1]: session-3.scope: Consumed 1.138s CPU time, 13.8M memory peak.
20:34:10 systemd[1]: session-3.scope: Deactivated successfully.
20:34:10 systemd[1]: run-user-1000-doc.mount: Deactivated successfully.

after a reload (not ok)

20:40:08 systemd[4410]: Reached target Sockets.
20:40:08 systemd[4410]: Listening on D-Bus User Message Bus Socket.
20:40:08 systemd[4410]: Listening on PipeWire Multimedia System Sockets.
20:40:08 systemd[4410]: Listening on PipeWire PulseAudio.
20:40:08 systemd[4410]: Starting D-Bus User Message Bus Socket...
20:40:08 systemd[4410]: Reached target Timers.
20:40:08 systemd[4410]: Reached target Paths.
20:40:08 systemd[4410]: Created slice User Application Slice.
20:40:08 systemd[4410]: Queued start job for default target Main User Target.
20:40:08 systemd-logind[1215]: New session 10 of user myUser.
20:40:08 (systemd)[4410]: pam_unix(systemd-user:session): session opened for user myUser(uid=1000) by (uid=0)
20:40:08 systemd[1]: Starting User Manager for UID 1000...
20:40:08 systemd[1]: user@1000.service: Consumed 7.561s CPU time, 57.3M memory peak.
20:40:08 systemd[1]: user@1000.service: Failed with result 'signal'.
20:40:02 systemd-logind[1215]: Session 8 logged out. Waiting for processes to exit.
20:39:37 systemd-logind[1215]: New session 9 of user myUser.
20:39:22 systemd[1]: run-credentials-systemd\x2dtmpfiles\x2dclean.service.mount: Deactivated successfully.
20:39:22 systemd[1]: Finished Cleanup of Temporary Directories.
20:39:22 systemd[1]: systemd-tmpfiles-clean.service: Deactivated successfully.
20:39:22 systemd-logind[1215]: Session 7 logged out. Waiting for processes to exit.
20:39:22 systemd[1]: Starting Cleanup of Temporary Directories...
20:39:15 systemd[1]: user@1000.service: Killing process 3794 (wireplumber) with signal SIGKILL.
20:39:15 systemd[1]: user@1000.service: Processes still around after SIGKILL. Ignoring.
20:38:02 systemd-logind[1215]: New session 8 of user myUser.
20:37:22 systemd-logind[1215]: New session 7 of user myUser.
20:37:15 systemd-logind[1215]: Removed session 4.
20:37:15 systemd-logind[1215]: Session 4 logged out. Waiting for processes to exit.
20:37:15 systemd[1]: greetd.service: Deactivated successfully.
20:37:15 systemd[1]: session-4.scope: Consumed 3.211s CPU time, 275.4M memory peak.
20:37:15 systemd[1]: session-4.scope: Deactivated successfully.
20:37:15 greetd[1538]: error: check_children: greeter exited without creating a session
20:37:15 systemd-logind[1215]: Removed session 6.
20:37:15 systemd-logind[1215]: Session 6 logged out. Waiting for processes to exit.
20:37:15 greetd[3644]: pam_systemd(greetd:session): New sd-bus connection (system-bus-pam-systemd-3644) opened.
20:37:15 greetd[3644]: pam_unix(greetd:session): session closed for user myUser
20:37:15 systemd[1]: user@1000.service: Killing process 3794 (wireplumber) with signal SIGKILL.
20:37:15 systemd[1]: user@1000.service: Killing process 3782 (fusermount3) with signal SIGKILL.
20:37:15 systemd[1]: user@1000.service: Main process exited, code=killed, status=9/KILL
20:37:15 systemd[1]: session-6.scope: Deactivated successfully.
20:37:15 systemd[1]: run-user-1000-doc.mount: Deactivated successfully.