1#! @shell@
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")
20
21while [ "$#" -gt 0 ]; do
22 i="$1"; shift 1
23 case "$i" in
24 --root)
25 mountPoint="$1"; shift 1
26 ;;
27 --system)
28 system="$1"; shift 1
29 ;;
30 --help)
31 exec man nixos-enter
32 exit 1
33 ;;
34 --command|-c)
35 command=($system/sw/bin/bash "-c" "$1")
36 shift 1
37 ;;
38 --)
39 command=("$@")
40 break
41 ;;
42 *)
43 echo "$0: unknown option \`$i'"
44 exit 1
45 ;;
46 esac
47done
48
49if [[ ! -e $mountPoint/etc/NIXOS ]]; then
50 echo "$0: '$mountPoint' is not a NixOS installation" >&2
51 exit 126
52fi
53
54mkdir -m 0755 -p "$mountPoint/dev" "$mountPoint/sys"
55mount --rbind /dev "$mountPoint/dev"
56mount --rbind /sys "$mountPoint/sys"
57
58# Run the activation script. Set $LOCALE_ARCHIVE to supress some Perl locale warnings.
59LOCALE_ARCHIVE=$system/sw/lib/locale/locale-archive chroot "$mountPoint" "$system/activate" >&2 || true
60
61exec chroot "$mountPoint" "${command[@]}"