# This file is generated by Nix-on-Droid. DO NOT EDIT.

set -eo pipefail

if [ "$#" -eq 0 ]; then  # if script is called from within Nix-on-Droid app
  echo "Welcome to Nix-on-Droid!
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/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-env --switch-profile /nix/var/nix/profiles/per-user/$USER/profile

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

  export NIX_SSL_CERT_FILE=/nix/store/5lhg8dcxy51wkzbsi7ibjd3d944cn4id-nss-cacert-3.83/etc/ssl/certs/ca-bundle.crt

  echo
  echo "Nix-on-Droid can be set up with channels or with flakes (still experimental)."
  while [[ -z $USE_FLAKE ]]; do
    read -r -p "Do you want to set it up with flakes? (y/N) " flakes

    if [[ "$flakes" =~ ^[Yy]$ ]]; then
      USE_FLAKE=1
    elif [[ "$flakes" =~ ^[Nn]$ || -z "$flakes" ]]; then
      USE_FLAKE=0
    else
      echo "Received invalid input '$flakes', please try again."
    fi
  done

  if [[ "$USE_FLAKE" == 0 ]]; then

    echo "Setting up Nix-on-Droid with channels..."

    echo "Installing and updating nix-channels..."
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-channel --add https://nixos.org/channels/nixos-23.05 nixpkgs
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-channel --update nixpkgs
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-channel --add https://github.com/t184256/nix-on-droid/archive/prerelease-23.05.tar.gz nix-on-droid
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-channel --update nix-on-droid

    DEFAULT_CONFIG=$(/nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix-instantiate --eval --expr "<nix-on-droid/modules/environment/login/nix-on-droid.nix.default>")

    echo "Installing first Nix-on-Droid generation..."
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix --extra-experimental-features 'flakes nix-command' build --no-link --file "<nix-on-droid>" nix-on-droid
    $(/nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix --extra-experimental-features 'flakes nix-command' path-info --file "<nix-on-droid>" nix-on-droid)/bin/nix-on-droid switch --file $DEFAULT_CONFIG

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

    echo "Copying default Nix-on-Droid config..."
    mkdir --parents $HOME/.config/nixpkgs
    cp $DEFAULT_CONFIG $HOME/.config/nixpkgs/nix-on-droid.nix
    chmod u+w $HOME/.config/nixpkgs/nix-on-droid.nix

  else

    echo "Setting up Nix-on-Droid with flakes..."

    echo "Installing flake from default template..."
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix --extra-experimental-features 'flakes nix-command' flake new /data/data/com.termux.nix/files/home/.config/nix-on-droid --template github:t184256/nix-on-droid/prerelease-23.05

    echo "Overriding input urls in flake..."
/nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix --extra-experimental-features 'flakes nix-command' run nixpkgs#gnused -- \
  -i \
  -e 's,\"github:NixOS/nixpkgs.*\",\"github:NixOS/nixpkgs/nixos-23.05\",' \
  -e 's,\"github:t184256/nix-on-droid.*\",\"github:t184256/nix-on-droid/prerelease-23.05\",' \
  "/data/data/com.termux.nix/files/home/.config/nix-on-droid/flake.nix"


    echo "Installing first Nix-on-Droid generation..."
    /nix/store/snsjmpdb8hs4jfp82wiykkbb9667rywr-nix-2.16.1/bin/nix --extra-experimental-features 'flakes nix-command' run github:t184256/nix-on-droid/prerelease-23.05 -- switch --flake /data/data/com.termux.nix/files/home/.config/nix-on-droid

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

  fi

  echo
  echo "Congratulations! Now you have Nix installed with some default packages like bashInteractive, \
coreutils, cacert and, most importantly, Nix-on-Droid itself to manage local configuration, see"
  echo "  nix-on-droid help"

  if [[ "$USE_FLAKE" == 0 ]]; then
    echo "or 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
  else
    echo "or the flake"
    echo "  ~/.config/nix-on-droid/"
    echo
    echo "You can go for the bare Nix-on-Droid setup or you can configure your phone via home-manager. See \
other templates in github:t184256/nix-on-droid/prerelease-23.05."
    echo
  fi
fi


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

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


usershell="/nix/store/99bgmihz5ybclg11h00nm9lgqmsfhp4p-bash-5.1-p16/bin/bash"
if [ "$#" -gt 0 ]; then  # if script is not called from within Nix-on-Droid app
  exec /usr/bin/env "$@"
elif [ -x "$usershell" ]; then
  exec -a "-${usershell##*/}" "$usershell"
else
  echo "Cannot execute shell '/nix/store/99bgmihz5ybclg11h00nm9lgqmsfhp4p-bash-5.1-p16/bin/bash', falling back to bash"
  exec -l bash
fi
