#!/usr/bin/env bash # This script builds the initial bootstrap zipball for Nix on Android. # Copyright (c) 2019 Alexander Sosedkin # 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 . set -uex ARCH=${1:-aarch64} ARCHIVE=nix-2.2.2-$ARCH-linux.tar.bz2 BASEURL=https://nixos.org/releases/nix/nix-2.2.2 URL="$BASEURL/$ARCHIVE" INST="/data/data/com.termux.nix/files/usr" export PROOT_NO_SECCOMP=1 # see https://github.com/proot-me/PRoot/issues/106 rm -rf ./bootstrap mkdir -p bootstrap echo "building proot for $ARCH (this may take a lot of time...)" nix build -f proot.nix -o proot-$ARCH --argstr arch aarch64 wget https://github.com/proot-me/proot-static-build/blob/master/static/proot-x86_64?raw=true -O proot-host chmod +x proot-host if [[ $ARCH != x86_64 && $ARCH != i686 ]]; then wget https://github.com/multiarch/qemu-user-static/releases/download/v4.0.0-4/qemu-$ARCH-static -O qemu-$ARCH chmod +x qemu-$ARCH fi echo "downloading nix archive" wget $URL -O $ARCHIVE echo -n "extracting $ARCHIVE " tar -xf "$ARCHIVE" -C bootstrap --checkpoint=200 --checkpoint-action=dot echo " done" mv bootstrap/nix* bootstrap/nix-installer echo -n "copying store... " mkdir bootstrap/nix cp -r bootstrap/nix-installer/store bootstrap/nix/ echo "done" mkdir -p bootstrap/bin TGT_SHELL=$(find bootstrap/nix/store -path '*/bin/bash' | sed s/^bootstrap//) echo "shell found at $TGT_SHELL" ln -s "$TGT_SHELL" bootstrap/bin/sh mkdir -p bootstrap/usr/bin TGT_ENV=$(find bootstrap/nix/store -path '*/bin/env' | sed s/^bootstrap//) echo "env found at $TGT_ENV" ln -s "$TGT_ENV" bootstrap/usr/bin/env TGT_NIX=$(find bootstrap/nix/store -path '*/bin/nix' | sed s/^bootstrap//) TGT_NIX=${TGT_NIX%/bin/nix} echo "nix derivation found at $TGT_NIX" TGT_CACERT=$(find bootstrap/nix/store -path '*-nss-cacert-*/ca-bundle.crt' | sed s/^bootstrap//) echo "cacert found at $TGT_CACERT" if [[ $ARCH != x86_64 && $ARCH != i686 ]]; then PROOT_CMD="./proot-host -q ./qemu-$ARCH -r bootstrap -w /" else PROOT_CMD="./proot-host -b /dev -r bootstrap -w /" fi echo "initialising Nix database..." $PROOT_CMD "$TGT_NIX/bin/nix-store" --init echo "registering paths..." $PROOT_CMD "$TGT_NIX/bin/nix-store" --load-db < bootstrap/nix-installer/.reginfo echo "injecting proot..." mkdir -p bootstrap/bin cp proot-$ARCH/bin/proot bootstrap/bin/proot echo "making up some resolv.conf..." mkdir -p bootstrap/etc echo -e "nameserver 1.1.1.1\nnameserver 8.8.8.8" > bootstrap/etc/resolv.conf echo "disabling sandboxing..." # we can't rely on USER_NS support mkdir -p bootstrap/etc/nix echo "sandbox = false " > bootstrap/etc/nix/nix.conf echo "composing login scripts..." mkdir -p bootstrap/bin mkdir -p bootstrap/root mkdir -p bootstrap/tmp mkdir -p bootstrap/.l2s cat > bootstrap/bin/login < $INST/etc/passwd echo "nix-on-droid:x:\$(/system/bin/stat -c '%u:%g' $INST):nix-on-droid:/data/data/com.termux.nix/files/home:/bin/sh" >> $INST/etc/passwd fi exec $INST/bin/proot \ -b $INST/nix:/nix \ -b $INST/bin:/bin \ -b $INST/etc:/etc \ -b $INST/tmp:/tmp \ -b $INST/usr:/usr \ -b /:/android \ --link2symlink \ $INST/bin/sh $INST/bin/.login-inner \$USER "\$@" EOF chmod +x bootstrap/bin/login cat > bootstrap/bin/.login-inner < bootstrap/bin/nix-on-droid-install <' -A install echo "Edit ~/.config/nixpkgs/home.nix and home-manager rebuild to control what is going on." echo "Run bash or restart your session to enjoy a much nicer environment." EOF chmod +x bootstrap/bin/nix-on-droid-install echo "providing a default home-manager config..." cat > bootstrap/etc/home.nix.default < bootstrap/EXECUTABLES.txt echo "finding symlinks..." find bootstrap -type l | while read -r LINK; do LNK=${LINK#bootstrap/} TGT=$(readlink "$LINK") echo "$TGT←$LNK" >> bootstrap/SYMLINKS.txt rm "$LINK" done echo "packing..." rm -f bootstrap-$ARCH.zip (cd bootstrap; zip -q -9 -r ../bootstrap-$ARCH ./* ./.l2s) echo "done"