Failed to create session: Unit dbus-org.freedesktop.login1.service not found

I’ve been trying to use SwayWM instead of i3 (mainly for hardware reasons, amdgpu with my laptop has a known issue for recovring from sleep while beeing on a Xorg session).

Sway is working really well, but I cannot start it on boot properly. I do not want to start a displaymanager because they are not working really well with Wayland sessions.

I currently start Sway this way :

  programs.sway = {
    enable = true;
    extraPackages = with pkgs; [
      swaylock swayidle xwayland termite
      mako grim slurp wl-clipboard wf-recorder
    ];
    extraSessionCommands = ''
      export SDL_VIDEODRIVER=wayland
      export QT_QPA_PLATFORM=wayland
      export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
      export _JAVA_AWT_WM_NONREPARENTING=1
      export MOZ_ENABLE_WAYLAND=1
    '';
  };

  systemd.defaultUnit = "graphical.target";
  systemd.services.sway = {
    enable = true;
    wants = [ "systemd-machined.service" ];
    aliases = [ "display-manager.service" ];
    after =  [
      "rc-local.service"
      "systemd-machined.service"
      "systemd-user-sessions.service"
      "plymouth-quit.service"
      "plymouth-start.service"
    ];
    serviceConfig = {
      ExecStartPre = "${config.system.path}/bin/chvt ${sway.tty}";
      ExecStart = "${pkgs.dbus}/bin/dbus-launch --exit-with-session ${pkgs.sway}/bin/sway";
      TTYPath = "/dev/tty${sway.tty}";
      TTYReset = "yes";
      TTYVHangup = "yes";
      TTYVTDisallocate = "yes";
      PAMName = "login";
      User = sway.username;
      WorkingDirectory = "/home/${sway.username}";
      StandardInput = "tty";
      StandardError = "journal";
      StandardOutput = "journal";
      Restart = "no";
    };
  };

I mainly used the gdm nix derivation as a source for creating this systemd unit.

But when it starts, it crashes completly at the first try :

Dec 01 10:14:29 ely-lenovo systemd[1]: Starting sway.service...
Dec 01 10:14:29 ely-lenovo systemd[1034]: pam_unix(login:session): session opened for user ely by (uid=0)
Dec 01 10:14:29 ely-lenovo systemd[1034]: pam_systemd(login:session): Failed to create session: Unit dbus-org.freedesktop.login1.service not found.
Dec 01 10:14:29 ely-lenovo systemd[1]: Started sway.service.
Dec 01 10:14:29 ely-lenovo systemd[1036]: pam_unix(login:session): session opened for user ely by (uid=0)
Dec 01 10:14:29 ely-lenovo systemd[1036]: pam_systemd(login:session): Failed to create session: Unit dbus-org.freedesktop.login1.service not found.
Dec 01 10:14:29 ely-lenovo dbus-launch[1036]: XDG_RUNTIME_DIR is not set in the environment. Aborting.
Dec 01 10:14:29 ely-lenovo systemd[1]: sway.service: Main process exited, code=exited, status=1/FAILURE
Dec 01 10:14:32 ely-lenovo systemd[1]: sway.service: Failed with result 'exit-code'.

If I put Restart to on-failure it works correctly at a second try. I conclude that the session starts a bit too early in the process, but I cannot figure out what I’m supposed to wait for. Any ideas ?

I didn’t test it but that should be avoidable by adding a dependency on systemd-logind.service to after = [ ... ].

Thank you very much. It was indeed the missing dependency on this unit.

Hi @Elyhaka,

I stumbled accross this discourse and wanted to ask if you could publish your nix files to get sway without a loginmanager running? The code posted here with the aforementioned fix does not work fully ( I only get a gray screen and can’t spawn any shell or the like) . That would be huge!

Hello,

You should add a variable like this one in the service definition :

let
  sway = {
    tty = 7;
    username = "whatever_username";
  };
in { ... }

Huh thanks your were fast to respond! ^^

I did that already (used tty2) but without success.

$ nix-info
system: "x86_64-linux", multi-user?: yes, version: nix-env (Nix) 2.3.3, channels(root): "nixos-19.09.2229.d7843c8add6, nixos-unstable-20.09pre215947.82b54d49066", nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos

I also tried on different sway versions without success.
If I get it to work I would like to make an official module out of it.
Can you reproduce your working configuration in a vm with:

$ nixos-rebuild --fast build-vm

Since Sway seems to open (if you have a grey screen with it inside) maybe its just Sway configuration that is just on default then ?

I was using home-manager to handle the configuration of my Sway desktop environment. I’ll do some test this week-end !

Yeah sway does start indeed but it does not read my configuration and I don’t see any error messages in my journalctl. Even when I tell sway with the --config flag to read that specific file it only spawns a gray screen which is why I am a bit lost here.

That would be awesome!

I’m having the same issue as @Luis-Hebendanz with build-vm as well as when booting on the physical hardware.

At boot sway seems to start up on the specified tty7 and the system switches to that one automatically, but same as Luis describes I cannot interact with it, as it only shows a grey screen with the working mouse cursor but no other graphical elements and keyboard inputs seem to not be not working.

I think we both may just be misunderstanding how your setup is meant to work in relation to the login process, @Elyhaka. I must admit I am quite new to NixOS and do not really know anything about sysemd services.

At the point when sway now starts automatically I would normally log into tty1 with my username and password and then start sway using exec sway.

Hello @mschwaig !

The configuration is supposed to open a Sway directly on a given TTY with the specified user logged-in. (no interaction required). I first set up this process since I use full disk encryption, thus I already authenticate myself at boot by unlocking the system drive.

If sway starts with the default configuration, if you type “Meta+Enter” does it spawn a terminal ? If it doesn’t, check if alacritty and/or urxvt is installed.

Disclaimer : I never tested this on 19.09 since I’m only working on unstable.

Let me know if that helped ! I’m currently a bit short (and I’m not using Sway anymore) but I will check it as soon as I can ! :slight_smile:

Hi @Elyhaka!

Thanks for the clarification.

Meta+Enter and Meta+d both do not work. I have st configured as my terminal and all of that works as long as I manually start sway. I also checked if installing alacritty and urxvt changes anything for some reason, but it does not. I am on 19.09.

Thanks for your help, maybe I will just have to manually start for now and wait until someone pushes the systemd integration into NixOS a little further.

I found out some more information about whats going on by using the loginctl command to inspect logged in sessions.

Sway cannot resolve any of the commands that are referenced in my config file.

Details and how I found this out

First I used loginctl to get the list of active sessions. I added some comments about each one.

mschwaig@nixos ~> loginctl
SESSION  UID USER     SEAT  TTY  
      4 1000 mschwaig       pts/0     # the ssh session that I use to inspect the others
      5 1000 mschwaig seat0 tty1      # the manually started session
      7 1000 mschwaig seat0 tty7      # the session launched by systemd

3 sessions listed.

Then I used loginctl session-status {SESSION} to look for differences.

This is the manually started one that’s working.

mschwaig@nixos ~> loginctl session-status 5
5 - mschwaig (1000)
           Since: Sat 2020-03-21 20:35:42 CET; 8min ago
          Leader: 1327 (login)
            Seat: seat0; vc1
             TTY: tty1
         Service: login; type tty; class user
           State: online
            Unit: session-5.scope
                  ├─1327 /nix/store/hq1lrqxl46xsj92x8261j4bmar03p2cs-shadow-4.6/bin/login --
                  ├─1690 /nix/store/5sqv5c2785hvli2f3jqvhyqz26s8c5kn-dbus-1.12.16/bin/dbus-run-session /nix/store/8hvb7ahsib46h4y8gf9b8s4k5b9linz8-sway-1.2/b>
                  ├─1872 dbus-daemon --nofork --print-address 4 --session
                  ├─1873 /nix/store/8hvb7ahsib46h4y8gf9b8s4k5b9linz8-sway-1.2/bin/sway
                  ├─1877 swaybar -b bar-0
                  ├─1880 sh -c while date +'%Y-%m-%d %l:%M:%S %p'; do sleep 1; done
                  └─3780 sleep 1

Mar 21 20:35:42 nixos systemd[1]: Started Session 5 of user mschwaig.

And the broken one started by systemd:

mschwaig@nixos ~> loginctl session-status 7
7 - mschwaig (1000)
           Since: Sat 2020-03-21 20:39:48 CET; 32s ago
          Leader: 3130 (.sway-wrapped)
            Seat: seat0; vc7
             TTY: tty7
         Service: login; type tty; class user
           State: active
            Unit: session-7.scope
                  ├─3130 /nix/store/8hvb7ahsib46h4y8gf9b8s4k5b9linz8-sway-1.2/bin/sway
                  ├─3132 (sd-pam)
                  ├─3135 /nix/store/5sqv5c2785hvli2f3jqvhyqz26s8c5kn-dbus-1.12.16/bin/dbus-launch --exit-with-session /nix/store/8hvb7ahsib46h4y8gf9b8s4k5b9l>
                  └─3136 /run/current-system/sw/bin/dbus-daemon --syslog --fork --print-pid 4 --print-address 6 --session

Mar 21 20:39:48 nixos systemd[1]: Started Session 7 of user mschwaig.
Mar 21 20:39:53 nixos dbus-launch[3130]: /bin/sh: dmenu_path: command not found
Mar 21 20:39:53 nixos dbus-launch[3130]: /bin/sh: dmenu: command not found
Mar 21 20:39:53 nixos dbus-launch[3130]: xargs: swaymsg: No such file or directory

Now from this and looking at my ~/.config/sway/config file I can see that the broken session cannot resoslve any of the commands that are referenced in my config file for sway.

I just tried switching from 19.09 to unstable and now it just works. :partying_face:

Thanks for your help, @Elyhaka!

:tada: Glad to hear !

If you want to stay (as close as possible) to a stable version, you can try to go on the 20.03 :slight_smile:.