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

Running Desktop Env from proot (and DeX) #14039

Open
rudolphpienaar opened this issue Nov 21, 2022 · 7 comments
Open

Running Desktop Env from proot (and DeX) #14039

rudolphpienaar opened this issue Nov 21, 2022 · 7 comments
Labels
discussion x11 Issue is related to stuff requiring X11 environment or x11-packages.

Comments

@rudolphpienaar
Copy link

rudolphpienaar commented Nov 21, 2022

Abstract

Running a Desktop Env (DE) from within a termux proot in termux-x11 is desirable for many reasons. A proot will often be a more complete and "standard" Linux experience offering many more X11 applications than currently available in stock termux. Currently, however, this is problematic, as in my anecdotal experience I can't seem to run any DE from the proot, with success only if the termux host runs the DE. While a closed older issue did address running a DE from a proot, the solution no longer seems to be working.

Introduction

I've been trying (and failing) to successfully and reliably run a desktop env (DE) from within a proot-login. The reasons for proot-login DE ability are of course obvious -- a proot, being a more "conventional" Linux installation, offers considerably more DE envs and X11 applications than the standard termux env which only seems to have xfce4 and very few X11 apps (no firefox / thunderbird for example).

Sources

There are several documented solutions (none of which work for me as of late November 2022). Most relevant is the closed issue #138 but there are others -- udroid and various postings on reddit of varying utility here, I thought to open an issue here as a central place to keep track.

Environment

My setup is a Samsung Galaxy Z Fold 3, hooked up to an external monitor that triggers DeX. By default, any attempt at running termux-x11 or even some vnc type solution will almost always die within a few minutes being killed by Android. A hack solution seems to be setting max_phantom_processes -- see here and a solution here.

Previously proposed solution (not fully working for reasons unknown)

The core solution offered entails running/starting the termux-x11 Xwayland server in termux, and then sharing the relevant UNIX domain socket that is created in the termux $XDG_RUNTIME_DIR with the proot environment. In this manner, X11 apps in the proot can communicate with the Xwayland server instance.

In summary, in termux:

# In termux, set some vars...
# First, enable PulseAudio over Network
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1

# Prepare termux-x11 session
export XDG_RUNTIME_DIR=${TMPDIR}
termux-x11 :0 >/dev/null &

Now, enter the proot. Critically, note the --shared-tmp flag -- this is the magic sauce that allows the proot to share the same tmp directory ($XDG_RUNTIME_DIR) between the host and proot):

proot-distro login archlinux --shared-tmp 

Obviously, in my above example, my proot is an Arch linux chroot. Now, in this proot, assuming I have installed xfce4, the concept is to simply run xfce4-session and it should connect to the Xwayland server running in the host termux:

# This is in the proot. Here, $TMPDIR is /tmp...
# Also, this doesn't work
export XDG_RUNTIME_DIR=${TMPDIR}
env DISPLAY=:0 xfce4-session

(Note that some documentation erroneously indicates to run env DISPLAY=:0 startxfce4 -- however startxfce4 is a wrapper that tries to start an X server. Since an Xserver is already running in the host, this will result in an X server already running error).

Observed behaviour

I haven't been able to ever get the xfce4 (or any other DE) started in the proot to connect to the host Xwayland. What has worked for me is to run/start the DE in termux. Since termux only seems to offer xfce4 this is the only environment possible:

# This works, but only when run in the termux host
export XDG_RUNTIME_DIR=${TMPDIR}
env DISPLAY=:0 xfce4-session

Now, in the xfce4 DE, I can open a terminal and within that terminal I can proot-distro login archlinux --shared-tmp. At this point, I'm in a DE in the termux, but my terminal is now open onto the proot. I can then

export DISPLAY=:0
firefox &

which runs the firefox from the proot and displays on the termux Xwayland.

Oddities?

Any application in the proot that wants to communicate on dbus seems to fail. Luckily firefox doesn't use dbus but many other modern apps do (so gnome-terminal won't work, but terminator will). This limits the space of available applications considerably, but does hint at the fact that dbus communication might be the issue?

I have tried many permutations, without success:

  • setting my user UID in the proot to be the same UID as in the termux host. Doesn't help
  • tried various chmod 777 /tmp/dbus* to see if I can speak to dbus from the proot. Doesn't help

Other annoyances

  • Often the termux-x11 "window" opens full screen (without the DeX window decoration), but the colors seem "washed out". I can close the window and reopen it, and the colors seem fine, but the annoying Dex top window bar returns.
  • The termux-x11 always seems to have a very annoying button bar at the bottom with Esc, and what looks like cursor directions. I can't seem to get rid of this and it is very annoying.
@ShapeShifter499
Copy link

ShapeShifter499 commented Dec 21, 2022

Fold 4 user here. I have debian and xfce4 working just fine here. Let me run though my initial setup.

Disabling Android 12+ background process limit

You must disable the background limit otherwise termux will crash eventually and it will be near impossible to run a proot distro at all let alone x11 apps.

At the time of writing I have Android 12 and One UI 4.1.1. You can disable this background process limit quite easily even without a dedicated computer to run android-tools on with a adb settings flag. More information about that can be found from here: https://github.com/agnostic-apollo/Android-Docs/blob/master/en/docs/apps/processes/phantom-cached-and-empty-processes.md#commands-to-disable-phantom-process-killing-and-tldr

The flag persists across reboots but you may need to undo the flag later for system updates by setting it back to true.

The standard way

Would be to connect your device to a computer with android-tools installed, notably adb. Enable developer settings on your device and then enable debugging in the developer options. Running the following adb command below.

adb shell settings put global settings_enable_monitor_phantom_procs false

From within termux

But you can set the process limit flag from inside termux itself using adb wireless. Make sure you have only settings app and termux running, close everything else first.

In termux you will want to install 'android-tools' and 'fakeroot'
pkg install android-tools fakeroot

Then enable wireless debugging in developer menu, this is only Android 11+, you can get a pair code and everything. Settings > Developer Options > Wireless Debugging > Pair Code.

You have to tap next to the switch to get into the menu where it will allow you to pair with a code once the option is turned on.

Take note of the pair code and the port code it gives you in the menu, back in termux you'll need to run the following to connect first.

ANDROID_NO_USE_FWMARK_CLIENT=1 fakeroot adb pair localhost:<port> <pincode>
then after the pair go back and see the new port number to connect to
ANDROID_NO_USE_FWMARK_CLIENT=1 fakeroot adb connect localhost:<port>

Once connected you can then disable the flag with
ANDROID_NO_USE_FWMARK_CLIENT=1 fakeroot adb shell settings put global settings_enable_monitor_phantom_procs false

ANDROID_NO_USE_FWMARK_CLIENT=1 fakeroot prefix is needed on Samsung devices, discused here: #7946

Starting from a freshly installed termux environment

OPTIONAL: you can clear a existing termux install by clearing the termux app data in android settings but backup what you want first.

Installing what's needed

Ensure termux is up to date

pkg update
pkg upgrade

Install termux-x11, follow the guide presented at the main page https://github.com/termux/termux-x11

Extra note, be sure to install the included .deb file in the termux system, not under the proot.
pkg install ./path/to/.deb

Setting up proot

You'll want to install the utility proot-distro, it makes this process so much easier.
pkg install proot-distro

I won't go into everything with proot-distro here but I did the following personally. This is just a idea of what to do, there maybe extra packages you want to install or even a different distro you might want to use. This is just literally what I did for my setup.

proot-distro install debian
proot-distro login debian

From inside the new proot shell
apt update
apt upgrade
apt install task-xfce-desktop
apt install dbus-launch (xfce4 threw some errors without)
apt install sudo
usermod -m <new_user> (-m flag to create a home directory for my user. <new_user> is just a stand in, feel free to use whatever username you want)
EDITOR=nano visudo (I like nano as a text editor and visudo to add my user to sudo so I can run some elevated processes, while proot isn't true root, not all apps will want to run as the "proot" root user)
exit (leave the proot, you don't need to be in the proot to start it in the termux-x11 session)

Launching everything

Firstly you'll want to make sure allow-external-apps = true is set in ~/.termux/termux.properties outside the proot so that the termux-x11 companion app can access termux and processes. Only allow trusted apps after this point as any app can ask for permission to use your temux setup.

I find that you might have to force close the termux-x11 companion app before or after each session to get things to launch correctly.

I basically barrowed the script that @waldhef posted here termux/termux-x11#138 (comment)

But I came across this for dbus, proot-distro login <some-distro> --bind /dev/null:/proc/sys/kernel/cap_last_cap from here: https://gist.github.com/SaicharanKandukuri/20e66e816a8b2c3ea9d3f7657f09f807

If you want audio to work you need to install pulseaudio inside of termux
pkg install pulseaudio

My updated script that works for me is below

#!/data/data/com.termux/files/usr/bin/bash

# Enable PulseAudio over Network
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1

# Prepare termux-x11 session
export XDG_RUNTIME_DIR=${TMPDIR}
termux-x11 :0 >/dev/null &

# Wait a bit until termux-x11 gets started.
sleep 3

# Login in PRoot Environment. Do some initialization for PulseAudio, /tmp directory
# and run xfce as your non-root user.
# See also: https://github.com/termux/proot-distro
# Argument -- acts as terminator of proot-distro login options processing.
# All arguments behind it would not be treated as options of PRoot Distro.
proot-distro login debian --shared-tmp --bind /dev/null:/proc/sys/kernel/cap_last_cap -- /bin/bash -c  'export PULSE_SERVER=127.0.0.1 && export XDG_RUNTIME_DIR=${TMPDIR} && su - <user> -c "env DISPLAY=:0 xfce4-session"'

exit 0

Save as whatever you'd like, I called my file start-xfce.sh. Swap out with the user you created earlier, if you didn't you will probably want to.

Make sure to give the file inside termux executable permissions.
chmod +x start-xfce.sh

and then launch it.

./start-xfce.sh

As mentioned earlier, make sure termux-x11 companion app isn't running before running the script, it may fail if the termux-x11 app isn't running fresh.

When you are finished with a session and want to close out of everything. Log out in your desktop environment, force close the termux-x11 app, and type exit in the termux shell. This will ensure everything is properly closed out and none of the termux or proot processes are taking up RAM or system resources.

@ShapeShifter499
Copy link

ShapeShifter499 commented Dec 22, 2022

Screen_Recording_20221221_200022_TermuxX11.mp4

Video of it working

@twaik
Copy link
Member

twaik commented Dec 22, 2022

Not related to termux-x11

@twaik twaik transferred this issue from termux/termux-x11 Dec 22, 2022
@xtkoba

This comment was marked as outdated.

@xtkoba xtkoba closed this as completed Dec 22, 2022
@xtkoba xtkoba reopened this Dec 24, 2022
@xtkoba xtkoba added the x11 Issue is related to stuff requiring X11 environment or x11-packages. label Dec 24, 2022
@rudolphpienaar
Copy link
Author

This is amazing @ShapeShifter499 ! I should update my original posting to reflect this perhaps, but your guide essentially made my whole day/month/year.

Some notes/comments? I can't seem to reliably get adb shell access from within termux. I can pair. Sometimes it connects. It never seems though to work beyond that . For setting the settings_enable_monitor_phantom_procs I use the Play Store app, LADB.

Another fun "struggle"... getting any DE other than xfce4 to work. Most of the usual suspects, GNOME, KDE, and derivativess complaing from the get go about dbus issues. I have tried a couple of stabs-in-the-dark -- setting my UID within proot to be the same as the host termux (didn't help).

Have you had success with any DE other than xfce4?

@ShapeShifter499
Copy link

ShapeShifter499 commented Jan 2, 2023

Some notes/comments? I can't seem to reliably get adb shell access from within termux. I can pair. Sometimes it connects. It never seems though to work beyond that . For setting the settings_enable_monitor_phantom_procs I use the Play Store app, LADB.

I had not heard of LADB so I'm glad that worked out for you.

Have you had success with any DE other than xfce4?

The only other desktop environment I've tried with so far was Gnome. I couldn't get it working and it is possible it might not work unmodified as it's starting to rely more and more on SystemD. SystemD unmodified can't run in proot as it wants to start as PID 1. I've seen some efforts online to get Gnome working without SystemD but I've not tried those yet.

But for the most part Gnome apps and others should work fine in XFCE as long as they don't rely on SystemD or dbus. For example I was using gThumb and gscan2pdf to help me compile documents I scanned or took photos of into pdf files earlier just fine.

A cool thing to note. If you have set up android file permissions for termux and run termux-setup-storage, proot-distro will by default bind the default android storage location to /sdcard inside the proot session allowing the proot distro to read-write files to your normal android storage location. To add to that however, in my experience some applications could read files but not write for some reason. For example I couldn't write the compiled pdf with gscan2pdf outside the proot but I could use thunar to manually move the generated pdfs out of /home to the /sdcard directory and gThumb was able to view and delete the photos I pointed it to. I'm not actually sure why that was but something to keep in mind if you experience any read-write issues to the simulated /sdcard directory.

That auto bind feature for the storage can be disabled by passing --isolated as mentioned at https://github.com/termux/proot-distro

@jsommr
Copy link

jsommr commented Sep 7, 2023

Have the same issue with running XFCE on a Surface Go 2/BlissOS with Debian proot. Can also render Firefox in termux-x11 (with poor performance), but when running DISPLAY=:0 xfce4-session --display=:0, termux-x11 is just a black screen and a cursor. There are no warnings and no errors, the command just hangs until SIGINT'ed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion x11 Issue is related to stuff requiring X11 environment or x11-packages.
Projects
None yet
Development

No branches or pull requests

5 participants