dotfiles/install_os.sh

116 lines
3.1 KiB
Bash
Raw Normal View History

2023-11-28 00:34:30 +01:00
#!/usr/bin/env nix-shell
#! nix-shell -i bash
#! nix-shell -p bash nixos-install-tools
set -euo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2023-11-28 00:34:30 +01:00
# Parse arguments
function help {
echo "Usage: $0 [-h|-e] profile"
2023-12-02 18:50:31 +01:00
echo "Install NixOS on a device."
echo
echo "Arguments:"
echo " profile: OS/disk profile to use"
2023-11-28 00:34:30 +01:00
echo
echo "Options:"
echo " -h: Display this help message."
2023-12-02 18:50:31 +01:00
echo " -e: Erase the disk. For cases where the partition scheme doesn't match the existing one."
2023-11-28 00:34:30 +01:00
}
disko_mode=mount
while getopts "he" OPTION
2023-11-28 00:34:30 +01:00
do
case "$OPTION" in
h)
help
exit 0
;;
e)
disko_mode=disko
;;
?)
help
exit 2
;;
esac
done
shift "$(($OPTIND -1))"
if [ "$#" -ne 1 ]
2023-11-28 00:34:30 +01:00
then
help
exit 2
fi
profile="$1"
profile_dir="${SCRIPT_DIR}/${profile}"
if [ ! -d "$profile_dir" ]
then
echo "Profile not found."
fi
2023-12-02 18:50:31 +01:00
disko_config="${profile_dir}/dk.nix"
if [ ! -f "$disko_config" ]
then
echo "Disk configuration not found."
fi
nixos_config="${profile_dir}/os.nix"
if [ ! -f "$nixos_config" ]
then
echo "NixOS configuration not found."
fi
2023-11-29 14:27:29 +01:00
mountpoint="/mnt/nixos"
nix_flakes_cmd="nix --extra-experimental-features nix-command --extra-experimental-features flakes"
2023-12-09 00:09:36 +01:00
luks_pass_path="luks/$(basename ${profile})"
2023-11-28 00:34:30 +01:00
set -x
2023-11-29 14:27:29 +01:00
sudo mkdir -p "$mountpoint"
2023-11-28 00:34:30 +01:00
# Add channels to root user, as nixos-install uses those.
# Not great, but fixable with flakes I guess
sudo ./add_channels.sh
2023-12-09 00:09:36 +01:00
# Load encryption password
luks_pass_file="$(mktemp --suffix="luks_password")"
pass $luks_pass_path | head -n1 | tr -d '\n' > $luks_pass_file
2023-11-28 00:34:30 +01:00
# Format or mount disk
2023-12-09 00:09:36 +01:00
sudo $nix_flakes_cmd run github:nix-community/disko -- --root-mountpoint "$mountpoint" --mode "$disko_mode" --argstr passwordFile "$luks_pass_file" "$disko_config"
# Unload encryption password
rm "$luks_pass_file"
2023-11-28 00:34:30 +01:00
# Generate hardware-config.nix
2023-11-29 14:27:29 +01:00
sudo nixos-generate-config --no-filesystems --root "$mountpoint"
2023-11-28 00:34:30 +01:00
# --no-filesystems because they are imported via disko
# Plug system configuration into this git repo
2023-11-29 14:27:29 +01:00
sudo mkdir -p "${mountpoint}/etc/nixos"
echo "{ ... }: { imports = [ ./hardware-configuration.nix ${nixos_config} ]; }" | sudo tee "${mountpoint}/etc/nixos/configuration.nix" > /dev/null
2023-11-28 00:34:30 +01:00
# Everything there should be covered by (and conflicts with) the repo anyways.
# Install NixOS! Or create a new generation.
2023-11-29 14:27:29 +01:00
sudo nixos-install --no-root-password --root "$mountpoint"
2023-11-28 00:34:30 +01:00
2023-11-29 14:27:29 +01:00
# Install dotfiles. Actually not needed by nixos-install since it doesn't rewrite global paths to the mountpoint.
2023-11-28 00:34:30 +01:00
# Without it no nixos-rebuild from the system itself once installed though.
# Should probably be replaced with something like git-sync
2023-11-29 14:27:29 +01:00
# sudo mkdir -p $mountpoint/home/geoffrey/.config/
# sudo cp -a ../dotfiles $mountpoint/home/geoffrey/.config/
# sudo chown geoffrey:geoffrey $mountpoint/home/geoffrey -R
2023-11-28 00:34:30 +01:00
2023-12-09 23:51:04 +01:00
set +x
# Signal the installation is done!
2023-11-28 00:34:30 +01:00
echo 
2023-12-09 23:51:04 +01:00
echo "Manual post-installation instructions:"
echo "- Boot into the system"
echo "- Transfer necessary private keys (or use ssh -A for testing)"
echo "- Run git-sync-init"
echo "- Check that the system can build itself"