From 3ef8d6ad5c33a0bc9d0350155c19cac757ca16dc Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 9 May 2014 00:25:05 +0200 Subject: [PATCH] nixos-install: Add operation --chroot "nixos-install --chroot" runs a command (by default a login shell) in a chroot inside the NixOS installation in /mnt. This might useful for poking around a new installation. --- nixos/doc/manual/development.xml | 14 +++-- .../modules/installer/tools/nixos-install.sh | 55 ++++++++++++------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/nixos/doc/manual/development.xml b/nixos/doc/manual/development.xml index a93b4b163bd5..6873710205d9 100644 --- a/nixos/doc/manual/development.xml +++ b/nixos/doc/manual/development.xml @@ -729,18 +729,22 @@ $ mount -o loop -t iso9660 ./result/iso/cd.iso /mnt/iso Testing the installer -Building, burning, and -booting from an installation CD is rather +Building, burning, and booting from an installation CD is rather tedious, so here is a quick way to see if the installer works properly: $ nix-build -A config.system.build.nixos-install -$ dd if=/dev/zero of=diskimage seek=2G count=0 bs=1 -$ yes | mke2fs -j diskimage -$ mount -o loop diskimage /mnt +$ mount -t tmpfs none /mnt $ ./result/bin/nixos-install +To start a login shell in the new NixOS installation in +/mnt: + + +$ ./result/bin/nixos-install --chroot + + diff --git a/nixos/modules/installer/tools/nixos-install.sh b/nixos/modules/installer/tools/nixos-install.sh index e708521c93f6..0ada85a26e2f 100644 --- a/nixos/modules/installer/tools/nixos-install.sh +++ b/nixos/modules/installer/tools/nixos-install.sh @@ -20,6 +20,7 @@ fi # Parse the command line for the -I flag extraBuildFlags=() +chrootCommand=(/run/current-system/sw/bin/bash) while [ "$#" -gt 0 ]; do i="$1"; shift 1 @@ -32,6 +33,11 @@ while [ "$#" -gt 0 ]; do --show-trace) extraBuildFlags+=("$i") ;; + --chroot) + runChroot=1 + chrootCommand=("$@") + break + ;; --help) exec man nixos-install exit 1 @@ -50,10 +56,6 @@ if test -z "$mountPoint"; then mountPoint=/mnt fi -if test -z "$NIXOS_CONFIG"; then - NIXOS_CONFIG=/etc/nixos/configuration.nix -fi - if ! test -e "$mountPoint"; then echo "mount point $mountPoint doesn't exist" exit 1 @@ -64,15 +66,8 @@ if ! grep -F -q " $mountPoint " /proc/mounts; then exit 1 fi -if ! test -e "$mountPoint/$NIXOS_CONFIG"; then - echo "configuration file $mountPoint/$NIXOS_CONFIG doesn't exist" - exit 1 -fi - -# Mount some stuff in the target root directory. We bind-mount /etc -# into the chroot because we need networking and the nixbld user -# accounts in /etc/passwd. But we do need the target's /etc/nixos. +# Mount some stuff in the target root directory. mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/etc $mountPoint/run mkdir -m 01777 -p $mountPoint/tmp mkdir -m 0755 -p $mountPoint/tmp/root @@ -81,12 +76,38 @@ mount --rbind /dev $mountPoint/dev mount --rbind /proc $mountPoint/proc mount --rbind /sys $mountPoint/sys mount --rbind / $mountPoint/tmp/root -mount --bind /etc $mountPoint/etc -mount --bind $mountPoint/tmp/root/$mountPoint/etc/nixos $mountPoint/etc/nixos mount -t tmpfs -o "mode=0755" none $mountPoint/run mount -t tmpfs -o "mode=0755" none $mountPoint/var/setuid-wrappers +if [ -n "$runChroot" ]; then + if ! [ -L $mountPoint/nix/var/nix/profiles/system ]; then + echo "$0: installation not finished; cannot chroot into installation directory" + exit 1 + fi + ln -s /nix/var/nix/profiles/system $mountPoint/run/current-system + exec chroot $mountPoint "${chrootCommand[@]}" +fi + + +# Bind-mount /etc into the chroot because we need networking and the +# nixbld user accounts in /etc/passwd. But we do need the target's +# /etc/nixos. +mount --bind /etc $mountPoint/etc +mount --bind $mountPoint/tmp/root/$mountPoint/etc/nixos $mountPoint/etc/nixos + + +# Get the path of the NixOS configuration file. +if test -z "$NIXOS_CONFIG"; then + NIXOS_CONFIG=/etc/nixos/configuration.nix +fi + +if ! test -e "$mountPoint/$NIXOS_CONFIG"; then + echo "configuration file $mountPoint/$NIXOS_CONFIG doesn't exist" + exit 1 +fi + + # Create the necessary Nix directories on the target device, if they # don't already exist. mkdir -m 0755 -p \ @@ -147,12 +168,6 @@ mkdir -m 0755 -p $mountPoint/bin ln -sf @shell@ $mountPoint/bin/sh -if test -n "$NIXOS_PREPARE_CHROOT_ONLY"; then - echo "User requested only to prepare chroot. Exiting." - exit 0 -fi - - # Make the build below copy paths from the CD if possible. Note that # /tmp/root in the chroot is the root of the CD. export NIX_OTHER_STORES=/tmp/root/nix:$NIX_OTHER_STORES