@@ -102,7 +102,7 @@ fi
102102extraBuildFlags+=(--option "build-users-group" "$buildUsersGroup")
103103
104104# Inherit binary caches from the host
105−# TODO: will this still work with Nix 1.12 now that it has no perl? Probably not...
105+# TODO: will this still work with Nix 1.12 now that it has no perl? Probably not...
106106binary_caches="$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"binary-caches"};')"
107107extraBuildFlags+=(--option "binary-caches" "$binary_caches")
108108
@@ -113,8 +113,33 @@ if [[ -z "$closure" ]]; then
113113fi
114114unset NIXOS_CONFIG
115115
116−# TODO: do I need to set NIX_SUBSTITUTERS here or is the --option binary-caches above enough?
117−
116+# These get created in nixos-prepare-root as well, but we want to make sure they're here in case we're
117+# running with --chroot. TODO: --chroot should just be split into a separate tool.
118+mkdir -m 0755 -p "$mountPoint/dev" "$mountPoint/proc" "$mountPoint/sys"
119+
120+# Set up some bind mounts we'll want regardless of chroot or not
121+mount --rbind /dev "$mountPoint/dev"
122+mount --rbind /proc "$mountPoint/proc"
123+mount --rbind /sys "$mountPoint/sys"
124+
125+# If we asked for a chroot, that means we're not actually installing anything (yeah I was confused too)
126+# and we just want to run a command in the context of a $mountPoint that we're assuming has already been
127+# set up by a previous nixos-install invocation. In that case we set up some remaining bind mounts and
128+# exec the requested command, skipping the rest of the installation procedure.
129+if [ -n "$runChroot" ]; then
130+ mount -t tmpfs -o "mode=0755" none $mountPoint/run
131+ rm -rf $mountPoint/var/run
132+ ln -s /run $mountPoint/var/run
133+ for f in /etc/resolv.conf /etc/hosts; do rm -f $mountPoint/$f; [ -f "$f" ] && cp -Lf $f $mountPoint/etc/; done
134+ for f in /etc/passwd /etc/group; do touch $mountPoint/$f; [ -f "$f" ] && mount --rbind -o ro $f $mountPoint/$f; done
135+
136+ if ! [ -L $mountPoint/nix/var/nix/profiles/system ]; then
137+ echo "$0: installation not finished; cannot chroot into installation directory"
138+ exit 1
139+ fi
140+ ln -s /nix/var/nix/profiles/system $mountPoint/run/current-system
141+ exec chroot $mountPoint "${chrootCommand[@]}"
142+fi
118143
119144# A place to drop temporary closures
120145trap "rm -rf $tmpdir" EXIT
@@ -153,9 +178,7 @@ nix-store --export $channel_root > $channel_closure
153178# nixos-prepare-root doesn't currently do anything with file ownership, so we set it up here instead
154179chown @root_uid@:@nixbld_gid@ $mountPoint/nix/store
155180
156−mount --rbind /dev $mountPoint/dev
157−mount --rbind /proc $mountPoint/proc
158−mount --rbind /sys $mountPoint/sys
181+
159182
160183# Grub needs an mtab.
161184ln -sfn /proc/mounts $mountPoint/etc/mtab