Change root: Difference between revisions

From NixOS Wiki
imported>Mic92
No edit summary
imported>Nh2
Show manual chroot
Line 1: Line 1:
[https://en.wikipedia.org/wiki/Chroot Chroot] is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree. This modified environment is called a chroot jail.
[https://en.wikipedia.org/wiki/Chroot Chroot] is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree. This modified environment is called a chroot jail.


= Usage =
= Using nixos-enter =


nixos-enter allows to access a nixos installation from a rescue system.
nixos-enter allows to access a NixOS installation from a NixOS rescue system.


== Using nixos-enter ==
The nixos-enter program is part of NixOS. Before it runs provides a shell, the script mounts api filesystems like /proc and setups the profile and /etc of the target system. To use it, setup <code>/mnt</code> as described in the [https://nixos.org/nixos/manual/#sec-installation installation manual].
 
The nixos-enter program is part of nixos. Before it runs provides a shell, the script mounts api filesystems like /proc and setups the profile and /etc of the target system. To use it, setup <code>/mnt</code> as described in the [https://nixos.org/nixos/manual/#sec-installation installation manual].


Then run <code>nixos-enter</code>:
Then run <code>nixos-enter</code>:
Line 14: Line 12:
$ nixos-enter
$ nixos-enter
</syntaxHighlight>
</syntaxHighlight>
= Manual chroot =
If a NixOS rescue system is not available, the chroot can be done manually from another Linux distribution.
Mount the file system containing the NixOS to chroot into at <code>/mnt</code>, using e.g.:
<syntaxHighlight lang=bash>
mount /dev/relevantPartitionNameHere /mnt
</syntaxHighlight>.
Mount the host system's Linux run-time api file systems inside the mount, then populate <code>/run</code> using the <code>activate</code> script and chroot inside, starting a bash shell (adapted from [https://nixos.org/nix-dev/2014-December/015253.html here]; you may copy all these lines into your terminal as one block to run them):
<syntaxHighlight lang=bash>
mount -o bind /dev /mnt/dev
mount -o bind /proc /mnt/proc
mount -o bind /sys /mnt/sys
chroot /mnt /nix/var/nix/profiles/system/activate
chroot /mnt /run/current-system/sw/bin/bash
</syntaxHighlight>
You should now be in your NixOS system, and should be able to adjust it by e.g. editing <code>/etc/nixos/configuration.nix</code> and running <code>nixos-rebuild switch</code> as usual. Remember that you may have to establish Internet access the chroot for some commands.

Revision as of 00:36, 5 December 2019

Chroot is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree. This modified environment is called a chroot jail.

Using nixos-enter

nixos-enter allows to access a NixOS installation from a NixOS rescue system.

The nixos-enter program is part of NixOS. Before it runs provides a shell, the script mounts api filesystems like /proc and setups the profile and /etc of the target system. To use it, setup /mnt as described in the installation manual.

Then run nixos-enter:

$ nixos-enter

Manual chroot

If a NixOS rescue system is not available, the chroot can be done manually from another Linux distribution.

Mount the file system containing the NixOS to chroot into at /mnt, using e.g.:

mount /dev/relevantPartitionNameHere /mnt

.

Mount the host system's Linux run-time api file systems inside the mount, then populate /run using the activate script and chroot inside, starting a bash shell (adapted from here; you may copy all these lines into your terminal as one block to run them):

mount -o bind /dev /mnt/dev
mount -o bind /proc /mnt/proc
mount -o bind /sys /mnt/sys
chroot /mnt /nix/var/nix/profiles/system/activate
chroot /mnt /run/current-system/sw/bin/bash

You should now be in your NixOS system, and should be able to adjust it by e.g. editing /etc/nixos/configuration.nix and running nixos-rebuild switch as usual. Remember that you may have to establish Internet access the chroot for some commands.