Setup nix-on-droid
Prerequisites
- Root privilege on you android phone(it's not stable in devices without root)
- nix-on-droid installed.
- Optional:
adb,pwshYou can useadbto type inputs from your computer connected with your android device. The following powershell function solves the escape problem ofadb shell input text, so you don't have to escape manually.ps1# Use -Enter to press enter after command input <# This script helps to input text for terminal like Termux on Android It may not function on input scenarios other than shell on Android It DOES NOT start shell on Android but simulate key press to input text. NOTE: if default port fails, try another port #> function Send-AdbShellInput { param( [Parameter(Mandatory)] [string]$InputText, [ushort]$Port = 5037, [string]$SerialNumber, [switch]$AcceptLine ) begin { & ./Assert-AdbServer.ps1 @PSBoundParameters if (-not $SerialNumber) { $flags = @('-P', $Port) } else { $flags = @('-P', $Port, '-s', $SerialNumber) } $specials = @' |$%;\&~*'"`<>() '@ } end { $InputText = $InputText -replace "[$([regex]::Escape($specials))]", '\$0' $InputText = $InputText -replace ' ', '%s' adb @flags shell input text $InputText if ($AcceptLine) { adb @flags shell input keyevent KEYCODE_ENTER } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42NOTE
You can wrap the same as bash function by
awkor other text manipulation tools.
Init
nix-on-droidmay ask for url for certain file, if the url is not accessible on your phone, download it and transfer to your phone. And replace the default url asfile:///sdcard/...
remember to allow file permision for nix-on-droid.
- Type
yeswhen nix prompt for downloads for first init. - Add and update channels:sh
nix-channel --add https://nixos.org/channels/nixos-unstable nixpkgs && nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager && nix-channel --update1TIP
If you use the wrapper function mentioned above, would much easier to input:
ps1Send-AdbShellInput -InputText 'nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager' -AcceptLine1 - Update
nixcli: default nix is out-dated, might not work with your current config like home-manager.shnix profile install nixpkgs#nix --priority 41
Connect to nix-on-droid
- Install
openssh
sh
nix profile install nixpkgs#openssh1
- create a empty
ssh_config,sshdrequires at least one specified. We don't specify any option in it in this guide but it's needed afterward.
sh
mkdir -p /etc/ssh/ && touch /etc/ssh/sshd_config1
- generate a host key for nix-on-droid, change the key type and passphrase as you like, they don't make too much difference.
sh
ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N "" # key is generated in pwd1
- create
~/.ssh/authorized_keysand paste your public key from your computer(gc ~/.ssh/<name>.pub) to this file.
sh
mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo <pub> >> ~/.ssh/authorized_keys1
TIP
Use this instead if you prefer pwsh wrapper function above, replace the pubkey name if necessary.
ps1
Send-AdbShellInput -InputText ('mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo ''{0}'' >> ~/.ssh/authorized_keys' -f (gc ~/.ssh/id_ed25519.pub)) -AcceptLine1
- start ssh daemon by
sshd
sh
$(which sshd) -p 8080 -h ./ssh_host_rsa_key -d1
-d is essential to know whether your port is been taken or not. See details in man sshd.
- now connect to nix-on-droid from your computer
sh
ssh -l nix-on-droid -p <port> <ipaddr_of_phone>1
NOTE
<ipaddr_of_phone> can be inspected from your Settings - About phone
Final Step
Finally you can type everything in your computer through SSH! So use nix as you like.