2006-11-08 02:34:14 +00:00
|
|
|
#! @shell@
|
|
|
|
|
|
|
|
# - make Nix store etc.
|
2007-02-05 15:52:55 +00:00
|
|
|
# - copy closure of Nix to target device
|
2006-11-08 02:34:14 +00:00
|
|
|
# - register validity
|
2007-02-05 15:52:55 +00:00
|
|
|
# - with a chroot to the target device:
|
2011-10-30 15:19:58 +00:00
|
|
|
# * nix-env -p /nix/var/nix/profiles/system -i <nix-expr for the configuration>
|
2014-05-08 10:29:51 +00:00
|
|
|
# * install the boot loader
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2013-09-29 14:38:57 +00:00
|
|
|
# Parse the command line for the -I flag
|
|
|
|
extraBuildFlags=()
|
|
|
|
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
i="$1"; shift 1
|
|
|
|
case "$i" in
|
2013-10-11 12:12:36 +00:00
|
|
|
-I)
|
|
|
|
given_path="$1"; shift 1
|
|
|
|
absolute_path=$(readlink -m $given_path)
|
|
|
|
extraBuildFlags+=("$i" "/mnt$absolute_path")
|
|
|
|
;;
|
2014-05-08 19:04:58 +00:00
|
|
|
--show-trace)
|
|
|
|
extraBuildFlags+=("$i")
|
|
|
|
;;
|
2013-10-11 12:12:36 +00:00
|
|
|
--help)
|
|
|
|
exec man nixos-install
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$0: unknown option \`$i'"
|
|
|
|
exit 1
|
|
|
|
;;
|
2013-09-29 14:38:57 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
set -e
|
2012-04-10 14:39:12 +00:00
|
|
|
shopt -s nullglob
|
2006-11-11 22:31:26 +00:00
|
|
|
|
2007-02-05 15:52:55 +00:00
|
|
|
if test -z "$mountPoint"; then
|
|
|
|
mountPoint=/mnt
|
|
|
|
fi
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2007-02-06 10:41:04 +00:00
|
|
|
if test -z "$NIXOS_CONFIG"; then
|
2012-11-15 21:53:57 +00:00
|
|
|
NIXOS_CONFIG=/etc/nixos/configuration.nix
|
2006-11-08 02:34:14 +00:00
|
|
|
fi
|
|
|
|
|
2007-01-11 00:06:46 +00:00
|
|
|
if ! test -e "$mountPoint"; then
|
|
|
|
echo "mount point $mountPoint doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
2007-02-05 15:52:55 +00:00
|
|
|
|
2007-02-05 21:06:59 +00:00
|
|
|
if ! grep -F -q " $mountPoint " /proc/mounts; then
|
2007-02-05 15:52:55 +00:00
|
|
|
echo "$mountPoint doesn't appear to be a mount point"
|
|
|
|
exit 1
|
|
|
|
fi
|
2012-11-22 11:04:00 +00:00
|
|
|
|
2012-11-15 21:53:57 +00:00
|
|
|
if ! test -e "$mountPoint/$NIXOS_CONFIG"; then
|
2013-04-22 20:06:27 +00:00
|
|
|
echo "configuration file $mountPoint/$NIXOS_CONFIG doesn't exist"
|
2007-01-11 00:06:46 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2012-11-22 11:04:00 +00:00
|
|
|
|
2007-01-11 00:06:46 +00:00
|
|
|
|
2012-04-10 14:39:12 +00:00
|
|
|
|
|
|
|
# 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.
|
2013-10-01 13:48:39 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/mnt $mountPoint/mnt2 $mountPoint/mnt-nixpkgs $mountPoint/etc /etc/nixos
|
2012-11-15 21:53:57 +00:00
|
|
|
mount --make-private / # systemd makes / shared, which is annoying
|
|
|
|
mount --bind / $mountPoint/mnt
|
2013-01-25 14:59:53 +00:00
|
|
|
mount --bind /nix $mountPoint/mnt/nix
|
2012-11-15 21:53:57 +00:00
|
|
|
mount --bind /nix/store $mountPoint/mnt/nix/store
|
|
|
|
mount --bind /dev $mountPoint/dev
|
|
|
|
mount --bind /dev/shm $mountPoint/dev/shm
|
|
|
|
mount --bind /proc $mountPoint/proc
|
|
|
|
mount --bind /sys $mountPoint/sys
|
2013-04-10 13:15:54 +00:00
|
|
|
mount --bind /sys/firmware/efi/efivars $mountPoint/sys/firmware/efi/efivars &>/dev/null || true
|
2012-11-15 21:53:57 +00:00
|
|
|
mount --bind $mountPoint/etc/nixos $mountPoint/mnt2
|
2012-04-10 14:39:12 +00:00
|
|
|
mount --bind /etc $mountPoint/etc
|
2012-11-15 21:53:57 +00:00
|
|
|
mount --bind $mountPoint/mnt2 $mountPoint/etc/nixos
|
2008-05-09 05:58:21 +00:00
|
|
|
|
2008-05-09 09:32:31 +00:00
|
|
|
cleanup() {
|
2012-04-10 14:39:12 +00:00
|
|
|
set +e
|
2012-11-15 21:53:57 +00:00
|
|
|
mountpoint -q $mountPoint/etc/nixos && umount $mountPoint/etc/nixos
|
|
|
|
mountpoint -q $mountPoint/etc && umount $mountPoint/etc
|
|
|
|
umount $mountPoint/mnt2
|
2013-04-11 20:12:43 +00:00
|
|
|
umount $mountPoint/mnt-nixpkgs
|
2013-04-10 19:08:21 +00:00
|
|
|
umount $mountPoint/sys/firmware/efi/efivars &>/dev/null || true
|
2012-11-15 21:53:57 +00:00
|
|
|
umount $mountPoint/sys
|
|
|
|
umount $mountPoint/proc
|
|
|
|
umount $mountPoint/dev/shm
|
|
|
|
umount $mountPoint/dev
|
|
|
|
umount $mountPoint/mnt/nix/store
|
2013-01-25 14:59:53 +00:00
|
|
|
umount $mountPoint/mnt/nix
|
2012-11-15 21:53:57 +00:00
|
|
|
umount $mountPoint/mnt
|
2013-10-01 13:48:39 +00:00
|
|
|
rmdir $mountPoint/mnt $mountPoint/mnt2 $mountPoint/mnt-nixpkgs
|
2008-05-09 09:32:31 +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 \
|
2011-10-30 15:19:58 +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-11 17:59:08 +00:00
|
|
|
|
2012-04-10 14:39:12 +00:00
|
|
|
mkdir -m 1775 -p $mountPoint/nix/store
|
2013-04-11 20:18:24 +00:00
|
|
|
build_users_group=$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"build-users-group"};')
|
2013-02-04 12:46:09 +00:00
|
|
|
if test -n "$build_users_group"; then
|
|
|
|
chown root:"$build_users_group" $mountPoint/nix/store
|
|
|
|
else
|
|
|
|
chown root $mountPoint/nix/store
|
|
|
|
fi
|
2012-04-10 14:39:12 +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
|
|
|
|
2008-09-08 12:40:05 +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"
|
2012-05-25 14:06:46 +00:00
|
|
|
chattr -R -i $mountPoint/$i 2> /dev/null || true # clear immutable bit
|
2011-10-30 15:19:58 +00:00
|
|
|
rsync -a $i $mountPoint/nix/store/
|
2006-11-11 17:59:08 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
|
2009-06-17 10:07:31 +00:00
|
|
|
# We don't have locale-archive in the chroot, so clear $LANG.
|
|
|
|
export LANG=
|
2012-04-10 13:28:17 +00:00
|
|
|
export LC_ALL=
|
|
|
|
export LC_TIME=
|
2009-06-17 10:07:31 +00:00
|
|
|
|
|
|
|
|
2013-02-06 17:20:52 +00:00
|
|
|
# There is no daemon in the chroot
|
|
|
|
unset NIX_REMOTE
|
|
|
|
|
|
|
|
|
2012-04-10 14:39:12 +00:00
|
|
|
# Create a temporary Nix config file that causes the nixbld users to
|
|
|
|
# be used.
|
2013-02-04 12:46:09 +00:00
|
|
|
if test -n "$build_users_group"; then
|
|
|
|
echo "build-users-group = $build_users_group" > $mountPoint/tmp/nix.conf
|
|
|
|
fi
|
2013-04-11 20:18:24 +00:00
|
|
|
binary_caches=$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"binary-caches"};')
|
2013-02-04 14:44:32 +00:00
|
|
|
if test -n "$binary_caches"; then
|
2013-02-05 11:39:18 +00:00
|
|
|
echo "binary-caches = $binary_caches" >> $mountPoint/tmp/nix.conf
|
2013-02-04 14:44:32 +00:00
|
|
|
fi
|
2012-04-10 14:39:12 +00:00
|
|
|
export NIX_CONF_DIR=/tmp
|
|
|
|
|
|
|
|
|
2006-11-11 17:59:08 +00: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 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
|
|
|
|
|
|
|
|
2012-04-10 15:36:50 +00:00
|
|
|
if test -n "$NIXOS_PREPARE_CHROOT_ONLY"; then
|
|
|
|
echo "User requested only to prepare chroot. Exiting."
|
2012-05-25 02:30:52 +00:00
|
|
|
exit 0
|
2012-04-10 15:36:50 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-09-08 12:40:05 +00: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 17:17:10 +00:00
|
|
|
|
2013-09-16 11:28:59 +00:00
|
|
|
p=@nix@/libexec/nix/substituters
|
|
|
|
export NIX_SUBSTITUTERS=$p/copy-from-other-stores.pl:$p/download-from-binary-cache.pl
|
|
|
|
|
2007-01-23 17:17:10 +00:00
|
|
|
|
2012-04-10 15:36:50 +00: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 17:59:08 +00:00
|
|
|
|
2012-04-23 00:41:37 +00:00
|
|
|
# Get the absolute path to the NixOS/Nixpkgs sources.
|
2013-04-11 20:12:43 +00:00
|
|
|
mount --bind $(readlink -f $(nix-instantiate --find-file nixpkgs)) $mountPoint/mnt-nixpkgs
|
2012-04-23 00:41:37 +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..."
|
2013-10-01 13:48:39 +00:00
|
|
|
NIX_PATH="nixpkgs=/mnt-nixpkgs:nixos=/mnt-nixpkgs/nixos:nixos-config=$NIXOS_CONFIG" NIXOS_CONFIG= \
|
2012-03-02 12:38:22 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
2014-05-08 19:04:58 +00:00
|
|
|
"${extraBuildFlags[@]}" -p /nix/var/nix/profiles/system -f '<nixos>' --set -A system
|
2006-12-16 23:50:10 +00:00
|
|
|
|
|
|
|
|
2012-04-23 00:41:37 +00:00
|
|
|
# Copy the NixOS/Nixpkgs sources to the target as the initial contents
|
|
|
|
# of the NixOS channel.
|
2012-04-26 13:16:58 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles
|
|
|
|
mkdir -m 1777 -p $mountPoint/nix/var/nix/profiles/per-user
|
|
|
|
mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles/per-user/root
|
2013-09-29 14:38:57 +00:00
|
|
|
srcs=$(nix-env "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "")
|
2013-02-04 12:38:54 +00:00
|
|
|
if test -n "$srcs"; then
|
|
|
|
echo "copying NixOS/Nixpkgs sources..."
|
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
2013-09-29 14:38:57 +00:00
|
|
|
"${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -i "$srcs" --quiet
|
2013-02-04 12:38:54 +00:00
|
|
|
fi
|
2012-04-23 00:41:37 +00:00
|
|
|
mkdir -m 0700 -p $mountPoint/root/.nix-defexpr
|
2012-05-24 23:37:06 +00:00
|
|
|
ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
|
2006-11-12 23:30:03 +00:00
|
|
|
|
|
|
|
|
2012-05-15 13:50:36 +00: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
|
|
|
|
|
|
|
|
|
2008-05-09 10:08:02 +00:00
|
|
|
# Grub needs an mtab.
|
|
|
|
ln -sfn /proc/mounts $mountPoint/etc/mtab
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-05-09 10:08:02 +00:00
|
|
|
|
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 \
|
2011-10-30 15:19:58 +00:00
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|