nix-community / nix-on-droid

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

ssh server #32

Closed mehlon closed 4 years ago

mehlon commented 4 years ago

It'd be nice to be able to access the Nix/Termux shell remotely. Can you add instructions how to do so? It's very easy on Termux, but simply installing nix-env -iA nixpkgs.openssh then running sshd doesn't work out of the box. https://wiki.termux.com/wiki/Remote_Access

t184256 commented 4 years ago

I didn't try this myself, but I recall someone reporting that it's possible:

https://logs.nix.samueldr.com/nixos-aarch64/2019-09-17 https://logs.nix.samueldr.com/nixos-aarch64/2019-09-18

Gerschtli commented 4 years ago

For now you can follow these steps to create a working sshd server:

  1. Install openssh via nix-env or nix-on-droid or what ever.
  2. Create host key: ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N ""
  3. Create sshd_config config file with following content:
    HostKey /absolute/path/to/ssh_host_rsa_key
    Port 8022
  4. Create ~/.ssh/authorized_keys and add a public key
  5. Run sshd with absolute path like: /data/data/com.termux.nix/files/home/.nix-profile/bin/sshd -f /path/to/sshd_config
  6. Connect to your phone via ssh -p 8022 -l nix-on-droid -i /path/to/id_rsa <ip of phone>

We will provide a module for the sshd config and service in the future to simplify the initial setup.

Kiwi commented 4 years ago

That looks like what I did! I was going to do a write up on it but... you know, life.

chenyulue commented 4 years ago

I follows the @Gerschtli 's instructions, and finally run: ssh -p 8022 -l nix-on-droid -i ~/.ssh/id_rsa 192.168.0.101

I can connect to my phone, but get the following strange results:

-sh-4.4$ ls -sh: ls: command not found -sh-4.4$ echo $HOME /data/data/com.termux.nix/files/home -sh-4.4$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/nix/store/xx89cqfsn7f9ydcwmyiw0n110gwrhacv-openssh-7.9p1/bin -sh-4.4$ echo $PREFIX -sh-4.4$ cd /usr/bin -sh-4.4$ ls -sh: ls: command not found -sh-4.4$ nix-env -qa python -sh: nix-env: command not found -sh-4.4$

What's the problem with it?

Kiwi commented 4 years ago

I think I solved that by adding source ~/.nix-profile/etc/profile.d/nix.sh to my .bash_profile

source ~/.nix-profile/etc/profile.d/nix.sh
[[ -r ~/.bashrc ]] && . ~/.bashrc
chenyulue commented 4 years ago

I think I solved that by adding source ~/.nix-profile/etc/profile.d/nix.sh to my .bash_profile

source ~/.nix-profile/etc/profile.d/nix.sh
[[ -r ~/.bashrc ]] && . ~/.bashrc

Solves my problem?

Kiwi commented 4 years ago

I think I solved that by adding source ~/.nix-profile/etc/profile.d/nix.sh to my .bash_profile

source ~/.nix-profile/etc/profile.d/nix.sh
[[ -r ~/.bashrc ]] && . ~/.bashrc

Solves my problem?

Yeah I think so. If you put that on your nix-on-droid it should work. At least, I was able to reproduce the problem by removing that line from mine. So unless there was something else I had to do to even make that work... idk. Hope that helps!

Gerschtli commented 4 years ago

To be more generic, you should source ~/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh too or put that in your bashrc. There should be a home-manager module to set this config.

chenyulue commented 4 years ago

Mine still doesn't work.

My .bashrc and .bash_profile in $HOME contains: .bashrc

alias ls="ls --color=always"

.bash_profile

source ~/.nix-profile/etc/profile.d/nix.sh
[[ -r ~/.bashrc ]] && . ~/.bashrc

There is nothing else in these two files. So maybe there is something that I have missed, since I'm not familiar with Linux.

BTW, there is no file of ~/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh in my phone.

However, a workaround is manually set the $PATH and the $NIX_PATH, or just run source ~/.bash_profile when connecting to my phone succsessfully. Then every thing seems OK.

So it seems that the .bashrc or .bash_profile are not loaded automatically when connecting to my phone. My OS in my computer is win10, connecting in Powershell.

Kiwi commented 4 years ago

I reinstalled because of reasons... anyway... this time (and maybe last time too...) I had to make the line be sourced in .profile instead of .bash_profile. Maybe that'll get it to work for you.

Gerschtli commented 4 years ago

I'm not sure but I might remember that the shell running on the phone when connected with ssh is not bash but sh, maybe even android's /system/bin/sh, which does not care of any bash config files. Therefore .profile seems to be the right file for now.

We should run the configured login shell instead of /bin/sh.

For the nix-on-droid-session-init.sh you need to update to the latest version.

chenyulue commented 4 years ago

@Gerschtli @Kiwi .profile works right! Thank both of you!

Kiwi commented 4 years ago

You're welcome! Glad it worked in the end.

For the sake of posterity: It may be that my .profile sourced my .bash_profile and that's why removing the line from .bash_profile reproduced the problem. I didn't notice the .profile or remember that was the real solution. Sorry for the goose hunt. I can't confirm since I've reinstalled nix-on-droid... but that'd make sense.