diff --git a/nixos/doc/manual/man-nixos-enter.xml b/nixos/doc/manual/man-nixos-enter.xml
new file mode 100644
index 000000000000..a2fbe07961db
--- /dev/null
+++ b/nixos/doc/manual/man-nixos-enter.xml
@@ -0,0 +1,119 @@
+
+
+
+ nixos-enter
+ 8
+ NixOS
+
+
+
+
+ nixos-enter
+ run a command in a NixOS chroot environment
+
+
+
+
+ nixos-enter
+
+
+ root
+
+
+
+ system
+
+
+
+ shell-command
+
+
+
+
+
+
+ arguments
+
+
+
+
+
+Description
+
+This command runs a command in a NixOS chroot environment, that
+is, in a filesystem hierarchy previously prepared using
+nixos-install.
+
+
+
+Options
+
+This command accepts the following options:
+
+
+
+
+
+
+ The path to the NixOS system you want to enter. It defaults to /mnt.
+
+
+
+
+
+
+ The NixOS system configuration to use. It defaults to
+ /nix/var/nix/profiles/system. You can enter
+ a previous NixOS configuration by specifying a path such as
+ /nix/var/nix/profiles/system-106-link.
+
+
+
+
+
+
+
+ The bash command to execute.
+
+
+
+
+
+
+ Interpret the remaining arguments as the program
+ name and arguments to be invoked. The program is not executed in a
+ shell.
+
+
+
+
+
+
+
+
+Examples
+
+Start an interactive shell in the NixOS installation in
+/mnt:
+
+
+# nixos-enter /mnt
+
+
+Run a shell command:
+
+
+# nixos-enter -c 'ls -l /; cat /proc/mounts'
+
+
+Run a non-shell command:
+
+
+# nixos-enter -- cat /proc/mounts
+
+
+
+
+
diff --git a/nixos/doc/manual/man-pages.xml b/nixos/doc/manual/man-pages.xml
index e945e0e62639..80a8458fbfec 100644
--- a/nixos/doc/manual/man-pages.xml
+++ b/nixos/doc/manual/man-pages.xml
@@ -15,7 +15,7 @@
- 2007-2015
+ 2007-2018
Eelco Dolstra
@@ -25,6 +25,7 @@
+
diff --git a/nixos/modules/installer/tools/nixos-enter.sh b/nixos/modules/installer/tools/nixos-enter.sh
new file mode 100644
index 000000000000..c5c7963b29f3
--- /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[@]}"
diff --git a/nixos/modules/installer/tools/tools.nix b/nixos/modules/installer/tools/tools.nix
index a3bae78c0ffc..9398e2dc1ebc 100644
--- a/nixos/modules/installer/tools/tools.nix
+++ b/nixos/modules/installer/tools/tools.nix
@@ -1,7 +1,9 @@
# This module generates nixos-install, nixos-rebuild,
# nixos-generate-config, etc.
-{ config, pkgs, modulesPath, ... }:
+{ config, lib, pkgs, modulesPath, ... }:
+
+with lib;
let
cfg = config.installer;
@@ -69,6 +71,11 @@ let
inherit (config.system) nixosVersion nixosCodeName nixosRevision;
};
+ nixos-enter = makeProg {
+ name = "nixos-enter";
+ src = ./nixos-enter.sh;
+ };
+
in
{
@@ -83,10 +90,11 @@ in
nixos-generate-config
nixos-option
nixos-version
+ nixos-enter
];
system.build = {
- inherit nixos-install nixos-prepare-root nixos-generate-config nixos-option nixos-rebuild;
+ inherit nixos-install nixos-prepare-root nixos-generate-config nixos-option nixos-rebuild nixos-enter;
};
};