Skip to content

Commit

Permalink
Check if the selected shell is a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Aug 29, 2024
1 parent 248cc08 commit c7db394
Showing 1 changed file with 126 additions and 115 deletions.
241 changes: 126 additions & 115 deletions modules/environment/login/login-inner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,148 +6,159 @@ let
inherit (initialPackageInfo) cacert nix;

nixCmd = "${nix}/bin/nix --extra-experimental-features 'flakes nix-command'";
isDir = path: builtins.toString path + "/.";
in

writeText "login-inner" ''
# This file is generated by Nix-on-Droid. DO NOT EDIT.
if ! builtins.pathExists config.user.shell then
builtins.abort "config.user.shell does not exist"
else if isDir config.user.shell then
builtins.abort "config.user.shell points to a directory, but it should refer to the shell executable file"
else
writeText "login-inner" ''
# This file is generated by Nix-on-Droid. DO NOT EDIT.
set -eo pipefail
set -eo pipefail
${lib.optionalString (config.environment.motd != null) ''
if [ "$#" -eq 0 ]; then # if script is called from within Nix-on-Droid app
echo "${lib.removeSuffix "\n" config.environment.motd}"
fi
''}
${lib.optionalString config.build.initialBuild ''
if [ -e /etc/UNINTIALISED ]; then
export HOME="${config.user.home}"
export USER="${config.user.userName}"
# To prevent gc warnings of nix, see https://github.com/NixOS/nix/issues/3237
export GC_NPROCS=1
${lib.optionalString (config.environment.motd != null) ''
if [ "$#" -eq 0 ]; then # if script is called from within Nix-on-Droid app
echo "${lib.removeSuffix "\n" config.environment.motd}"
fi
''}
echo "Setting default user profile..."
${nix}/bin/nix-env --switch-profile /nix/var/nix/profiles/per-user/$USER/profile
${lib.optionalString config.build.initialBuild ''
if [ -e /etc/UNINTIALISED ]; then
export HOME="${config.user.home}"
export USER="${config.user.userName}"
[ "$#" -gt 0 ] || echo "Sourcing Nix environment..."
. ${nix}/etc/profile.d/nix.sh
# To prevent gc warnings of nix, see https://github.com/NixOS/nix/issues/3237
export GC_NPROCS=1
export NIX_SSL_CERT_FILE=${cacert}
echo "Setting default user profile..."
${nix}/bin/nix-env --switch-profile /nix/var/nix/profiles/per-user/$USER/profile
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
[ "$#" -gt 0 ] || echo "Sourcing Nix environment..."
. ${nix}/etc/profile.d/nix.sh
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
export NIX_SSL_CERT_FILE=${cacert}
if [[ "$USE_FLAKE" == 0 ]]; then
echo "Setting up Nix-on-Droid with channels..."
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
echo "Installing and updating nix-channels..."
${nix}/bin/nix-channel --add ${config.build.channel.nixpkgs} nixpkgs
${nix}/bin/nix-channel --update nixpkgs
${nix}/bin/nix-channel --add ${config.build.channel.nix-on-droid} nix-on-droid
${nix}/bin/nix-channel --update nix-on-droid
if [[ "$USE_FLAKE" == 0 ]]; then
DEFAULT_CONFIG=$(${nix}/bin/nix-instantiate --eval --expr "<nix-on-droid/modules/environment/login/nix-on-droid.nix.default>")
echo "Setting up Nix-on-Droid with channels..."
echo "Installing first Nix-on-Droid generation..."
${nixCmd} build --no-link --file "<nix-on-droid>" nix-on-droid
$(${nixCmd} path-info --file "<nix-on-droid>" nix-on-droid)/bin/nix-on-droid switch --file $DEFAULT_CONFIG
echo "Installing and updating nix-channels..."
${nix}/bin/nix-channel --add ${config.build.channel.nixpkgs} nixpkgs
${nix}/bin/nix-channel --update nixpkgs
${nix}/bin/nix-channel --add ${config.build.channel.nix-on-droid} nix-on-droid
${nix}/bin/nix-channel --update nix-on-droid
. "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
DEFAULT_CONFIG=$(${nix}/bin/nix-instantiate --eval --expr "<nix-on-droid/modules/environment/login/nix-on-droid.nix.default>")
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
echo "Installing first Nix-on-Droid generation..."
${nixCmd} build --no-link --file "<nix-on-droid>" nix-on-droid
$(${nixCmd} path-info --file "<nix-on-droid>" nix-on-droid)/bin/nix-on-droid switch --file $DEFAULT_CONFIG
else
. "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
echo "Setting up Nix-on-Droid with flakes..."
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
echo "Installing flake from default template..."
${nixCmd} flake new ${config.user.home}/.config/nix-on-droid --template ${config.build.flake.nix-on-droid}
else
${lib.optionalString config.build.flake.inputOverrides ''
echo "Overriding input urls in the flake..."
echo "Setting up Nix-on-Droid with flakes..."
echo "Installing flake from default template..."
${nixCmd} flake new ${config.user.home}/.config/nix-on-droid --template ${config.build.flake.nix-on-droid}
${lib.optionalString config.build.flake.inputOverrides ''
echo "Overriding input urls in the flake..."
while IFS="" read -r p || [[ -n "$p" ]]
do
if [[ $p =~ (.*)github:NixOS/nixpkgs.*\"\; ]]; then
printf "''${BASH_REMATCH[1]}${config.build.flake.nixpkgs}\";\n" "$p"
elif [[ $p =~ (.*)github:nix-community/nix-on-droid.*\"\; ]]; then
printf "''${BASH_REMATCH[1]}${config.build.flake.nix-on-droid}\";\n" "$p"
else
printf '%s\n' "$p"
fi
done <<<$(< "${config.user.home}/.config/nix-on-droid/flake.nix") \
> "${config.user.home}/.config/nix-on-droid/flake.nix"
''}
echo "Overriding system value in the flake..."
while IFS="" read -r p || [[ -n "$p" ]]
do
if [[ $p =~ (.*)github:NixOS/nixpkgs.*\"\; ]]; then
printf "''${BASH_REMATCH[1]}${config.build.flake.nixpkgs}\";\n" "$p"
elif [[ $p =~ (.*)github:nix-community/nix-on-droid.*\"\; ]]; then
printf "''${BASH_REMATCH[1]}${config.build.flake.nix-on-droid}\";\n" "$p"
else
printf '%s\n' "$p"
fi
if [[ $p =~ (.*)\"aarch64-linux\"(.*) ]]; then
printf "''${BASH_REMATCH[1]}\"${targetSystem}\"''${BASH_REMATCH[2]}\n" "$p"
else
printf '%s\n' "$p"
fi
done <<<$(< "${config.user.home}/.config/nix-on-droid/flake.nix") \
> "${config.user.home}/.config/nix-on-droid/flake.nix"
''}
echo "Overriding system value in the flake..."
while IFS="" read -r p || [[ -n "$p" ]]
do
if [[ $p =~ (.*)\"aarch64-linux\"(.*) ]]; then
printf "''${BASH_REMATCH[1]}\"${targetSystem}\"''${BASH_REMATCH[2]}\n" "$p"
else
printf '%s\n' "$p"
fi
done <<<$(< "${config.user.home}/.config/nix-on-droid/flake.nix") \
> "${config.user.home}/.config/nix-on-droid/flake.nix"
echo "Installing first Nix-on-Droid generation..."
${nixCmd} run ${config.build.flake.nix-on-droid} -- switch --flake ${config.user.home}/.config/nix-on-droid
echo "Installing first Nix-on-Droid generation..."
${nixCmd} run ${config.build.flake.nix-on-droid} -- switch --flake ${config.user.home}/.config/nix-on-droid
. "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
. "${config.user.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"
fi
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 ${config.build.flake.nix-on-droid}."
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 ${config.build.flake.nix-on-droid}."
echo
fi
fi
''}
. "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
${lib.optionalString config.build.initialBuild ''
exec /usr/bin/env bash # otherwise it'll be a limited bash that came with Nix
''}
usershell="${config.user.shell}"
if [ "$#" -gt 0 ]; then # if script is not called from within Nix-on-Droid app
exec /usr/bin/env "$@"
elif [ -d "$usershell" ]; then
echo "Cannot execute shell '${config.user.shell}', it is a directory."
echo "Unlike nixos you should point `user.shell` to the exact binary."
echo "Falling back to bash"
exec -l bash
elif [ -x "$usershell" ]; then
exec -a "-''${usershell##*/}" "$usershell"
else
echo "Cannot execute shell '${config.user.shell}', falling back to bash"
exec -l bash
fi
''}
. "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh"
${lib.optionalString config.build.initialBuild ''
exec /usr/bin/env bash # otherwise it'll be a limited bash that came with Nix
''}
usershell="${config.user.shell}"
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 '${config.user.shell}', falling back to bash"
exec -l bash
fi
''
''

0 comments on commit c7db394

Please sign in to comment.