2022-05-09 13:20:43 +02:00
##
# NixOS Installation
##
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
## Entry point to the installation, see »./README.md«.
2022-11-30 13:41:21 +01:00
function install-system { ( set -u # 1: blockDev
2022-08-31 08:38:33 +02:00
trap - EXIT # start with empty traps for sub-shell
2022-11-30 13:41:21 +01:00
prepare-installer " $@ " || exit
do -disk-setup " ${ argv [0] } " || exit
install-system-to $mnt || exit
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
) }
2022-07-29 12:49:55 +02:00
## Does very simple argument paring and validation, performs some sanity checks, includes a hack to make installation work when nix isn't installed for root, and enables debugging (if requested).
2022-05-09 13:20:43 +02:00
function prepare-installer { # ...
2022-11-30 13:41:21 +01:00
generic-arg-parse " $@ " || return
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2022-08-31 08:38:33 +02:00
if [ [ ${ args [debug] :- } ] ] ; then set -x ; fi
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
: ${ argv [0] : ? "Required: Target disk or image paths." }
2022-11-30 13:41:21 +01:00
if [ [ " $( id -u) " != '0' ] ] ; then echo 'Script must be run as root.' 1>& 2 ; return 1 ; fi
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
umask 0022 # Ensure consistent umask (default permissions for new files).
2022-05-09 13:20:43 +02:00
2022-11-30 13:41:21 +01:00
if [ [ -e "/run/keystore-@{config.networking.hostName!hashString.sha256:0:8}" ] ] ; then echo "Keystore »/run/keystore-@{config.networking.hostName!hashString.sha256:0:8}/« is already open. Close it and remove the mountpoint before running the installer." 1>& 2 ; return 1 ; fi
2022-05-09 13:20:43 +02:00
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
# (partitions are checked in »partition-disks« once the target devices are known)
local luksName ; for luksName in "@{!config.boot.initrd.luks.devices!catAttrSets.device[@]}" ; do
2022-11-30 13:41:21 +01:00
if [ [ -e " /dev/mapper/ $luksName " ] ] ; then echo " LUKS device mapping » $luksName « is already open. Close it before running the installer. " 1>& 2 ; return 1 ; fi
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
local poolName ; for poolName in "@{!config.wip.fs.zfs.pools[@]}" ; do
2022-11-30 13:41:21 +01:00
if @{ native.zfs} /bin/zfs get -o value -H name " $poolName " & >/dev/null ; then echo " ZFS pool » $poolName « is already imported. Export the pool before running the installer. " 1>& 2 ; return 1 ; fi
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
done
2022-05-09 13:20:43 +02:00
2022-08-31 08:38:33 +02:00
if [ [ ${ SUDO_USER :- } ] ] ; then # use Nix as the user who called this script, as Nix may not be set up for root
function nix { ( set +x ; declare -a args = ( " $@ " ) ; PATH = $hostPath su - " $SUDO_USER " -c " $( declare -p args) " ' ; nix "${args[@]}"' ) }
else # use Nix by absolute path, as it won't be on »$PATH«
PATH = $PATH :@{ native.nix} /bin
fi
_set_x = 'set -x' ; if [ [ ${ args [quiet] :- } ] ] ; then _set_x = : ; fi
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2022-08-31 08:38:33 +02:00
if [ [ ${ args [debug] :- } ] ] ; then set +e ; set -E ; trap 'code= ; timeout .2s cat &>/dev/null || true ; @{native.bashInteractive}/bin/bash --init-file @{config.environment.etc.bashrc.source} || code=$? ; if [[ $code ]] ; then exit $code ; fi' ERR ; fi # On error, instead of exiting straight away, open a shell to allow diagnosing/fixing the issue. Only exit if that shell reports failure (e.g. CtrlC + CtrlD). Unfortunately, the exiting has to be repeated for each level of each nested sub-shells. The »timeout cat« eats anything lined up on stdin, which would otherwise be sent to bash and interpreted as commands.
2022-05-09 13:20:43 +02:00
2022-07-29 12:49:55 +02:00
export PATH = $PATH :@{ native.util-linux} /bin # Doing a system installation requires a lot of stuff from »util-linux«. This should probably be moved into the individual functions that actually use the tools ...
2022-05-09 13:20:43 +02:00
2022-07-29 12:49:55 +02:00
}
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
## The default command that will activate the system and install the bootloader. In a separate function to make it easy to replace.
function nixos-install-cmd { ( set -eu # 1: mnt, 2: topLevel
# »nixos-install« by default does some stateful things (see the »--no« options below), builds and copies the system config (but that's already done), and then calls »NIXOS_INSTALL_BOOTLOADER=1 nixos-enter -- $topLevel/bin/switch-to-configuration boot«, which is essentially the same as »NIXOS_INSTALL_BOOTLOADER=1 nixos-enter -- @{config.system.build.installBootLoader} $targetSystem«, i.e. the side effects of »nixos-enter« and then calling the bootloader-installer.
2022-11-30 13:41:21 +01:00
PATH = @{ config.systemd.package} /bin:@{ native.nix} /bin:$PATH TMPDIR = /tmp LC_ALL = C @{ native.nixos-install-tools} /bin/nixos-install --system " $2 " --no-root-passwd --no-channel-copy --root " $1 " || exit #--debug
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
) }
2022-05-09 13:20:43 +02:00
## Copies the system's dependencies to the disks mounted at »$mnt« and installs the bootloader. If »$inspect« is set, a root shell will be opened in »$mnt« afterwards.
# »$topLevel« may point to an alternative top-level dependency to install.
2022-11-30 13:41:21 +01:00
function install-system-to { ( set -u # 1: mnt
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
mnt = $1 ; topLevel = ${ 2 :- }
2022-08-31 08:38:33 +02:00
targetSystem = ${ args [toplevel] :- @{config.system.build.toplevel } }
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
trap - EXIT # start with empty traps for sub-shell
# Link/create files that some tooling expects:
2022-11-30 13:41:21 +01:00
mkdir -p -m 755 $mnt /nix/var/nix || exit ; mkdir -p -m 1775 $mnt /nix/store || exit
mkdir -p $mnt /etc $mnt /run || exit ; mkdir -p -m 1777 $mnt /tmp || exit
mount tmpfs -t tmpfs $mnt /run || exit ; prepend_trap " umount -l $mnt /run " EXIT || exit # If there isn't anything mounted here, »activate« will mount a tmpfs (inside »nixos-enter«'s private mount namespace). That would hide the additions below.
[ [ -e $mnt /etc/NIXOS ] ] || touch $mnt /etc/NIXOS || exit # for »switch-to-configuration«
[ [ -e $mnt /etc/mtab ] ] || ln -sfn /proc/mounts $mnt /etc/mtab || exit
ln -sT $( realpath $targetSystem ) $mnt /run/current-system || exit
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
#mkdir -p /nix/var/nix/db # »nixos-containers« requires this but nothing creates it before nix is used. BUT »nixos-enter« screams: »/nix/var/nix/db exists and is not a regular file.«
2022-07-29 12:49:55 +02:00
# If the system configuration is supposed to be somewhere on the system, might as well initialize that:
if [ [ @{ config.environment.etc.nixos.source:-} && @{ config.environment.etc.nixos.source} != /nix/store/* && @{ config.environment.etc.nixos.source} != /run/current-system/config && ! -e $mnt /@{ config.environment.etc.nixos.source} && -e $targetSystem /config ] ] ; then
2022-11-30 13:41:21 +01:00
mkdir -p -- $mnt /@{ config.environment.etc.nixos.source} || exit
cp -at $mnt /@{ config.environment.etc.nixos.source} -- $targetSystem /config/* || exit
chown -R 0:0 $mnt /@{ config.environment.etc.nixos.source} || exit
chmod -R u+w $mnt /@{ config.environment.etc.nixos.source} || exit
2022-07-29 12:49:55 +02:00
fi
2022-12-27 15:37:27 +01:00
# Set this as the initial system generation (just in case »nixos-install-cmd« won't):
2022-11-30 13:41:21 +01:00
mkdir -p -m 755 $mnt /nix/var/nix/profiles || exit
2022-12-27 15:37:27 +01:00
[ [ -e $mnt /nix/var/nix/profiles/system-1-link ] ] || ln -sT $( realpath $targetSystem ) $mnt /nix/var/nix/profiles/system-1-link || exit
[ [ -e $mnt /nix/var/nix/profiles/system ] ] || ln -sT system-1-link $mnt /nix/var/nix/profiles/system || exit
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
# Support cross architecture installation (not sure if this is actually required)
2022-06-28 05:00:48 +02:00
if [ [ $( cat /run/current-system/system 2>/dev/null || echo "x86_64-linux" ) != "@{config.wip.preface.hardware}" -linux ] ] ; then
2022-11-30 13:41:21 +01:00
mkdir -p $mnt /run/binfmt || exit ; [ [ ! -e /run/binfmt/"@{config.wip.preface.hardware}" -linux ] ] || cp -a { ,$mnt } /run/binfmt/"@{config.wip.preface.hardware}" -linux || exit
2022-06-28 05:00:48 +02:00
# Ubuntu (by default) expects the "interpreter" at »/usr/bin/qemu-@{config.wip.preface.hardware}-static«.
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
fi
2022-07-29 12:49:55 +02:00
# Copy system closure to new nix store:
2022-11-30 13:41:21 +01:00
if [ [ ${ SUDO_USER :- } ] ] ; then chown -R $SUDO_USER : $mnt /nix/store $mnt /nix/var || exit ; fi
( cmd = ( nix --extra-experimental-features nix-command --offline copy --no-check-sigs --to $mnt ${ topLevel :- $targetSystem } ) ; if [ [ ${ args [quiet] :- } ] ] ; then " ${ cmd [@] } " --quiet & >/dev/null || exit ; else set -x ; time " ${ cmd [@] } " || exit ; fi ) || exit ; rm -rf $mnt /nix/var/nix/gcroots || exit
2022-12-27 15:37:27 +01:00
# TODO: if the target has @{config.nix.settings.auto-optimise-store} and the host doesn't (there is no .links dir?), optimize now
2022-11-30 13:41:21 +01:00
if [ [ ${ SUDO_USER :- } ] ] ; then chown -R root:root $mnt /nix $mnt /nix/var || exit ; chown :30000 $mnt /nix/store || exit ; fi
2022-07-29 12:49:55 +02:00
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
# Run the main install command (primarily for the bootloader):
2022-11-30 13:41:21 +01:00
mount -o bind,ro /nix/store $mnt /nix/store || exit ; prepend_trap '! mountpoint -q $mnt/nix/store || umount -l $mnt/nix/store' EXIT || exit # all the things required to _run_ the system are copied, but (may) need some more things to initially install it and/or enter the chroot (like qemu, see above)
run-hook-script 'Pre Installation' @{ config.wip.fs.disks.preInstallCommands!writeText.preInstallCommands} || exit
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
code = 0 ; nixos-install-cmd $mnt " ${ topLevel :- $targetSystem } " || code = $?
2022-11-30 13:41:21 +01:00
run-hook-script 'Post Installation' @{ config.wip.fs.disks.postInstallCommands!writeText.postInstallCommands} || exit
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
# Done!
2022-11-30 13:41:21 +01:00
if [ [ ${ args [no-inspect] :- } ] ] ; then
if ( ( code != 0 ) ) ; then exit $code ; fi
elif [ [ ${ args [inspect-cmd] :- } ] ] ; then
if ( ( code != 0 ) ) ; then exit $code ; fi
eval " ${ args [inspect-cmd] } " || exit
else
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
if ( ( code != 0 ) ) ; then
2022-11-30 13:41:21 +01:00
( set +x ; echo "Something went wrong in the last step of the installation. Inspect the output above and the mounted system in this chroot shell to decide whether it is critical. Exit the shell with 0 to proceed, or non-zero to abort." 1>& 2 )
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
else
2022-11-30 13:41:21 +01:00
( set +x ; echo "Installation done! This shell is in a chroot in the mounted system for inspection. Exiting the shell will unmount the system." 1>& 2 )
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
fi
2022-11-30 13:41:21 +01:00
PATH = @{ config.systemd.package} /bin:$PATH @{ native.nixos-install-tools} /bin/nixos-enter --root $mnt || exit # TODO: construct path as it would be at login
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
#( cd $mnt ; mnt=$mnt @{native.bashInteractive}/bin/bash --init-file @{config.environment.etc.bashrc.source} )
fi
2022-11-30 13:41:21 +01:00
mkdir -p $mnt /var/lib/systemd/timesync && touch $mnt /var/lib/systemd/timesync/clock || true # save current time
2022-05-09 13:20:43 +02:00
) }