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