Starting X11 in VNC, some environment variables are missing

I’m trying to set up system service, starting vnc for a selected user using this snippet from my nixos module:


systemd.services = 
  let cfg = [
    {
      user = "user";
      interface = "0.0.0.0";
    }
  ]
  in lib.attrsets.mergeAttrsList (lib.lists.imap1 (i:
    { user, interface, ... }: {
      "vncserver-${user}" = let home = config.users.users.${user}.home;
      in {
        environment = {
          PATH = pkgs.lib.mkForce
            "/run/wrappers/bin:${home}/.nix-profile/bin:/nix/profile/bin:${home}/.local/state/nix/profile/bin:/etc/profiles/per-user/user/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin";
          XDG_DATA_DIRS = lib.makeSearchPath "share" ([
            config.services.displayManager.sessionData.desktops
            pkgs.gnome.gnome-control-center # for accessibility icon
            pkgs.gnome.adwaita-icon-theme
            pkgs.hicolor-icon-theme # empty icon theme as a base
          ] ++ config.environment.systemPackages);
          XDG_RUNTIME_DIR = "/run/user/${toString config.users.users.${user}.uid}";
        };
        unitConfig = {
          Description = "Remote desktop service (VNC) :${toString i}";
          After = "syslog.target network.target systemd-user-sessions.service";
        };

        serviceConfig = {
          Type = "simple";
          User = "${user}";
          WorkingDirectory = "${home}";
          Restart = "always";
        };

        wantedBy = [ "multi-user.target" ];

        script = ''
          export DISPLAY=:${toString i}
          # prepare Xauthority file
          cookie=$(mcookie)
          xauth -f $HOME/.Xauthority source - <<EOF
          add $HOSTNAME$DISPLAY . $cookie
          add $HOSTNAME/unix$DISPLAY . $cookie
          EOF

          exec Xvnc $DISPLAY -auth $HOME/.Xauthority -desktop $HOSTNAME$DISPLAY -interface ${interface} -PasswordFile=$HOME/.config/tigervnc/passwd & xvncPid=$!
          ($HOME/.config/tigervnc/xstartup &>$HOME/.local/state/desktop.log; sleep 0.5; kill $xvncPid )& dePid=$!
          trap "kill $xfcePid; kill $xvncPid" TERM
          wait $xfcePid
        '';
      };
    }) cfg);

Contents of ~/.config/tigervnc/xstartup (generated):

exec /nix/store/lzvpirsppkfs76m6hapaq99s30fk3zjh-xfce4-session-4.18.3/bin/startxfce4

As a result, some environment variables are missing when comparing normal login and using vnc. For example, XDG_DATA_DIRS, GIO_EXTRA_MODULES contain much less directories, XDG_SESSION_CLASS, XDG_SESSION_DESKTOP are missing etc. It’s not only limited to XDG variables.

I can add a variable one by one, but it’s cumbersome and I feel that I’ll miss any new entries which will come in the future.

How can I start the X11 properly, replicating what lightdm does?

I think I’ve solved it. The display-manager.service is using this wrapper script to start desktop environment: nixpkgs/nixos/modules/services/x11/display-managers/default.nix at 080166c15633801df010977d9d7474b4a6c549d7 · NixOS/nixpkgs · GitHub

So in my case I need to start desktop environment using that script. So the contents of ~/.config/tigervnc/xstartup need to be:

${config.services.displayManager.sessionData.wrapper} ${lib.getExe' pkgs.xfce.xfce4-session "startxfce4"}

Not all the variables seem to be exactly the same, but the icons came back.