2006-11-08 03:34:14 +01:00
|
|
|
#! @shell@
|
|
|
|
|
2007-02-05 16:52:55 +01:00
|
|
|
# - [mount target device] <- currently disabled
|
2006-11-08 03:34:14 +01:00
|
|
|
# - make Nix store etc.
|
2007-02-05 16:52:55 +01:00
|
|
|
# - copy closure of Nix to target device
|
2006-11-08 03:34:14 +01:00
|
|
|
# - register validity
|
2007-02-05 16:52:55 +01:00
|
|
|
# - with a chroot to the target device:
|
2006-11-08 03:34:14 +01:00
|
|
|
# * do a nix-pull
|
2011-10-30 16:19:58 +01:00
|
|
|
# * nix-env -p /nix/var/nix/profiles/system -i <nix-expr for the configuration>
|
2007-02-05 16:52:55 +01:00
|
|
|
# * run the activation script of the configuration (also installs Grub)
|
2006-11-08 03:34:14 +01:00
|
|
|
|
2006-11-11 23:31:26 +01:00
|
|
|
set -e
|
2012-04-10 16:39:12 +02:00
|
|
|
shopt -s nullglob
|
2006-11-11 23:31:26 +01:00
|
|
|
|
2007-02-05 16:52:55 +01:00
|
|
|
if test -z "$mountPoint"; then
|
|
|
|
mountPoint=/mnt
|
|
|
|
fi
|
2006-11-08 03:34:14 +01:00
|
|
|
|
2007-02-06 11:41:04 +01:00
|
|
|
if test -z "$NIXOS_CONFIG"; then
|
2012-03-07 22:17:46 +01:00
|
|
|
NIXOS_CONFIG=/mnt/etc/nixos/configuration.nix
|
2006-11-08 03:34:14 +01:00
|
|
|
fi
|
|
|
|
|
2007-01-11 01:06:46 +01:00
|
|
|
if ! test -e "$mountPoint"; then
|
|
|
|
echo "mount point $mountPoint doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
2007-02-05 16:52:55 +01:00
|
|
|
|
2007-02-05 22:06:59 +01:00
|
|
|
if ! grep -F -q " $mountPoint " /proc/mounts; then
|
2007-02-05 16:52:55 +01:00
|
|
|
echo "$mountPoint doesn't appear to be a mount point"
|
|
|
|
exit 1
|
|
|
|
fi
|
2007-01-11 01:06:46 +01:00
|
|
|
|
2012-03-07 22:17:46 +01:00
|
|
|
if ! test -e "$NIXOS_CONFIG"; then
|
|
|
|
echo "configuration file $NIXOS_CONFIG doesn't exist"
|
2007-01-11 01:06:46 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2012-04-10 16:39:12 +02:00
|
|
|
# Do a nix-pull to speed up building.
|
2012-04-11 00:17:37 +02:00
|
|
|
if test -n "@nixosURL@" -a ${NIXOS_PULL:-1} != 0; then
|
|
|
|
@nix@/bin/nix-pull @nixosURL@/MANIFEST || true
|
2012-04-10 16:39:12 +02:00
|
|
|
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.
|
|
|
|
mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/mnt $mountPoint/etc
|
2012-04-10 15:25:55 +02:00
|
|
|
mount --rbind /dev $mountPoint/dev
|
|
|
|
mount --rbind /proc $mountPoint/proc
|
|
|
|
mount --rbind /sys $mountPoint/sys
|
2008-05-09 07:58:21 +02:00
|
|
|
mount --rbind / $mountPoint/mnt
|
2012-04-10 16:39:12 +02:00
|
|
|
mount --bind /etc $mountPoint/etc
|
|
|
|
mount --bind $mountPoint/mnt/$mountPoint/etc/nixos $mountPoint/etc/nixos
|
2008-05-09 07:58:21 +02:00
|
|
|
|
2008-05-09 11:32:31 +02:00
|
|
|
cleanup() {
|
2012-04-10 16:39:12 +02:00
|
|
|
set +e
|
2012-04-10 15:25:55 +02:00
|
|
|
umount -l $mountPoint/mnt
|
|
|
|
umount -l $mountPoint/dev
|
|
|
|
umount -l $mountPoint/proc
|
|
|
|
umount -l $mountPoint/sys
|
2012-04-10 16:39:12 +02:00
|
|
|
mountpoint -q $mountPoint/etc && umount -l $mountPoint/etc
|
2008-05-09 11:32:31 +02:00
|
|
|
}
|
|
|
|
|
2006-11-11 23:31:26 +01:00
|
|
|
trap "cleanup" EXIT
|
2006-11-11 18:59:08 +01:00
|
|
|
|
2006-11-13 12:42:23 +01:00
|
|
|
mkdir -m 01777 -p $mountPoint/tmp
|
|
|
|
mkdir -m 0755 -p $mountPoint/var
|
2006-11-11 18:59:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Create the necessary Nix directories on the target device, if they
|
|
|
|
# don't already exist.
|
2006-11-13 12:42:23 +01:00
|
|
|
mkdir -m 0755 -p \
|
2011-10-30 16:19:58 +01:00
|
|
|
$mountPoint/nix/var/nix/gcroots \
|
|
|
|
$mountPoint/nix/var/nix/temproots \
|
|
|
|
$mountPoint/nix/var/nix/manifests \
|
|
|
|
$mountPoint/nix/var/nix/userpool \
|
|
|
|
$mountPoint/nix/var/nix/profiles \
|
|
|
|
$mountPoint/nix/var/nix/db \
|
|
|
|
$mountPoint/nix/var/log/nix/drvs
|
2006-11-11 18:59:08 +01:00
|
|
|
|
2012-04-10 16:39:12 +02:00
|
|
|
mkdir -m 1775 -p $mountPoint/nix/store
|
|
|
|
chown root.nixbld $mountPoint/nix/store
|
|
|
|
|
|
|
|
|
2006-11-13 20:01:39 +01:00
|
|
|
# Get the store paths to copy from the references graph.
|
2007-01-23 16:07:30 +01:00
|
|
|
storePaths=$(@perl@/bin/perl @pathsFromGraph@ @nixClosure@)
|
2006-11-13 20:01:39 +01:00
|
|
|
|
2008-09-08 14:40:05 +02:00
|
|
|
|
2006-11-08 03:34:14 +01:00
|
|
|
# Copy Nix to the Nix store on the target device.
|
2007-01-10 00:12:41 +01:00
|
|
|
echo "copying Nix to $mountPoint...."
|
2006-11-13 20:01:39 +01:00
|
|
|
for i in $storePaths; do
|
2006-11-11 18:59:08 +01:00
|
|
|
echo " $i"
|
2011-10-30 16:19:58 +01:00
|
|
|
rsync -a $i $mountPoint/nix/store/
|
2006-11-11 18:59:08 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
|
2009-06-17 12:07:31 +02:00
|
|
|
# We don't have locale-archive in the chroot, so clear $LANG.
|
|
|
|
export LANG=
|
2012-04-10 15:28:17 +02:00
|
|
|
export LC_ALL=
|
|
|
|
export LC_TIME=
|
2009-06-17 12:07:31 +02:00
|
|
|
|
|
|
|
|
2012-04-10 16:39:12 +02:00
|
|
|
# Create a temporary Nix config file that causes the nixbld users to
|
|
|
|
# be used.
|
2012-04-23 02:41:37 +02:00
|
|
|
echo "build-users-group = nixbld" > $mountPoint/tmp/nix.conf
|
2012-04-10 16:39:12 +02:00
|
|
|
export NIX_CONF_DIR=/tmp
|
|
|
|
|
|
|
|
|
2006-11-11 18:59:08 +01:00
|
|
|
# Register the paths in the Nix closure as valid. This is necessary
|
|
|
|
# to prevent them from being deleted the first time we install
|
|
|
|
# something. (I.e., Nix will see that, e.g., the glibc path is not
|
|
|
|
# valid, delete it to get it out of the way, but as a result nothing
|
|
|
|
# will work anymore.)
|
2006-11-13 20:01:39 +01:00
|
|
|
chroot $mountPoint @nix@/bin/nix-store --register-validity < @nixClosure@
|
2006-11-11 18:59:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Create the required /bin/sh symlink; otherwise lots of things
|
|
|
|
# (notably the system() function) won't work.
|
2006-11-13 12:42:23 +01:00
|
|
|
mkdir -m 0755 -p $mountPoint/bin
|
2007-01-09 22:25:53 +01:00
|
|
|
# !!! assuming that @shell@ is in the closure
|
|
|
|
ln -sf @shell@ $mountPoint/bin/sh
|
2006-11-11 18:59:08 +01:00
|
|
|
|
|
|
|
|
2012-04-10 17:36:50 +02:00
|
|
|
if test -n "$NIXOS_PREPARE_CHROOT_ONLY"; then
|
|
|
|
echo "User requested only to prepare chroot. Exiting."
|
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-09-08 14:40:05 +02:00
|
|
|
# Make the build below copy paths from the CD if possible. Note that
|
|
|
|
# /mnt in the chroot is the root of the CD.
|
|
|
|
export NIX_OTHER_STORES=/mnt/nix:$NIX_OTHER_STORES
|
2007-01-23 18:17:10 +01:00
|
|
|
|
|
|
|
|
2012-04-10 17:36:50 +02:00
|
|
|
# Make manifests available in the chroot.
|
|
|
|
rm -f $mountPoint/nix/var/nix/manifests/*
|
|
|
|
for i in /nix/var/nix/manifests/*.nixmanifest; do
|
|
|
|
chroot $mountPoint @nix@/bin/nix-store -r "$(readlink -f "$i")" > /dev/null
|
|
|
|
cp -pd "$i" $mountPoint/nix/var/nix/manifests/
|
|
|
|
done
|
|
|
|
|
2006-11-11 18:59:08 +01:00
|
|
|
|
2012-04-23 02:41:37 +02:00
|
|
|
# Get the absolute path to the NixOS/Nixpkgs sources.
|
|
|
|
srcs=$(nix-env -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path)
|
|
|
|
|
|
|
|
|
2006-11-11 23:31:26 +01:00
|
|
|
# Build the specified Nix expression in the target store and install
|
|
|
|
# it into the system configuration profile.
|
2007-01-23 18:17:10 +01:00
|
|
|
echo "building the system configuration..."
|
2012-04-23 02:41:37 +02:00
|
|
|
NIX_PATH="/mnt$srcs/nixos:nixos-config=/mnt$NIXOS_CONFIG" NIXOS_CONFIG= \
|
2012-03-02 13:38:22 +01:00
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
2012-03-07 22:17:46 +01:00
|
|
|
-p /nix/var/nix/profiles/system -f '<nixos>' --set -A system --show-trace
|
2006-12-17 00:50:10 +01:00
|
|
|
|
|
|
|
|
2012-04-10 16:39:12 +02:00
|
|
|
# We're done building/downloading, so we don't need the /etc bind
|
|
|
|
# mount anymore. In fact, below we want to modify the target's /etc.
|
|
|
|
umount $mountPoint/etc/nixos
|
|
|
|
umount $mountPoint/etc
|
|
|
|
|
|
|
|
|
2012-04-23 02:41:37 +02:00
|
|
|
# Copy the NixOS/Nixpkgs sources to the target as the initial contents
|
|
|
|
# of the NixOS channel.
|
|
|
|
echo "copying NixOS/Nixpkgs sources..."
|
|
|
|
mkdir -p $mountPoint/nix/var/nix/profiles/per-user/root
|
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
|
|
|
-p /nix/var/nix/profiles/per-user/root/channels -i "$srcs" --quiet
|
|
|
|
mkdir -m 0700 -p $mountPoint/root/.nix-defexpr
|
|
|
|
ln -s /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
|
2006-11-13 00:30:03 +01:00
|
|
|
|
|
|
|
|
2008-05-09 12:08:02 +02:00
|
|
|
# Grub needs an mtab.
|
|
|
|
ln -sfn /proc/mounts $mountPoint/etc/mtab
|
|
|
|
|
|
|
|
|
2006-12-16 22:48:12 +01:00
|
|
|
# Mark the target as a NixOS installation, otherwise
|
|
|
|
# switch-to-configuration will chicken out.
|
|
|
|
touch $mountPoint/etc/NIXOS
|
|
|
|
|
2008-05-09 12:08:02 +02:00
|
|
|
|
2006-11-13 00:30:03 +01:00
|
|
|
# Switch to the new system configuration. This will install Grub with
|
|
|
|
# a menu default pointing at the kernel/initrd/etc of the new
|
|
|
|
# configuration.
|
|
|
|
echo "finalising the installation..."
|
2006-12-17 01:10:28 +01:00
|
|
|
NIXOS_INSTALL_GRUB=1 chroot $mountPoint \
|
2011-10-30 16:19:58 +01:00
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|