1#! @shell@ 2 3# - make Nix store etc. 4# - copy closure of Nix to target device 5# - register validity 6# - with a chroot to the target device: 7# * nix-env -p /nix/var/nix/profiles/system -i <nix-expr for the configuration> 8# * install the boot loader 9 10# Ensure a consistent umask. 11umask 0022 12 13# Re-exec ourselves in a private mount namespace so that our bind 14# mounts get cleaned up automatically. 15if [ "$(id -u)" = 0 ]; then 16 if [ -z "$NIXOS_INSTALL_REEXEC" ]; then 17 export NIXOS_INSTALL_REEXEC=1 18 exec unshare --mount --uts -- "$0" "$@" 19 else 20 mount --make-rprivate / 21 fi 22fi 23 24# Parse the command line for the -I flag 25extraBuildFlags=() 26chrootCommand=(/run/current-system/sw/bin/bash) 27 28while [ "$#" -gt 0 ]; do 29 i="$1"; shift 1 30 case "$i" in 31 --max-jobs|-j|--cores|-I) 32 j="$1"; shift 1 33 extraBuildFlags+=("$i" "$j") 34 ;; 35 --option) 36 j="$1"; shift 1 37 k="$1"; shift 1 38 extraBuildFlags+=("$i" "$j" "$k") 39 ;; 40 --root) 41 mountPoint="$1"; shift 1 42 ;; 43 --show-trace) 44 extraBuildFlags+=("$i") 45 ;; 46 --chroot) 47 runChroot=1 48 if [[ "$@" != "" ]]; then 49 chrootCommand=("$@") 50 fi 51 break 52 ;; 53 --help) 54 exec man nixos-install 55 exit 1 56 ;; 57 *) 58 echo "$0: unknown option \`$i'" 59 exit 1 60 ;; 61 esac 62done 63 64set -e 65shopt -s nullglob 66 67if test -z "$mountPoint"; then 68 mountPoint=/mnt 69fi 70 71if ! test -e "$mountPoint"; then 72 echo "mount point $mountPoint doesn't exist" 73 exit 1 74fi 75 76 77# Mount some stuff in the target root directory. 78mkdir -m 0755 -p $mountPoint/dev $mountPoint/proc $mountPoint/sys $mountPoint/etc $mountPoint/run $mountPoint/home 79mkdir -m 01777 -p $mountPoint/tmp 80mkdir -m 0755 -p $mountPoint/tmp/root 81mkdir -m 0755 -p $mountPoint/var/setuid-wrappers 82mkdir -m 0700 -p $mountPoint/root 83mount --rbind /dev $mountPoint/dev 84mount --rbind /proc $mountPoint/proc 85mount --rbind /sys $mountPoint/sys 86mount --rbind / $mountPoint/tmp/root 87mount -t tmpfs -o "mode=0755" none $mountPoint/run 88mount -t tmpfs -o "mode=0755" none $mountPoint/var/setuid-wrappers 89rm -rf $mountPoint/var/run 90ln -s /run $mountPoint/var/run 91rm -f $mountPoint/etc/{resolv.conf,hosts} 92cp -Lf /etc/resolv.conf /etc/hosts $mountPoint/etc/ 93 94if [ -e "$SSL_CERT_FILE" ]; then 95 cp -Lf "$SSL_CERT_FILE" "$mountPoint/tmp/ca-cert.crt" 96 export SSL_CERT_FILE=/tmp/ca-cert.crt 97 # For Nix 1.7 98 export CURL_CA_BUNDLE=/tmp/ca-cert.crt 99fi 100 101if [ -n "$runChroot" ]; then 102 if ! [ -L $mountPoint/nix/var/nix/profiles/system ]; then 103 echo "$0: installation not finished; cannot chroot into installation directory" 104 exit 1 105 fi 106 ln -s /nix/var/nix/profiles/system $mountPoint/run/current-system 107 exec chroot $mountPoint "${chrootCommand[@]}" 108fi 109 110 111# Get the path of the NixOS configuration file. 112if test -z "$NIXOS_CONFIG"; then 113 NIXOS_CONFIG=/etc/nixos/configuration.nix 114fi 115 116if ! test -e "$mountPoint/$NIXOS_CONFIG"; then 117 echo "configuration file $mountPoint/$NIXOS_CONFIG doesn't exist" 118 exit 1 119fi 120 121 122# Create the necessary Nix directories on the target device, if they 123# don't already exist. 124mkdir -m 0755 -p \ 125 $mountPoint/nix/var/nix/gcroots \ 126 $mountPoint/nix/var/nix/temproots \ 127 $mountPoint/nix/var/nix/manifests \ 128 $mountPoint/nix/var/nix/userpool \ 129 $mountPoint/nix/var/nix/profiles \ 130 $mountPoint/nix/var/nix/db \ 131 $mountPoint/nix/var/log/nix/drvs 132 133mkdir -m 1775 -p $mountPoint/nix/store 134chown root:nixbld $mountPoint/nix/store 135 136 137# There is no daemon in the chroot. 138unset NIX_REMOTE 139 140 141# We don't have locale-archive in the chroot, so clear $LANG. 142export LANG= 143export LC_ALL= 144export LC_TIME= 145 146 147# Create a temporary Nix config file that causes the nixbld users to 148# be used. 149echo "build-users-group = nixbld" > $mountPoint/tmp/nix.conf # FIXME: remove in Nix 1.8 150binary_caches=$(@perl@/bin/perl -I @nix@/lib/perl5/site_perl/*/* -e 'use Nix::Config; Nix::Config::readConfig; print $Nix::Config::config{"binary-caches"};') 151if test -n "$binary_caches"; then 152 echo "binary-caches = $binary_caches" >> $mountPoint/tmp/nix.conf 153fi 154export NIX_CONF_DIR=/tmp 155 156touch $mountPoint/etc/passwd $mountPoint/etc/group 157mount --bind -o ro /etc/passwd $mountPoint/etc/passwd 158mount --bind -o ro /etc/group $mountPoint/etc/group 159 160 161# Copy Nix to the Nix store on the target device, unless it's already there. 162if ! NIX_DB_DIR=$mountPoint/nix/var/nix/db nix-store --check-validity @nix@ 2> /dev/null; then 163 echo "copying Nix to $mountPoint...." 164 for i in $(@perl@/bin/perl @pathsFromGraph@ @nixClosure@); do 165 echo " $i" 166 chattr -R -i $mountPoint/$i 2> /dev/null || true # clear immutable bit 167 rsync -a $i $mountPoint/nix/store/ 168 done 169 170 # Register the paths in the Nix closure as valid. This is necessary 171 # to prevent them from being deleted the first time we install 172 # something. (I.e., Nix will see that, e.g., the glibc path is not 173 # valid, delete it to get it out of the way, but as a result nothing 174 # will work anymore.) 175 chroot $mountPoint @nix@/bin/nix-store --register-validity < @nixClosure@ 176fi 177 178 179# Create the required /bin/sh symlink; otherwise lots of things 180# (notably the system() function) won't work. 181mkdir -m 0755 -p $mountPoint/bin 182# !!! assuming that @shell@ is in the closure 183ln -sf @shell@ $mountPoint/bin/sh 184 185 186# Build hooks likely won't function correctly in the minimal chroot; just disable them. 187unset NIX_BUILD_HOOK 188 189# Make the build below copy paths from the CD if possible. Note that 190# /tmp/root in the chroot is the root of the CD. 191export NIX_OTHER_STORES=/tmp/root/nix:$NIX_OTHER_STORES 192 193p=@nix@/libexec/nix/substituters 194export NIX_SUBSTITUTERS=$p/copy-from-other-stores.pl:$p/download-from-binary-cache.pl 195 196 197# Make manifests available in the chroot. 198rm -f $mountPoint/nix/var/nix/manifests/* 199for i in /nix/var/nix/manifests/*.nixmanifest; do 200 chroot $mountPoint @nix@/bin/nix-store -r "$(readlink -f "$i")" > /dev/null 201 cp -pd "$i" $mountPoint/nix/var/nix/manifests/ 202done 203 204 205# Get the absolute path to the NixOS/Nixpkgs sources. 206nixpkgs="$(readlink -f $(nix-instantiate --find-file nixpkgs))" 207 208 209# Build the specified Nix expression in the target store and install 210# it into the system configuration profile. 211echo "building the system configuration..." 212NIX_PATH="nixpkgs=/tmp/root/$nixpkgs:nixos-config=$NIXOS_CONFIG" NIXOS_CONFIG= \ 213 chroot $mountPoint @nix@/bin/nix-env \ 214 "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/system -f '<nixpkgs/nixos>' --set -A system 215 216 217# Copy the NixOS/Nixpkgs sources to the target as the initial contents 218# of the NixOS channel. 219mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles 220mkdir -m 1777 -p $mountPoint/nix/var/nix/profiles/per-user 221mkdir -m 0755 -p $mountPoint/nix/var/nix/profiles/per-user/root 222srcs=$(nix-env "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -q nixos --no-name --out-path 2>/dev/null || echo -n "") 223if test -n "$srcs"; then 224 echo "copying NixOS/Nixpkgs sources..." 225 chroot $mountPoint @nix@/bin/nix-env \ 226 "${extraBuildFlags[@]}" -p /nix/var/nix/profiles/per-user/root/channels -i "$srcs" --quiet 227fi 228mkdir -m 0700 -p $mountPoint/root/.nix-defexpr 229ln -sfn /nix/var/nix/profiles/per-user/root/channels $mountPoint/root/.nix-defexpr/channels 230 231 232# Get rid of the /etc bind mounts. 233umount $mountPoint/etc/passwd $mountPoint/etc/group 234 235 236# Grub needs an mtab. 237ln -sfn /proc/mounts $mountPoint/etc/mtab 238 239 240# Mark the target as a NixOS installation, otherwise 241# switch-to-configuration will chicken out. 242touch $mountPoint/etc/NIXOS 243 244 245# Switch to the new system configuration. This will install Grub with 246# a menu default pointing at the kernel/initrd/etc of the new 247# configuration. 248echo "finalising the installation..." 249NIXOS_INSTALL_GRUB=1 chroot $mountPoint \ 250 /nix/var/nix/profiles/system/bin/switch-to-configuration boot 251 252 253# Run the activation script. 254chroot $mountPoint /nix/var/nix/profiles/system/activate 255 256 257# Ask the user to set a root password. 258if [ "$(chroot $mountPoint /run/current-system/sw/bin/sh -l -c "nix-instantiate --eval '<nixpkgs/nixos>' -A config.users.mutableUsers")" = true ] && [ -t 0 ] ; then 259 echo "setting root password..." 260 chroot $mountPoint /var/setuid-wrappers/passwd 261fi 262 263 264echo "installation finished!"