forked from t184256/nix-in-termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nix-in-termux
153 lines (118 loc) · 5.08 KB
/
nix-in-termux
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh -ue
# This script installs Nix package manager inside a Termux installation.
# Copyright (c) 2019 Alexander Sosedkin <monk@unboiled.info>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Based off the official Nix install script (https://nixos.org/nix/install),
# presumably written by Eelco Dolstra.
# This script installs Nix package manager inside a Termux installation.
# It does not require root, user namespaces or disabling SELinux,
# but it relies on proot and numerous hacks instead.
# Only tested with aarch64, may also accidentally work on x86.
# Usage:
# * Install and run Termux
# * Execute this script inside Termux
# Careful, this is carelessly written alpha-quality stuff.
PLAYGROUND="$HOME/.nix"
DIVE_SCRIPT="$PLAYGROUND/nix-powered"
TERMUX_BIN="/data/data/com.termux/files/usr/bin"
oops() {
echo "$0:" "$@" >&2
exit 1
}
if [ ! -x "$DIVE_SCRIPT" ]; then
echo "removing the previous installation..."
chmod -R 700 "$PLAYGROUND" || true
rm -rf "$PLAYGROUND" || oops "failed to remove $PLAYGROUND"
mkdir -p "$PLAYGROUND" || oops "failed to create $PLAYGROUND"
echo "installing tools..."
pkg install -y proot bzip2 tar wget || oops "failed to install required tools"
tmpDir="$PLAYGROUND/tmp"
mkdir -p "$tmpDir" || oops "false to create $tmpDir"
echo "preparing mock /nix and /etc..."
mkdir "$PLAYGROUND/nix" || oops "failed to create $PLAYGROUND/nix"
mkdir "$PLAYGROUND/etc" || oops "failed to create $PLAYGROUND/etc"
echo 'nameserver 1.1.1.1' > "$PLAYGROUND/etc/resolv.conf" || oops "failed to create $PLAYGROUND/etc/resolv.conf"
mkdir "$PLAYGROUND/etc/nix/" || oops "failed to create $PLAYGROUND/etc/nix"
# https://github.com/NixOS/nix/issues/2632#issuecomment-457610729
echo 'sandbox = false' > "$PLAYGROUND/etc/nix/nix.conf" || oops "failed to create $PLAYGROUND/etc/nix/nix.conf"
case "$(uname -s).$(uname -m)" in
Linux.x86_64) system=x86_64-linux; hash=e229e28f250cad684c278c9007b07a24eb4ead239280c237ed2245871eca79e0;;
Linux.i?86) system=i686-linux; hash=223c141b7ce322aacd4003dfc79b969ee813396d2ac586cf44c0e787233b1666;;
Linux.aarch64) system=aarch64-linux; hash=94d3d7d2b551b228ec38f83c6f037c5e5b6aaf0805450967f800c02a827b1f41;;
Darwin.x86_64) system=x86_64-darwin; hash=4182a38948c9ce520993f79a5caf8ce3d49636189f4f0f7e8decbc91ddb61cd1;;
*) oops "sorry, there is no binary distribution of Nix for your platform";;
esac
url="https://nixos.org/releases/nix/nix-2.2.1/nix-2.2.1-$system.tar.bz2"
tarball="$tmpDir/$(basename "$tmpDir/nix-2.2.1-$system.tar.bz2")"
#tarball="$HOME/nix-2.2.1-$system.tar.bz2"
echo "downloading Nix 2.2.1 binary tarball for $system from '$url' to '$tmpDir'..."
$TERMUX_BIN/wget "$url" -O "$tarball" || oops "failed to download '$url'"
hash2="$(sha256sum -b "$tarball" | cut -c1-64)"
if [ "$hash" != "$hash2" ]; then
oops "SHA-256 hash mismatch in '$url'; expected $hash, got $hash2"
fi
echo "unpacking $tarball..."
unpack=$tmpDir/unpack
mkdir -p "$unpack"
cleanup() {
rm -rf "$unpack"
}
trap cleanup EXIT INT QUIT TERM
< "$tarball" bzcat | $TERMUX_BIN/tar -xf - -C "$unpack" || oops "failed to unpack $tarball"
script=$(echo "$unpack"/*/install)
[ -e "$script" ] || oops "installation script is missing from the binary tarball!"
INTERMEDIATE_SCRIPT="$tmpDir/intermediate_install_script"
{
########
cat > "$INTERMEDIATE_SCRIPT" << --------EOF
export USER=\$(whoami)
export NIX_INSTALLER_NO_MODIFY_PROFILE=true
unset LD_LIBRARY_PATH
unset LD_PRELOAD
$script "\$@"
--------EOF
} || oops "failed to create $INTERMEDIATE_SCRIPT"
chmod +x "$INTERMEDIATE_SCRIPT" || oops "failed to chmod +x $INTERMEDIATE_SCRIPT"
echo "running Nix install script in proot..."
proot \
-b "$PLAYGROUND/nix:/nix" \
-b "$PLAYGROUND/etc:/etc" \
--link2symlink \
"$INTERMEDIATE_SCRIPT" || oops "failed to run Nix install script in proot"
echo "creating $DIVE_SCRIPT and finalizind the installation..."
{
########
cat > "$DIVE_SCRIPT" << --------EOF
#!/bin/sh -e
export PS1="nix-powered> "
export USER=$(whoami)
unset LD_LIBRARY_PATH
unset LD_PRELOAD
echo Termux PATH \$PATH
TERMUX_PATH="\$PATH"
. \$HOME/.nix-profile/etc/profile.d/nix.sh
export PATH=\${PATH%":\$TERMUX_PATH"}
"\$@"
--------EOF
} || oops "failed to create $DIVE_SCRIPT"
chmod +x "$DIVE_SCRIPT" || oops "failed to chmod +x $DIVE_SCRIPT"
echo "installation is completed."
echo "to reinstall, rm -r \"$PLAYGROUND\" and re-run the script"
fi
proot \
-b "$PLAYGROUND/nix:/nix" \
-b "$PLAYGROUND/etc:/etc" \
-b "/etc:/android-etc" \
--link2symlink \
"$DIVE_SCRIPT" "$@"