nix-community / nix-on-droid

Nix-enabled environment for your Android device. [maintainers=@t184256,@Gerschtli]
https://nix-on-droid.unboiled.info
MIT License
1.16k stars 65 forks source link

Install onto vanilla termux? #257

Open KoviRobi opened 1 year ago

KoviRobi commented 1 year ago

Hi,

Is it possible to install onto vanilla termux? The main reason I would like to do so is for using solarized colorschemes (if I do Nix-on-droid -> [hold] -> More... -> Style, it says "The Termux:Styling Plugin App is not installed", despite it being installed (via F-Droid).

I have Nix v0.118.0_v0.3.1.

Probably relevant:

I think there might be other upsides to installing onto termux:

I think the install is done by

https://github.com/t184256/nix-on-droid-app/blob/f55587fc9eed7d8a0515f05f0b40a8b0c72c7c66/app/src/main/java/com/termux/app/TermuxInstaller.java#L149-L285

which as I understand unzips, creates symlinks, chmods executables. But not sure what it does after to start the whole thing. Does it just execute bin/login?

KoviRobi commented 1 year ago

Ok I am getting there:

t184256 commented 1 year ago
  1. Yes: https://github.com/t184256/nix-in-termux
  2. For colorschemes alone, you don't have to, just copy one over from Termux's .termux/colors.properties.
KoviRobi commented 1 year ago

Thanks! I did spot that, but I had the invalid ELF header with that, and I saw it is archived so I'd try this, but in the end I had it here too. I did manage to solve that though:

https://github.com/KoviRobi/nix-on-droid/commit/95c17519f830ff2dba58ec5aac985065386755d7

So now the steps are:

[pc:~/nix-on-droid]$ NIX_ON_DROID_CHANNEL_URL=https://github.com/KoviRobi/nix-on-droid/archive/refs/heads/install-on-vanilla-termux.tar.gz \
                     NIX_ON_DROID_FLAKE_URL=github:KoviRobi/nix-on-droid/install-on-vanilla-termux \
                     nix build --impure .#bootstrapZip
[android:~]$ scp pc:nix-on-droid/result/bootstrap-aarch64.tar.gz .
[android:~]$ mkdir ../nix/
[android:~]$ tar xf ~/bootstrap-aarch64.tar.gz -C ../nix/
[android:~]$ chmod +w -R ../nix/
[android:~]$ ../nix/bin/login

which works for me :) Only one thing remaining is after install it downgrades nix to 2.11.1 which has this issue https://github.com/t184256/nix-on-droid/issues/213

I also need to unset SHELL so it's not trying to use the android shell.

Gerschtli commented 1 year ago

@KoviRobi That's pretty neat. Is the termux integration with its addons and the am script working fine?

KoviRobi commented 1 year ago

So there's some gotcha's I haven't figured out:

The -b /data/data/com.termux/files/nix/bin:/bin part of proot (from nix/bin/login) makes termux programs break, e.g.

~ $ /data/data/com.termux/files/usr/bin/bash
-bash: /data/data/com.termux/files/usr/bin/bash: cannot execute: required file not found

which in turn breaks stuff because $SHELL in termux by default is set to said bash, e.g.

~ $ ../nix/bin/login
Welcome to Nix-on-Droid!
If nothing works, open an issue at https://github.com/t184256/nix-on-droid/issues or try the rescue shell.
~ $ nix shell nixpkgs#iproute2
error: unable to execute '/data/data/com.termux/files/usr/bin/bash': No such file or directory
~ $ unset SHELL
~ $ nix shell nixpkgs#iproute2
bash-5.2$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
[...]

If instead I remove/comment out (taking care of bash not liking commenting out lines with line-continuaton \)

~ $ ../nix/bin/login
proot warning: can't sanitize binding "/linkerconfig": Permission denied
Welcome to Nix-on-Droid!
If nothing works, open an issue at https://github.com/t184256/nix-on-droid/issues or try the rescue shell.
~ $ $SHELL
# works
~ $ /data/data/com.termux/files/usr/bin/bash
# works

But am doesn't yet work

~ $ ../nix/bin/login
Welcome to Nix-on-Droid!
If nothing works, open an issue at https://github.com/t184256/nix-on-droid/issues or try the rescue shell.
~ $ am
-bash: /data/data/com.termux/files/usr/bin/am: cannot execute: required file not found
~ $ strace am
[...]
openat(AT_FDCWD, "/system/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/odm/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/vendor/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[...]
write(2, "library \"libnativeloader.so\" not"..., 65library "libnativeloader.so" not found: needed by main executable) = 65
[...]
exit_group(1)                           = ?
+++ exited with 1 +++

compared with vanilla termux

~ $ strace am
openat(AT_FDCWD, "/system/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/system_ext/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/apex/com.android.art/lib64/libnativeloader.so", O_RDONLY|O_CLOEXEC) = 9
readlinkat(AT_FDCWD, "/proc/self/fd/9", "/apex/com.android.art/lib64/libn"..., 4096) = 46

I believe this is due to

newfstatat(AT_FDCWD, "/linkerconfig/ld.config.txt", {st_mode=S_IFREG|0644, st_size=71632, ...}, 0) = 0
openat(AT_FDCWD, "/linkerconfig/ld.config.txt", O_RDONLY|O_NOFOLLOW|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=71632, ...}) = 0
mmap(NULL, 73728, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7c05926000
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, 0x7c05926000, 73728, "bionic_alloc_lob") = 0
read(3, "dir.system = /system/bin/\ndir.sy"..., 1024) = 1024
[...]
openat(AT_FDCWD, "/dev/__properties__/u:object_r:vndk_prop:s0", O_RDONLY|O_NOFOLLOW|O_CLOEXEC) = 3

but doing -b /linkerconfig:/linkerconfig doesn't seem to work. More experimentation needed

KoviRobi commented 1 year ago

But the termux addons seem to work (tried termux:style, and termux:float anyway)

KoviRobi commented 1 year ago

I will probably pursue this further still, it doesn't feel like it's far now :)

KoviRobi commented 1 year ago

Success! I had to remove the following lines from the proot script

 -b /data/data/com.termux/files/nix/bin:/bin \
 -b /data/data/com.termux/files/nix/etc:/etc \

and instead of using the proot-static, I used the proot from termux.

And I could do the following:

~ $ ../nix/bin/login
Welcome to Nix-on-Droid!
If nothing works, open an issue at https://github.com/t184256/nix-on-droid/issues or try the rescue shell.
~ $ am broadcast --user 0 \
                 --es com.termux.app.reload_style storage \
                 -a com.termux.app.reload_style com.termux

and it asked for permissions to access my storage :)

t184256 commented 1 year ago

and instead of using the proot-static, I used the proot from termux.

Huh. Never thought that's what I've been missing. Our proot is compiled from their sources.

t184256 commented 1 year ago

Tried not mounting over /bin and /etc, that wasn't enough indeed. Will try with newest proot-termux patches next time.

KoviRobi commented 1 year ago

I should also say not mounting /etc means nix has problems due to lack of resolv.conf (I think) so I need to look into that too, because I want both to work without causing problems to the other one

t184256 commented 1 year ago

Yeah, I realize that, I just hope it'll only need some linker configuration or something equally trivial.

t184256 commented 1 year ago

doing -b /linkerconfig:/linkerconfig doesn't seem to work

https://github.com/termux/proot/commit/0fdcda1d302dd05c175f250f2dd06c90c4e255cb is the right -b /linkerconfig

t184256 commented 1 year ago

~ $ am broadcast

which am was it, /system/bin/am, /bin/am, or termux's special am?

KoviRobi commented 1 year ago
~ $ nix run --no-net nixpkgs#bashInteractive
bash-5.2$ am broadcast --user 0 --es com.termux.app.reload_style storage -a com.termux.app.reload_style com.termux
Broadcasting: Intent { act=com.termux.app.reload_style pkg=com.termux (has extras) }
Broadcast completed: result=0
bash-5.2$ command -v am
/data/data/com.termux/files/usr/bin/am
myguidingstar commented 1 year ago

@t184256 @KoviRobi Has anyone tried termux's very own proot-distro? It seems to allow adding custom distro by providing a tarball https://github.com/termux/proot-distro#adding-distribution

tcely commented 11 months ago

@t184256 @KoviRobi Has anyone tried termux's very own proot-distro? It seems to allow adding custom distro by providing a tarball https://github.com/termux/proot-distro#adding-distribution

I'd like to see nix-on-droid have a configuration that I could just add to Termux.

https://github.com/termux/proot-distro/issues/215#issuecomment-1397017046

It looks like they went wrong by trying to support multiple users (with the daemon) but a single user system would make more sense for Termux and Android.

SeerLite commented 4 months ago

@KoviRobi what ended up missing? Would following your instructions in Termux give a working Nix setup?

KoviRobi commented 4 months ago

I can't remember exactly, it was a weekend project kind of thing which ran out of steam because even with using a monitor for my phone, and plugging in a keyboard, my phone wasn't ready to just replace my laptop (when travelling).

But also my motivation for this as opposed to the nix app was terminal styling, which turned out you can manually edit the .termux/colors.properties.

You are welcome to try and maybe you can fix the last couple of issue if you want