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 11:29:51 +01:00
|
|
|
# * install the boot loader
|
2006-11-08 02:34:14 +00:00
|
|
|
|
2014-09-03 22:10:40 +01:00
|
|
|
# Ensure a consistent umask.
|
|
|
|
umask 0022
|
|
|
|
|
2014-05-08 22:20:36 +01:00
|
|
|
# Re-exec ourselves in a private mount namespace so that our bind
|
|
|
|
# mounts get cleaned up automatically.
|
|
|
|
if [ "$(id -u)" = 0 ]; then
|
|
|
|
if [ -z "$NIXOS_INSTALL_REEXEC" ]; then
|
|
|
|
export NIXOS_INSTALL_REEXEC=1
|
2014-05-09 12:19:02 +01:00
|
|
|
exec unshare --mount --uts -- "$0" "$@"
|
2014-05-08 22:20:36 +01:00
|
|
|
else
|
|
|
|
mount --make-rprivate /
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-09-29 15:38:57 +01:00
|
|
|
# Parse the command line for the -I flag
|
|
|
|
extraBuildFlags=()
|
2014-05-08 23:25:05 +01:00
|
|
|
chrootCommand=(/run/current-system/sw/bin/bash)
|
2016-08-16 04:02:20 +01:00
|
|
|
buildUsersGroup="nixbld"
|
2013-09-29 15:38:57 +01:00
|
|
|
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
i="$1"; shift 1
|
|
|
|
case "$i" in
|
2015-03-11 16:44:31 +00:00
|
|
|
--max-jobs|-j|--cores|-I)
|
|
|
|
j="$1"; shift 1
|
|
|
|
extraBuildFlags+=("$i" "$j")
|
|
|
|
;;
|
|
|
|
--option)
|
|
|
|
j="$1"; shift 1
|
|
|
|
k="$1"; shift 1
|
|
|
|
extraBuildFlags+=("$i" "$j" "$k")
|
2013-10-11 13:12:36 +01:00
|
|
|
;;
|
2014-08-24 14:57:00 +01:00
|
|
|
--root)
|
|
|
|
mountPoint="$1"; shift 1
|
|
|
|
;;
|
2016-07-24 14:45:24 +01:00
|
|
|
--closure)
|
|
|
|
closure="$1"; shift 1
|
2016-08-16 04:02:20 +01:00
|
|
|
buildUsersGroup=""
|
2016-07-24 14:45:24 +01:00
|
|
|
;;
|
|
|
|
--no-channel-copy)
|
|
|
|
noChannelCopy=1
|
|
|
|
;;
|
|
|
|
--no-root-passwd)
|
|
|
|
noRootPasswd=1
|
|
|
|
;;
|
|
|
|
--no-bootloader)
|
2016-08-16 01:15:27 +01:00
|
|
|
noBootLoader=1
|
2016-07-24 14:45:24 +01:00
|
|
|
;;
|
2014-05-08 20:04:58 +01:00
|
|
|
--show-trace)
|
|
|
|
extraBuildFlags+=("$i")
|
|
|
|
;;
|
2014-05-08 23:25:05 +01:00
|
|
|
--chroot)
|
|
|
|
runChroot=1
|
2015-04-28 03:16:38 +01:00
|
|
|
if [[ "$@" != "" ]]; then
|
|
|
|
chrootCommand=("$@")
|
|
|
|
fi
|
2014-05-08 23:25:05 +01:00
|
|
|
break
|
|
|
|
;;
|
2013-10-11 13:12:36 +01:00
|
|
|
--help)
|
|
|
|
exec man nixos-install
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$0: unknown option \`$i'"
|
|
|
|
exit 1
|
|
|
|
;;
|
2013-09-29 15:38:57 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2006-11-11 22:31:26 +00:00
|
|
|
set -e
|
2012-04-10 15:39:12 +01: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-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
|
|
|
|
2012-11-22 11:04:00 +00:00
|
|
|
|
2014-05-08 23:25:05 +01:00
|
|
|
# Mount some stuff in the target root directory.
|
2014-05-08 23:49:18 +01:00
|
|
|
mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/etc $mountPoint/run $mountPoint/home
|
2006-11-13 11:42:23 +00:00
|
|
|
mkdir -m 01777 -p $mountPoint/tmp
|
2014-05-08 22:20:36 +01:00
|
|
|
mkdir -m 0755 -p $mountPoint/tmp/root
|
2017-01-29 04:48:03 +00:00
|
|
|
mkdir -m 0755 -p $mountPoint/var
|
2015-01-15 17:28:47 +00:00
|
|
|
mkdir -m 0700 -p $mountPoint/root
|
2014-05-08 22:20:36 +01:00
|
|
|
mount --rbind /dev $mountPoint/dev
|
|
|
|
mount --rbind /proc $mountPoint/proc
|
|
|
|
mount --rbind /sys $mountPoint/sys
|
|
|
|
mount --rbind / $mountPoint/tmp/root
|
2014-05-08 23:04:48 +01:00
|
|
|
mount -t tmpfs -o "mode=0755" none $mountPoint/run
|
2014-05-08 23:49:18 +01:00
|
|
|
rm -rf $mountPoint/var/run
|
|
|
|
ln -s /run $mountPoint/var/run
|
2016-08-16 04:02:20 +01:00
|
|
|
for f in /etc/resolv.conf /etc/hosts; do rm -f $mountPoint/$f; [ -f "$f" ] && cp -Lf $f $mountPoint/etc/; done
|
|
|
|
for f in /etc/passwd /etc/group; do touch $mountPoint/$f; [ -f "$f" ] && mount --rbind -o ro $f $mountPoint/$f; done
|
2006-11-11 17:59:08 +00:00
|
|
|
|
2016-05-20 13:30:56 +01:00
|
|
|
cp -Lf "@cacert@" "$mountPoint/tmp/ca-cert.crt"
|
|
|
|
export SSL_CERT_FILE=/tmp/ca-cert.crt
|
|
|
|
# For Nix 1.7
|
|
|
|
export CURL_CA_BUNDLE=/tmp/ca-cert.crt
|
2006-11-11 17:59:08 +00:00
|
|
|
|
2014-05-08 23:25:05 +01:00
|
|
|
if [ -n "$runChroot" ]; then
|
|
|
|
if ! [ -L $mountPoint/nix/var/nix/profiles/system ]; then
|
|
|
|
echo "$0: installation not finished; cannot chroot into installation directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
ln -s /nix/var/nix/profiles/system $mountPoint/run/current-system
|
|
|
|
exec chroot $mountPoint "${chrootCommand[@]}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Get the path of the NixOS configuration file.
|
|
|
|
if test -z "$NIXOS_CONFIG"; then
|
|
|
|
NIXOS_CONFIG=/etc/nixos/configuration.nix
|
|
|
|
fi
|
|
|
|
|
2016-07-24 14:45:24 +01:00
|
|
|
if [ ! -e "$mountPoint/$NIXOS_CONFIG" ] && [ -z "$closure" ]; then
|
2014-05-08 23:25:05 +01:00
|
|
|
echo "configuration file $mountPoint/$NIXOS_CONFIG doesn't exist"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
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/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
|
|
|
|
2015-04-07 12:15:48 +01:00
|
|
|
mkdir -m 1775 -p $mountPoint/nix/store
|
2016-08-16 04:02:20 +01:00
|
|
|
chown @root_uid@:@nixbld_gid@ $mountPoint/nix/store
|
2012-04-10 15:39:12 +01:00
|
|
|
|
|
|
|
|
2014-05-08 22:40:17 +01:00
|
|
|
# There is no daemon in the chroot.
|
|
|
|
unset NIX_REMOTE
|
2006-11-11 17:59:08 +00:00
|
|
|
|
|
|
|
|
2009-06-17 11:07:31 +01:00
|
|
|
# We don't have locale-archive in the chroot, so clear $LANG.
|
|
|
|
export LANG=
|
2012-04-10 14:28:17 +01:00
|
|
|
export LC_ALL=
|
|
|
|
export LC_TIME=
|
2009-06-17 11:07:31 +01:00
|
|
|
|
|
|
|
|
2016-08-16 01:15:27 +01:00
|
|
|
# Builds will use users that are members of this group
|
2016-08-16 04:02:20 +01:00
|
|
|
extraBuildFlags+=(--option "build-users-group" "$buildUsersGroup")
|
2016-08-16 01:15:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Inherit binary caches from the host
|
|
|
|
binary_caches="$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"binary-caches"};')"
|
|
|
|
extraBuildFlags+=(--option "binary-caches" "$binary_caches")
|
|
|
|
|
2012-04-10 15:39:12 +01:00
|
|
|
|
2014-05-08 22:40:17 +01:00
|
|
|
# Copy Nix to the Nix store on the target device, unless it's already there.
|
|
|
|
if ! NIX_DB_DIR=$mountPoint/nix/var/nix/db nix-store --check-validity @nix@ 2> /dev/null; then
|
|
|
|
echo "copying Nix to $mountPoint...."
|
|
|
|
for i in $(@perl@/bin/perl @pathsFromGraph@ @nixClosure@); do
|
|
|
|
echo " $i"
|
|
|
|
chattr -R -i $mountPoint/$i 2> /dev/null || true # clear immutable bit
|
2016-08-07 01:25:06 +01:00
|
|
|
@rsync@/bin/rsync -a $i $mountPoint/nix/store/
|
2014-05-08 22:40:17 +01: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.)
|
|
|
|
chroot $mountPoint @nix@/bin/nix-store --register-validity < @nixClosure@
|
|
|
|
fi
|
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
|
|
|
|
|
|
|
|
2015-09-30 20:43:06 +01:00
|
|
|
# Build hooks likely won't function correctly in the minimal chroot; just disable them.
|
|
|
|
unset NIX_BUILD_HOOK
|
|
|
|
|
2008-09-08 13:40:05 +01:00
|
|
|
# Make the build below copy paths from the CD if possible. Note that
|
2014-05-08 22:20:36 +01:00
|
|
|
# /tmp/root in the chroot is the root of the CD.
|
|
|
|
export NIX_OTHER_STORES=/tmp/root/nix:$NIX_OTHER_STORES
|
2007-01-23 17:17:10 +00:00
|
|
|
|
2013-09-16 12:28:59 +01: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
|
|
|
|
2016-07-24 14:45:24 +01:00
|
|
|
if [ -z "$closure" ]; then
|
|
|
|
# Get the absolute path to the NixOS/Nixpkgs sources.
|
|
|
|
nixpkgs="$(readlink -f $(nix-instantiate --find-file nixpkgs))"
|
2012-04-23 01:41:37 +01:00
|
|
|
|
2016-07-24 14:45:24 +01:00
|
|
|
nixEnvAction="-f <nixpkgs/nixos> --set -A system"
|
|
|
|
else
|
|
|
|
nixpkgs=""
|
|
|
|
nixEnvAction="--set $closure"
|
|
|
|
fi
|
2012-04-23 01:41:37 +01: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..."
|
2014-05-08 22:20:36 +01:00
|
|
|
NIX_PATH="nixpkgs=/tmp/root/$nixpkgs:nixos-config=$NIXOS_CONFIG" NIXOS_CONFIG= \
|
2012-03-02 12:38:22 +00:00
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
2016-07-24 14:45:24 +01:00
|
|
|
"${extraBuildFlags[@]}" -p /nix/var/nix/profiles/system $nixEnvAction
|
2006-12-16 23:50:10 +00:00
|
|
|
|
|
|
|
|
2012-04-23 01:41:37 +01:00
|
|
|
# Copy the NixOS/Nixpkgs sources to the target as the initial contents
|
|
|
|
# of the NixOS channel.
|
2012-04-26 14:16:58 +01: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 15:38:57 +01: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 "")
|
2016-07-24 14:45:24 +01:00
|
|
|
if [ -z "$noChannelCopy" ] && [ -n "$srcs" ]; then
|
2013-02-04 12:38:54 +00:00
|
|
|
echo "copying NixOS/Nixpkgs sources..."
|
|
|
|
chroot $mountPoint @nix@/bin/nix-env \
|
2013-09-29 15:38:57 +01: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 01:41:37 +01:00
|
|
|
mkdir -m 0700 -p $mountPoint/root/.nix-defexpr
|
2012-05-25 00:37:06 +01:00
|
|
|
ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels
|
2006-11-12 23:30:03 +00:00
|
|
|
|
|
|
|
|
2014-05-08 23:49:18 +01:00
|
|
|
# Get rid of the /etc bind mounts.
|
2016-08-16 04:02:20 +01:00
|
|
|
for f in /etc/passwd /etc/group; do [ -f "$f" ] && umount $mountPoint/$f; done
|
2012-05-15 14:50:36 +01:00
|
|
|
|
|
|
|
|
2008-05-09 11:08:02 +01: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 11:08:02 +01: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..."
|
2016-08-16 01:15:27 +01:00
|
|
|
if [ -z "$noBootLoader" ]; then
|
2016-08-16 12:51:58 +01:00
|
|
|
NIXOS_INSTALL_BOOTLOADER=1 chroot $mountPoint \
|
2016-08-16 01:15:27 +01:00
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
|
|
|
fi
|
2014-05-08 23:04:48 +01:00
|
|
|
|
|
|
|
# Run the activation script.
|
|
|
|
chroot $mountPoint /nix/var/nix/profiles/system/activate
|
|
|
|
|
|
|
|
|
|
|
|
# Ask the user to set a root password.
|
2017-01-29 10:11:01 +00:00
|
|
|
if [ -z "$noRootPasswd" ] && chroot $mountPoint [ -x /run/wrappers/bin/passwd ] && [ -t 0 ]; then
|
2014-05-08 23:04:48 +01:00
|
|
|
echo "setting root password..."
|
2017-01-29 10:11:01 +00:00
|
|
|
chroot $mountPoint /run/wrappers/bin/passwd
|
2014-05-08 23:04:48 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
echo "installation finished!"
|