1#! @runtimeShell@
2
3set -e
4
5# Re-exec ourselves in a private mount namespace so that our bind
6# mounts get cleaned up automatically.
7if [ -z "$NIXOS_ENTER_REEXEC" ]; then
8 export NIXOS_ENTER_REEXEC=1
9 if [ "$(id -u)" != 0 ]; then
10 extraFlags="-r"
11 fi
12 exec unshare --fork --mount --uts --mount-proc --pid $extraFlags -- "$0" "$@"
13else
14 mount --make-rprivate /
15fi
16
17mountPoint=/mnt
18system=/nix/var/nix/profiles/system
19command=("$system/sw/bin/bash" "--login")
20silent=0
21
22while [ "$#" -gt 0 ]; do
23 i="$1"; shift 1
24 case "$i" in
25 --root)
26 mountPoint="$1"; shift 1
27 ;;
28 --system)
29 system="$1"; shift 1
30 ;;
31 --help)
32 exec man nixos-enter
33 exit 1
34 ;;
35 --command|-c)
36 command=("$system/sw/bin/bash" "-c" "$1")
37 shift 1
38 ;;
39 --silent)
40 silent=1
41 ;;
42 --)
43 command=("$@")
44 break
45 ;;
46 *)
47 echo "$0: unknown option \`$i'"
48 exit 1
49 ;;
50 esac
51done
52
53if [[ ! -e $mountPoint/etc/NIXOS ]]; then
54 echo "$0: '$mountPoint' is not a NixOS installation" >&2
55 exit 126
56fi
57
58mkdir -p "$mountPoint/dev" "$mountPoint/sys"
59chmod 0755 "$mountPoint/dev" "$mountPoint/sys"
60mount --rbind /dev "$mountPoint/dev"
61mount --rbind /sys "$mountPoint/sys"
62
63# modified from https://github.com/archlinux/arch-install-scripts/blob/bb04ab435a5a89cd5e5ee821783477bc80db797f/arch-chroot.in#L26-L52
64chroot_add_resolv_conf() {
65 local chrootdir=$1 resolv_conf=$1/etc/resolv.conf
66
67 [[ -e /etc/resolv.conf ]] || return 0
68
69 # Handle resolv.conf as a symlink to somewhere else.
70 if [[ -L $chrootdir/etc/resolv.conf ]]; then
71 # readlink(1) should always give us *something* since we know at this point
72 # it's a symlink. For simplicity, ignore the case of nested symlinks.
73 # We also ignore the possibility if `../`s escaping the root.
74 resolv_conf=$(readlink "$chrootdir/etc/resolv.conf")
75 if [[ $resolv_conf = /* ]]; then
76 resolv_conf=$chrootdir$resolv_conf
77 else
78 resolv_conf=$chrootdir/etc/$resolv_conf
79 fi
80 fi
81
82 # ensure file exists to bind mount over
83 if [[ ! -f $resolv_conf ]]; then
84 install -Dm644 /dev/null "$resolv_conf" || return 1
85 fi
86
87 mount --bind /etc/resolv.conf "$resolv_conf"
88}
89
90chroot_add_resolv_conf "$mountPoint" || print "ERROR: failed to set up resolv.conf"
91
92(
93 # If silent, write both stdout and stderr of activation script to /dev/null
94 # otherwise, write both streams to stderr of this process
95 if [ "$silent" -eq 1 ]; then
96 exec 2>/dev/null
97 fi
98
99 # Run the activation script. Set $LOCALE_ARCHIVE to supress some Perl locale warnings.
100 LOCALE_ARCHIVE="$system/sw/lib/locale/locale-archive" IN_NIXOS_ENTER=1 chroot "$mountPoint" "$system/activate" 1>&2 || true
101
102 # Create /tmp
103 chroot "$mountPoint" systemd-tmpfiles --create --remove --exclude-prefix=/dev 1>&2 || true
104)
105
106exec chroot "$mountPoint" "${command[@]}"