# This file is generated by nix-on-droid. DO NOT EDIT.

set -eu -o pipefail

if [ "$#" -eq 0 ]; then  # if script is called from within nix-on-droid app
  echo "Welcome to Nix-on-Droid!"
  echo "If nothing works, open an issue at https://github.com/t184256/nix-on-droid/issues or try the rescue shell."
fi

if [ -e /etc/UNINTIALISED ]; then
  export HOME="/data/data/com.termux.nix/files/home"
  export USER="nix-on-droid"

  # To prevent gc warnings of nix, see https://github.com/NixOS/nix/issues/3237
  export GC_NPROCS=1

  echo "Setting default user profile..."
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-env --switch-profile /nix/var/nix/profiles/per-user/$USER/profile

  [ "$#" -gt 0 ] || echo "Sourcing Nix environment..."
  . /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/etc/profile.d/nix.sh

  export NIX_SSL_CERT_FILE=/nix/store/2n66r4hi7z223vaqcnz41xchlcfmmadf-nss-cacert-3.49.2/etc/ssl/certs/ca-bundle.crt

  echo "Installing and updating nix-channels..."
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-channel --add https://nixos.org/channels/nixos-20.03 nixpkgs
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-channel --update nixpkgs
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-channel --add https://github.com/t184256/nix-on-droid/archive/release-20.03.tar.gz nix-on-droid
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-channel --update nix-on-droid

  echo "Copying default nix-on-droid config..."
  /nix/store/62l1fmmmynxr5q8jc65wllk9wl2fmnp3-coreutils-8.31/bin/mkdir --parents $HOME/.config/nixpkgs
  /nix/store/62l1fmmmynxr5q8jc65wllk9wl2fmnp3-coreutils-8.31/bin/cp $(/nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix-instantiate --eval --expr "<nix-on-droid/modules/environment/login/nix-on-droid.nix.default>") $HOME/.config/nixpkgs/nix-on-droid.nix
  /nix/store/62l1fmmmynxr5q8jc65wllk9wl2fmnp3-coreutils-8.31/bin/chmod u+w $HOME/.config/nixpkgs/nix-on-droid.nix

  echo "Installing first nix-on-droid generation..."
  /nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix build --no-link --file "<nix-on-droid>" nix-on-droid
  $(/nix/store/72pwn0nm9bjqx9vpi8sgh4bl6g5wh814-nix-2.3.6/bin/nix path-info --file "<nix-on-droid>" nix-on-droid)/bin/nix-on-droid switch

  echo
  echo "Congratulations! Now you have Nix installed with some default packages like bashInteractive, \
coreutils, cacert and most important nix-on-droid itself to manage local configuration, see"
  echo "  nix-on-droid help"
  echo "or in the config file"
  echo "  ~/.config/nixpkgs/nix-on-droid.nix"
  echo
  echo "You can go for the bare nix-on-droid setup or you can configure your phone via home-manager. See \
config file for further information."
  echo
fi


set +u
. "/data/data/com.termux.nix/files/home/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
set -u

exec /usr/bin/env bash  # otherwise it'll be a limited bash that came with Nix


if [ "$#" -gt 0 ]; then  # if script is not called from within nix-on-droid app
  exec /usr/bin/env "$@"
elif [ -x "/nix/store/ch6r6393g2aryabygwb4cqjxsmsbn5s8-bash-4.4-p23/bin/bash" ]; then
  exec "/nix/store/ch6r6393g2aryabygwb4cqjxsmsbn5s8-bash-4.4-p23/bin/bash"
else
  echo "Cannot execute shell '/nix/store/ch6r6393g2aryabygwb4cqjxsmsbn5s8-bash-4.4-p23/bin/bash', falling back to bash"
  exec /usr/bin/env bash
fi
