Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Services requiring WAYLAND_DISPLAY cannot access it with Niri on NixOS #40

Closed
e-tho opened this issue Sep 9, 2024 · 21 comments
Closed

Comments

@e-tho
Copy link

e-tho commented Sep 9, 2024

I'm trying to set up a session for Niri with the NixOS module, but no matter what I do, services that require WAYLAND_DISPLAY fail to start properly.

Here’s what I’ve tried so far:

  • Binding services to [email protected] or [email protected]
  • Starting the session with Niri's --session flag
  • Starting the session from a TTY rather than using a Display Manager
  • Resetting failed user services via systemctl --user reset-failed && /run/current-system/sw/bin/uwsm finalize NIRI_SOCKET in the Niri configuration’s spawn-at-startup
  • Adding a Niri plugin through an overlay on the uwsm package with the following content:
#!/bin/false

quirks_niri() {
    if [ "${__WM_DESKTOP_NAMES_EXCLUSIVE__}" != "true" ]; then
        case "A:${XDG_CURRENT_DESKTOP}:Z" in
        *:niri:*) true ;;
        *) export XDG_CURRENT_DESKTOP="${XDG_CURRENT_DESKTOP}:niri" ;;
        esac
    fi

    UWSM_FINALIZE_VARNAMES="${UWSM_FINALIZE_VARNAMES}${UWSM_FINALIZE_VARNAMES+: }NIRI_SOCKET XCURSOR_SIZE XCURSOR_THEME"
    export UWSM_FINALIZE_VARNAMES
}

Logs:

╰─ sudo journalctl -xb | rg uwsm
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Preparing environment for niri...
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Loading plugin "/run/current-system/sw/share/uwsm/plugins/niri.sh"
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Applying quirks for "niri"
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Exporting variables to systemd user manager:
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   DOCKER_HOST
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   EDITOR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   GTK_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   INFOPATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LESSKEYIN_SYSTEM
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LESSOPEN
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LIBEXEC_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LIBVA_DRIVER_NAME
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIXPKGS_CONFIG
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_PROFILES
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_USER_PROFILE_DIR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   PAGER
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   QTWEBKIT_PLUGIN_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   SSH_ASKPASS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   TERM
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   TERMINFO_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   UWSM_FINALIZE_VARNAMES
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   VDPAU_DRIVER
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XCURSOR_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XCURSOR_SIZE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_BACKEND
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_CONFIG_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_CURRENT_DESKTOP
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_DATA_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_MENU_PREFIX
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_DESKTOP
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_ID
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_TYPE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_VTNR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   __ETC_PROFILE_DONE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   __NIXOS_SET_ENVIRONMENT_DONE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Variables marked for cleanup from systemd user manager on stop:
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   DISPLAY
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   DOCKER_HOST
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   EDITOR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   GTK_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   INFOPATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LANG
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LESSKEYIN_SYSTEM
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LESSOPEN
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LIBEXEC_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   LIBVA_DRIVER_NAME
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIXPKGS_CONFIG
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_PROFILES
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   NIX_USER_PROFILE_DIR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   PAGER
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   QTWEBKIT_PLUGIN_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   SSH_ASKPASS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   TERM
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   TERMINFO_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   UWSM_FINALIZE_VARNAMES
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   VDPAU_DRIVER
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   WAYLAND_DISPLAY
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XCURSOR_PATH
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XCURSOR_SIZE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XCURSOR_THEME
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_BACKEND
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_CONFIG_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_CURRENT_DESKTOP
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_DATA_DIRS
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_MENU_PREFIX
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_DESKTOP
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_ID
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_SESSION_TYPE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   XDG_VTNR
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   __ETC_PROFILE_DONE
Sep 09 21:35:16 nixos uwsm_env-preloader[1973]:   __NIXOS_SET_ENVIRONMENT_DONE
Sep 09 21:35:16 nixos uwsm_niri[2019]: Starting: /run/current-system/sw/bin/niri...
Sep 09 21:35:16 nixos uwsm_niri[2019]: 2024-09-09T19:35:16.316404Z  INFO niri: starting version unstable 2024-09-09 (commit 96083847fb8776ca4f0ed613148e2e6540690cb3)
Sep 09 21:35:16 nixos uwsm_niri[2019]: 2024-09-09T19:35:16.323688Z DEBUG niri_config: loaded config from "/home/etho/.config/niri/config.kdl"
Sep 09 21:35:16 nixos uwsm_niri[2019]: 2024-09-09T19:35:16.772047Z  INFO niri::backend::tty: using as the render node: "/dev/dri/renderD128"
Sep 09 21:35:16 nixos uwsm_niri[2019]: 2024-09-09T19:35:16.907697Z DEBUG niri::backend::tty: device added: 57857 "/dev/dri/card1"
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.114594Z DEBUG niri::backend::tty: this is the primary node
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.114609Z DEBUG niri::backend::tty: this is the primary render node
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.138881Z DEBUG niri::backend::tty: device changed: 57857
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.143393Z DEBUG niri::backend::tty: connecting connector: DP-1
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.143748Z DEBUG niri::backend::tty: picking mode: Mode { name: "2560x1440", clock: 645000, size: (2560, 1440), hsync: (2568, 2600, 2640), vsync: (1446, 1454, 1480), hskew: 0, vscan: 0, vrefresh: 165, mode_type: ModeTypeFlags(DRIVER) }
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.143806Z DEBUG niri::backend::tty: set max bpc to 8
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.147594Z DEBUG niri::niri: putting output DP-1 at x=0 y=0
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.147660Z  INFO niri: listening on Wayland socket: wayland-1
Sep 09 21:35:17 nixos uwsm_niri[2019]: 2024-09-09T19:35:17.147666Z  INFO niri: IPC listening on: /run/user/1000/niri.wayland-1.2019.sock
╰─ systemctl --user status swayidle.service
○ swayidle.service - Idle manager for Wayland
     Loaded: loaded (/home/etho/.config/systemd/user/swayidle.service; enabled; preset: enabled)
     Active: inactive (dead)
  Condition: start condition unmet at Mon 2024-09-09 21:35:15 CEST; 41s ago
             └─ ConditionEnvironment=WAYLAND_DISPLAY was not met
       Docs: man:swayidle(1)

Sep 09 21:35:15 nixos systemd[1851]: Idle manager for Wayland was skipped because of an unmet condition check (ConditionEnvironment=WAYLAND_DISPLAY).
@Vladimir-csp
Copy link
Owner

Most likely your swayidle.service unit lacks proper ordering. It should either be ordered After=graphical-session.target (good idea for any unit requiring WAYLAND_DISPLAY), or assigned to one of uwsm's custom *-graphical.slice units via Slice= directive (which does the same, plus gracefully ordered shutdown). See example-units dir.

Plus your log does not include results of uwsm finalize execution by niri, is it configured?

@Pacman99
Copy link

Pacman99 commented Sep 9, 2024

I'm getting a similar issue and I don't think its because of an ordering issue. Waybar is giving an error that it cannot open the display when I restart it after I'm in niri. I've also set After=graphical-session.target and Slice=app-graphical.slice in the waybar.service.

I believe niri does something similar to hyprland where it notifies systemd of startup. So this could be a similar issue to #14.

@Vladimir-csp
Copy link
Owner

Vladimir-csp commented Sep 10, 2024

Status of swayidle.service:

start condition unmet at Mon 2024-09-09 21:35:15

Which means it was started earlier than units of graphical-session-pre.target and graphical-session.target:

Sep 09 21:35:16 nixos uwsm_env-preloader[1973]: Preparing environment for niri...
...
Sep 09 21:35:16 nixos uwsm_niri[2019]: Starting: /run/current-system/sw/bin/niri...

@Vladimir-csp
Copy link
Owner

Plus niri exports WAYLAND_DISPLAY and notifies its readines (link). Which should make things work, although it will most likely trigger units startup before any additional vars are handled by uwsm finalize.

@Pacman99
Copy link

Something else I noticed is that when I run systemctl --user show-environment after starting niri with uwsm there is no WAYLAND_DISPLAY set. When I start niri with its own niri-session, that variable is set. This is why I was thinking there could be some issue with environment variables and not just service ordering.

@Vladimir-csp
Copy link
Owner

Please show configuration of swayidle.servie

@e-tho
Copy link
Author

e-tho commented Sep 10, 2024

Something else I noticed is that when I run systemctl --user show-environment after starting niri with uwsm there is no WAYLAND_DISPLAY set. When I start niri with its own niri-session, that variable is set. This is why I was thinking there could be some issue with environment variables and not just service ordering.

I've noticed that as well, systemctl --user reset-failed && /run/current-system/sw/bin/uwsm finalize works as a workaround for service ordering, but services that depend on WAYLAND_DISPLAY still don't start properly.

@Vladimir-csp
Copy link
Owner

Please show unit configurations of services that fail.

@e-tho
Copy link
Author

e-tho commented Sep 11, 2024

Here is the service unit for swayidle, generated by its Home Manager module:

# /home/etho/.config/systemd/user/swayidle.service
[Install]
WantedBy=graphical-session.target

[Service]
Environment=PATH=/nix/store/vpvy79k1qq02p1vyqjk6nb89gwhxqvyb-bash-5.2p32/bin
ExecStart=/nix/store/b11svzm1gjnb2qrgc56qybrmqk7444yl-swayidle-1.8.0/bin/swayidle -w timeout 300 '/nix/store/szxalsfcjw62lw4h6n31z4499kw5saia-check-audio-script || /run/current-system/sw/bin/niri msg action power-off-monitors' timeout 900 '/nix/store/szxalsfcjw62lw4h6n31z4499kw5saia-check-audio-script || /run/current-system/sw/bin/systemctl suspend-then-hibernate' before-sleep '/run/current-system/sw/bin/loginctl lock-session' lock /nix/store/yd7mixw0p2pq37bpv4i3048zicfjbwkq-swaylock-1.7.2/bin/swaylock
Restart=always
Type=simple

[Unit]
ConditionEnvironment=WAYLAND_DISPLAY
Description=Idle manager for Wayland
Documentation=man:swayidle(1)
PartOf=graphical-session.target

@Vladimir-csp
Copy link
Owner

It lacks ordering, which means it will fire up right away in any transaction that includes graphical-session.target startup. ConditionEnvironment like any Condition* just skips startup if false, it does not affect ordering.

Add After=graphical-session.target (to [Unit]) or Slice=app-graphical.slice (to [Service]) to fix it.

@Vladimir-csp Vladimir-csp closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2024
@Pacman99
Copy link

I had this issue even when setting After and Slice. I used the example waybar unit in this repo and waybar gave the error cannot open display:.

@e-tho
Copy link
Author

e-tho commented Sep 11, 2024

Add After=graphical-session.target (to [Unit]) or Slice=app-graphical.slice (to [Service]) to fix it.

This indeed fixes the service ordering, now Waybar starts properly without needing to run systemctl --user reset-failed with uwsm finalize.
However, the initial issue remains: swayidle still doesn't have access to the environment at the time of execution.

ConditionEnvironment=WAYLAND_DISPLAY was not met

Here’s how the service is declared in the Home Manager module: https://github.com/nix-community/home-manager/blob/8a1671642826633586d12ac3158e463c7a50a112/modules/services/swayidle.nix#L117

Could this be related to how the service environment is handled?

@Vladimir-csp
Copy link
Owner

Could it be that Home Manager module overwrites units? What if you add ordering to a drop-in?

It would probably be a good idea to patch the module with After=graphical-session.target irregardless of whether uwsm is used or not.

@Vladimir-csp
Copy link
Owner

I was wrong about slice ordering: it does not affect ordering of included units. On contrary, unit assigned to some *-graphical.slice but lacking After=graphical-session.target may trigger dependency cycle for some reason. So After=graphical-session.target is effectively mandatory for any graphical unit.

@e-tho
Copy link
Author

e-tho commented Sep 12, 2024

I've opened a PR to fix the swayidle module, but I noticed that similar services like waybar or hypridle were created with graphical-session-pre.target in the After option, which is also causing the service ordering issue with uwsm. What is the correct approach? Do these units need to be modified, or is the issue more likely related to how uwsm handles graphical-session-pre.target?

@Vladimir-csp
Copy link
Owner

To my knowledge compositors bind either directly or via an intermediate to graphical-session.target and are ordered After=graphical-session-pre.target, Before=graphical-session.target.
Some examples:

Waybar or hypridle, like any wayland apps, require compositor to run, so they should go After=graphical-session.target.

@e-tho
Copy link
Author

e-tho commented Sep 18, 2024

This issue should be reopened, as the initial problem persists: swayidle still can't start because it lacks access to WAYLAND_DISPLAY. Yet when I run systemctl --user show-environment, all necessary variables are printed, and manually restarting the service works fine.

This issue does not occur when using the niri-session wrapper. The environment is properly imported for user services, and swayidle starts without any issues. This confirms that the problem is tied to UWSM rather than the service itself or Home Manager.

@Brisingr05
Copy link

Brisingr05 commented Sep 18, 2024

This issue should be reopened, as the initial problem persists: swayidle still can't start because it lacks access to WAYLAND_DISPLAY. Yet when I run systemctl --user show-environment, all necessary variables are printed, and manually restarting the service works fine.

This issue does not occur when using the niri-session wrapper. The environment is properly imported for user services, and swayidle starts without any issues. This confirms that the problem is tied to UWSM rather than the service itself or Home Manager.

I've been using UWSM for a couple of days without issues (although IDK if this is the correct usage). Here are some code snippets:

NixOS config

{ config, lib, pkgs, ... }:

let
  # wrapper script for `binPath` since the option type is `path`
  niriSession = lib.getExe (
    pkgs.writeShellScriptBin "niriSession" ''
      ${lib.getExe config.programs.niri.package } --session
    ''
  );
in

{
  programs.uwsm = {
    enable = true;
    waylandCompositors = {
      niri = {
        prettyName = "niri";
        comment = "niri compositor managed by UWSM";
        binPath = niriSession;
      };
    };
  };
}

Home Manager config

{
  systemd.user.swayidle.Unit.After = [ "graphical-session.target" ];
}

@Vladimir-csp
Copy link
Owner

With v0.19.x graphical session is delayed until WAYLAND_DISPLAY actually appears in systemd activation environment. Check journal to see which comes after which. If swayidle start is initiated before graphical-session.target is reached, then it is most likely a misconfiguration in swayidle unit.

@e-tho
Copy link
Author

e-tho commented Sep 18, 2024

@Brisingr05, Passing --session did the trick, thanks !

I didn't think of trying again with this flag, as I assumed it wasn't necessary when using UWSM. Btw the binPath option of the UWSM module should be changed, unless there are limitations I'm not aware of that justify this choice, this type is really inconvenient for compositors that require using options.

@Vladimir-csp, Is running uwsm finalize still necessary with the latest changes?

@Vladimir-csp
Copy link
Owner

@Vladimir-csp, Is running uwsm finalize still necessary with the latest changes?

In general, if compositor puts WAYLAND_DISPLAY to systemd activation environment, then no. More info here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants