summary refs log tree commit diff
path: root/nixos/modules/installer/tools/nixos-enter.sh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2018-02-05 19:41:54 +0100
committerEelco Dolstra <edolstra@gmail.com>2018-02-05 19:41:54 +0100
commit60cb23001a1e8e4102ce905810c5641a7eb3a237 (patch)
tree0c7e4a39beacc03e201af3f81163f015941a4d48 /nixos/modules/installer/tools/nixos-enter.sh
parent294a4e6ea5245a7dede7932383813fa7272f277b (diff)
downloadnixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar.gz
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar.bz2
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar.lz
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar.xz
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.tar.zst
nixpkgs-60cb23001a1e8e4102ce905810c5641a7eb3a237.zip
Add a "nixos-enter" command
This factors out the functionality in nixos-install for running a
command inside a NixOS installation (nixos-install --chroot).
Diffstat (limited to 'nixos/modules/installer/tools/nixos-enter.sh')
-rw-r--r--nixos/modules/installer/tools/nixos-enter.sh58
1 files changed, 58 insertions, 0 deletions
diff --git a/nixos/modules/installer/tools/nixos-enter.sh b/nixos/modules/installer/tools/nixos-enter.sh
new file mode 100644
index 00000000000..c5c7963b29f
--- /dev/null
+++ b/nixos/modules/installer/tools/nixos-enter.sh
@@ -0,0 +1,58 @@
+#! @shell@
+
+set -e
+
+# Re-exec ourselves in a private mount namespace so that our bind
+# mounts get cleaned up automatically.
+if [ "$(id -u)" = 0 ]; then
+    if [ -z "$NIXOS_ENTER_REEXEC" ]; then
+        export NIXOS_ENTER_REEXEC=1
+        exec unshare --mount --uts -- "$0" "$@"
+    else
+        mount --make-rprivate /
+    fi
+fi
+
+mountPoint=/mnt
+command=("bash" "--login")
+system=/nix/var/nix/profiles/system
+
+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)
+            command=("bash" "-c" "$1")
+            shift 1
+            ;;
+        --)
+            command=("$@")
+            break
+            ;;
+        *)
+            echo "$0: unknown option \`$i'"
+            exit 1
+            ;;
+    esac
+done
+
+# Set up some bind mounts we'll want regardless of chroot or not
+mkdir -m 0755 -p "$mountPoint/dev" "$mountPoint/proc" "$mountPoint/sys" "$mountPoint/run"
+mount --rbind /dev "$mountPoint/dev"
+mount -t proc none "$mountPoint/proc"
+mount -t sysfs none "$mountPoint/sys"
+mount -t tmpfs none "$mountPoint/run"
+
+# Run the activation script. Set $LOCALE_ARCHIVE to supress some Perl locale warnings.
+LOCALE_ARCHIVE=$system/sw/lib/locale/locale-archive chroot "$mountPoint" "$system/activate" >&2
+
+exec chroot "$mountPoint" "${command[@]}"