2006-11-08 02:34:14 +00:00
|
|
|
#! @shell@
|
|
|
|
|
|
|
|
# Syntax: installer.sh <DEVICE> <NIX-EXPR>
|
|
|
|
# (e.g., installer.sh /dev/hda1 ./my-machine.nix)
|
|
|
|
|
|
|
|
# - 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
|
|
|
|
|
2006-11-08 02:34:14 +00:00
|
|
|
targetDevice="$1"
|
2006-11-11 22:31:26 +00:00
|
|
|
nixExpr="$2"
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
if test -z "$targetDevice" -o -z "$nixExpr"; then
|
2006-11-23 23:22:43 +00:00
|
|
|
echo "Syntax: installer.sh <targetDevice> <nixExpr>"
|
2006-11-08 02:34:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
nixExpr=$(readlink -f "$nixExpr")
|
|
|
|
|
2006-11-08 02:34:14 +00:00
|
|
|
|
|
|
|
# Make sure that the target device isn't mounted.
|
2006-11-11 22:31:26 +00:00
|
|
|
umount "$targetDevice" 2> /dev/null || true
|
2006-11-08 02:34:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Check it.
|
2006-11-12 23:30:03 +00:00
|
|
|
fsck -n "$targetDevice"
|
2006-11-08 02:34:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Mount the target device.
|
|
|
|
mountPoint=/tmp/inst-mnt
|
|
|
|
mkdir -p $mountPoint
|
2006-11-11 22:31:26 +00:00
|
|
|
mount "$targetDevice" $mountPoint
|
|
|
|
|
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() {
|
|
|
|
for i in $(grep -F "$mountPoint" /proc/mounts \
|
|
|
|
| perl -e 'while (<>) { /^\S+\s+(\S+)\s+/; print "$1\n"; }' \
|
|
|
|
| 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.
|
2006-11-17 14:13:21 +00:00
|
|
|
storePaths=$(@shell@ @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.
|
2006-11-11 17:59:08 +00:00
|
|
|
echo "copying Nix to $targetDevice...."
|
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
|
2006-11-11 17:59:08 +00:00
|
|
|
ln -sf $(type -tp sh) $mountPoint/bin/sh
|
|
|
|
|
|
|
|
|
|
|
|
# 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/
|
|
|
|
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
# Do a nix-pull to speed up building.
|
2006-11-11 17:59:08 +00:00
|
|
|
nixpkgsURL=http://nix.cs.uu.nl/dist/nix/nixpkgs-0.11pre6984
|
2006-11-13 11:42:23 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-pull $nixpkgsURL/MANIFEST
|
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.
|
2006-11-11 17:59:08 +00:00
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
#rm -rf $mountPoint/scratch
|
|
|
|
#mkdir $mountPoint/scratch
|
|
|
|
#curl $nixpkgsURL/nixexprs.tar.bz2 | tar xj -C $mountPoint/scratch
|
|
|
|
#nixpkgsName=$(cd $mountPoint/scratch && ls)
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
|
|
|
-p /nix/var/nix/profiles/system \
|
2006-12-13 12:21:37 +00:00
|
|
|
-f "/mnt/$nixExpr" -i --set system
|
2006-11-12 23:30:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Grub needs a mtab.
|
|
|
|
echo "$targetDevice / somefs rw 0 0" > $mountPoint/etc/mtab
|
|
|
|
|
|
|
|
|
|
|
|
# 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-11-27 13:51:08 +00:00
|
|
|
NIXOS_INSTALL_GRUB=1 chroot $mountPoint /nix/var/nix/profiles/system/bin/switch-to-configuration
|