2020-08-13 19:54:51 +00:00
|
|
|
#! @runtimeShell@
|
2021-11-26 05:54:23 +00:00
|
|
|
# shellcheck shell=bash
|
2018-02-05 18:41:54 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Re-exec ourselves in a private mount namespace so that our bind
|
|
|
|
# mounts get cleaned up automatically.
|
2018-02-07 16:59:04 +00:00
|
|
|
if [ -z "$NIXOS_ENTER_REEXEC" ]; then
|
|
|
|
export NIXOS_ENTER_REEXEC=1
|
|
|
|
if [ "$(id -u)" != 0 ]; then
|
|
|
|
extraFlags="-r"
|
2018-02-05 18:41:54 +00:00
|
|
|
fi
|
2018-02-07 16:59:04 +00:00
|
|
|
exec unshare --fork --mount --uts --mount-proc --pid $extraFlags -- "$0" "$@"
|
|
|
|
else
|
|
|
|
mount --make-rprivate /
|
2018-02-05 18:41:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
mountPoint=/mnt
|
|
|
|
system=/nix/var/nix/profiles/system
|
2019-08-13 21:05:22 +00:00
|
|
|
command=("$system/sw/bin/bash" "--login")
|
|
|
|
silent=0
|
2018-02-05 18:41:54 +00:00
|
|
|
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
i="$1"; shift 1
|
|
|
|
case "$i" in
|
|
|
|
--root)
|
|
|
|
mountPoint="$1"; shift 1
|
|
|
|
;;
|
|
|
|
--system)
|
|
|
|
system="$1"; shift 1
|
|
|
|
;;
|
|
|
|
--help)
|
|
|
|
exec man nixos-enter
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
--command|-c)
|
2019-08-13 21:05:22 +00:00
|
|
|
command=("$system/sw/bin/bash" "-c" "$1")
|
2018-02-05 18:41:54 +00:00
|
|
|
shift 1
|
|
|
|
;;
|
2019-08-13 21:05:22 +00:00
|
|
|
--silent)
|
|
|
|
silent=1
|
|
|
|
;;
|
2018-02-05 18:41:54 +00:00
|
|
|
--)
|
|
|
|
command=("$@")
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$0: unknown option \`$i'"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2018-02-07 16:23:05 +00:00
|
|
|
if [[ ! -e $mountPoint/etc/NIXOS ]]; then
|
|
|
|
echo "$0: '$mountPoint' is not a NixOS installation" >&2
|
|
|
|
exit 126
|
|
|
|
fi
|
|
|
|
|
2019-08-13 21:05:22 +00:00
|
|
|
mkdir -p "$mountPoint/dev" "$mountPoint/sys"
|
|
|
|
chmod 0755 "$mountPoint/dev" "$mountPoint/sys"
|
2018-02-05 18:41:54 +00:00
|
|
|
mount --rbind /dev "$mountPoint/dev"
|
2018-04-22 20:02:57 +00:00
|
|
|
mount --rbind /sys "$mountPoint/sys"
|
2018-02-05 18:41:54 +00:00
|
|
|
|
2021-09-13 17:56:03 +00:00
|
|
|
# modified from https://github.com/archlinux/arch-install-scripts/blob/bb04ab435a5a89cd5e5ee821783477bc80db797f/arch-chroot.in#L26-L52
|
|
|
|
chroot_add_resolv_conf() {
|
2022-03-18 16:19:09 +00:00
|
|
|
local chrootDir="$1" resolvConf="$1/etc/resolv.conf"
|
2021-09-13 17:56:03 +00:00
|
|
|
|
|
|
|
[[ -e /etc/resolv.conf ]] || return 0
|
|
|
|
|
|
|
|
# Handle resolv.conf as a symlink to somewhere else.
|
2022-03-18 16:19:09 +00:00
|
|
|
if [[ -L "$resolvConf" ]]; then
|
2021-09-13 17:56:03 +00:00
|
|
|
# readlink(1) should always give us *something* since we know at this point
|
|
|
|
# it's a symlink. For simplicity, ignore the case of nested symlinks.
|
2022-03-18 16:19:09 +00:00
|
|
|
# We also ignore the possibility of `../`s escaping the root.
|
|
|
|
resolvConf="$(readlink "$resolvConf")"
|
|
|
|
if [[ "$resolvConf" = /* ]]; then
|
|
|
|
resolvConf="$chrootDir$resolvConf"
|
2021-09-13 17:56:03 +00:00
|
|
|
else
|
2022-03-18 16:19:09 +00:00
|
|
|
resolvConf="$chrootDir/etc/$resolvConf"
|
2021-09-13 17:56:03 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ensure file exists to bind mount over
|
2022-03-18 16:19:09 +00:00
|
|
|
if [[ ! -f "$resolvConf" ]]; then
|
|
|
|
install -Dm644 /dev/null "$resolvConf" || return 1
|
2021-09-13 17:56:03 +00:00
|
|
|
fi
|
|
|
|
|
2022-03-18 16:19:09 +00:00
|
|
|
mount --bind /etc/resolv.conf "$resolvConf"
|
2021-09-13 17:56:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:19:02 +00:00
|
|
|
chroot_add_resolv_conf "$mountPoint" || echo "$0: failed to set up resolv.conf" >&2
|
2021-09-13 17:56:03 +00:00
|
|
|
|
2020-02-13 01:50:05 +00:00
|
|
|
(
|
|
|
|
# If silent, write both stdout and stderr of activation script to /dev/null
|
|
|
|
# otherwise, write both streams to stderr of this process
|
|
|
|
if [ "$silent" -eq 1 ]; then
|
|
|
|
exec 2>/dev/null
|
|
|
|
fi
|
2019-08-13 21:05:22 +00:00
|
|
|
|
2023-05-20 02:11:38 +00:00
|
|
|
# Run the activation script. Set $LOCALE_ARCHIVE to suppress some Perl locale warnings.
|
2021-10-13 11:05:54 +00:00
|
|
|
LOCALE_ARCHIVE="$system/sw/lib/locale/locale-archive" IN_NIXOS_ENTER=1 chroot "$mountPoint" "$system/activate" 1>&2 || true
|
2020-02-22 01:49:56 +00:00
|
|
|
|
2023-01-01 16:18:20 +00:00
|
|
|
# Create /tmp. This is needed for nix-build and the NixOS activation script to work.
|
|
|
|
# Hide the unhelpful "failed to replace specifiers" errors caused by missing /etc/machine-id.
|
|
|
|
chroot "$mountPoint" "$system/sw/bin/systemd-tmpfiles" --create --remove -E 2> /dev/null || true
|
2020-02-13 01:50:05 +00:00
|
|
|
)
|
2018-02-05 18:41:54 +00:00
|
|
|
|
2021-12-30 23:10:14 +00:00
|
|
|
unset TMPDIR
|
|
|
|
|
2018-02-05 18:41:54 +00:00
|
|
|
exec chroot "$mountPoint" "${command[@]}"
|