2006-11-08 02:34:14 +00:00
|
|
|
#! @shell@
|
|
|
|
|
|
|
|
# Syntax: installer.sh <DEVICE> <NIX-EXPR>
|
2007-01-09 23:12:41 +00:00
|
|
|
# (e.g., installer.sh /mnt/root ./my-machine.nix)
|
2006-11-08 02:34:14 +00:00
|
|
|
|
|
|
|
# - mount target device
|
|
|
|
# - make Nix store etc.
|
|
|
|
# - copy closure of rescue env to target device
|
|
|
|
# - register validity
|
|
|
|
# - start the "target" installer in a chroot to the target device
|
|
|
|
# * do a nix-pull
|
|
|
|
# * nix-env -p system-profile -i <nix-expr for the configuration>
|
|
|
|
# * run hook scripts provided by packages in the configuration?
|
|
|
|
# - install/update grub
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
set -e
|
|
|
|
|
2007-01-09 23:12:41 +00:00
|
|
|
mountPoint="$1"
|
2006-12-16 23:50:10 +00:00
|
|
|
nixosDir="$2"
|
|
|
|
configuration="$3"
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2007-01-11 00:06:46 +00:00
|
|
|
if test -z "$mountPoint" -o -z "$nixosDir" -o -z "$configuration"; then
|
2007-01-23 20:09:06 +00:00
|
|
|
echo "Syntax: nixos-installer.sh <targetRootDir> <nixosDir> <configuration>"
|
2006-11-08 02:34:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2007-01-11 00:06:46 +00:00
|
|
|
if ! test -e "$mountPoint"; then
|
|
|
|
echo "mount point $mountPoint doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -e "$nixosDir"; then
|
|
|
|
echo "NixOS source directory $nixosDir doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -e "$configuration"; then
|
|
|
|
echo "configuration file $configuration doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2006-12-16 23:50:10 +00:00
|
|
|
nixosDir=$(readlink -f "$nixosDir")
|
|
|
|
configuration=$(readlink -f "$configuration")
|
2006-11-11 22:31:26 +00:00
|
|
|
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2007-01-09 23:12:41 +00:00
|
|
|
# Mount some stuff in the target root directory.
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/mnt
|
2006-11-11 22:31:26 +00:00
|
|
|
mount --rbind / $mountPoint/mnt
|
|
|
|
mount --bind /dev $mountPoint/dev
|
|
|
|
mount --bind /proc $mountPoint/proc
|
|
|
|
mount --bind /sys $mountPoint/sys
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
cleanup() {
|
2007-01-09 23:12:41 +00:00
|
|
|
# !!! don't umount any we didn't mount ourselves
|
2006-11-11 22:31:26 +00:00
|
|
|
for i in $(grep -F "$mountPoint" /proc/mounts \
|
2007-01-23 15:07:30 +00:00
|
|
|
| @perl@/bin/perl -e 'while (<>) { /^\S+\s+(\S+)\s+/; print "$1\n"; }' \
|
2006-11-11 22:31:26 +00:00
|
|
|
| sort -r);
|
|
|
|
do
|
|
|
|
umount $i
|
|
|
|
done
|
|
|
|
}
|
2006-11-11 17:59:08 +00:00
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
trap "cleanup" EXIT
|
2006-11-11 17:59:08 +00:00
|
|
|
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 01777 -p $mountPoint/tmp
|
|
|
|
mkdir -m 0755 -p $mountPoint/var
|
2006-11-11 17:59:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Create the necessary Nix directories on the target device, if they
|
|
|
|
# don't already exist.
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 0755 -p \
|
2006-11-11 17:59:08 +00: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-29 23:41:21 +00:00
|
|
|
mkdir -m 1777 -p \
|
|
|
|
$mountPoint/nix/store \
|
|
|
|
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2006-11-13 19:01:39 +00:00
|
|
|
# Get the store paths to copy from the references graph.
|
2007-01-23 15:07:30 +00:00
|
|
|
storePaths=$(@perl@/bin/perl @pathsFromGraph@ @nixClosure@)
|
2006-11-13 19:01:39 +00:00
|
|
|
|
2006-11-08 02:34:14 +00:00
|
|
|
# Copy Nix to the Nix store on the target device.
|
2007-01-09 23:12:41 +00:00
|
|
|
echo "copying Nix to $mountPoint...."
|
2006-11-13 19:01:39 +00:00
|
|
|
for i in $storePaths; do
|
2006-11-11 17:59:08 +00:00
|
|
|
echo " $i"
|
2006-11-11 22:31:26 +00:00
|
|
|
rsync -a $i $mountPoint/nix/store/
|
2006-11-11 17:59:08 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# 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 19:01:39 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-store --register-validity < @nixClosure@
|
2006-11-11 17:59:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Create the required /bin/sh symlink; otherwise lots of things
|
|
|
|
# (notably the system() function) won't work.
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/bin
|
2007-01-09 21:25:53 +00:00
|
|
|
# !!! assuming that @shell@ is in the closure
|
|
|
|
ln -sf @shell@ $mountPoint/bin/sh
|
2006-11-11 17:59:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Enable networking in the chroot.
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/etc
|
2006-11-11 17:59:08 +00:00
|
|
|
cp /etc/resolv.conf $mountPoint/etc/
|
|
|
|
|
|
|
|
|
2007-01-23 17:17:10 +00:00
|
|
|
# Pull the manifest on the CD so that everything in the Nix store on
|
|
|
|
# the CD can be copied directly.
|
|
|
|
echo "registering substitutes to speed up builds..."
|
|
|
|
chroot $mountPoint @nix@/bin/nix-store --clear-substitutes
|
2007-02-05 15:12:47 +00:00
|
|
|
if test -e /mnt/MANIFEST; then
|
|
|
|
chroot $mountPoint @nix@/bin/nix-pull file:///mnt/MANIFEST
|
|
|
|
fi
|
2007-01-23 17:17:10 +00:00
|
|
|
rm -f $mountPoint/tmp/inst-store
|
|
|
|
ln -s /mnt/nix/store $mountPoint/tmp/inst-store
|
|
|
|
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
# Do a nix-pull to speed up building.
|
2006-12-16 18:24:49 +00:00
|
|
|
if test -n "@nixpkgsURL@"; then
|
2007-01-23 17:17:10 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-pull @nixpkgsURL@/MANIFEST || true
|
2006-12-16 18:24:49 +00:00
|
|
|
fi
|
2006-11-11 17:59:08 +00:00
|
|
|
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
# Build the specified Nix expression in the target store and install
|
|
|
|
# it into the system configuration profile.
|
2007-01-23 17:17:10 +00:00
|
|
|
echo "building the system configuration..."
|
2006-11-11 22:31:26 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
|
|
|
-p /nix/var/nix/profiles/system \
|
2007-01-09 21:19:53 +00:00
|
|
|
-f "/mnt$nixosDir/system/system.nix" \
|
2006-12-17 00:10:28 +00:00
|
|
|
--arg configuration "import /mnt$configuration" \
|
2006-12-16 23:50:10 +00:00
|
|
|
--set -A system
|
|
|
|
|
|
|
|
|
|
|
|
# Copy the configuration to /etc/nixos.
|
2006-12-17 00:10:28 +00:00
|
|
|
targetConfig=$mountPoint/etc/nixos/configuration.nix
|
2007-01-09 21:28:08 +00:00
|
|
|
mkdir -p $(dirname $targetConfig)
|
2006-12-17 00:10:28 +00:00
|
|
|
if test -e $targetConfig -o -L $targetConfig; then
|
2007-01-09 23:12:41 +00:00
|
|
|
cp -f $targetConfig $targetConfig.backup-$(date "+%Y%m%d%H%M%S")
|
2006-12-16 23:50:10 +00:00
|
|
|
fi
|
2007-01-11 00:06:46 +00:00
|
|
|
if test "$configuration" != "$targetConfig"; then
|
|
|
|
cp -f $configuration $targetConfig
|
|
|
|
fi
|
2006-11-12 23:30:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Grub needs a mtab.
|
2007-01-09 23:12:41 +00:00
|
|
|
rootDevice=$(df $mountPoint | grep '^/' | sed 's^ .*^^')
|
|
|
|
echo "$rootDevice / somefs rw 0 0" > $mountPoint/etc/mtab
|
2006-11-12 23:30:03 +00:00
|
|
|
|
|
|
|
|
2006-12-16 21:48:12 +00:00
|
|
|
# Mark the target as a NixOS installation, otherwise
|
|
|
|
# switch-to-configuration will chicken out.
|
|
|
|
touch $mountPoint/etc/NIXOS
|
|
|
|
|
|
|
|
|
2006-11-12 23:30:03 +00: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 00:10:28 +00:00
|
|
|
NIXOS_INSTALL_GRUB=1 chroot $mountPoint \
|
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|