dotfiles/install_os.sh

122 lines
3.4 KiB
Bash
Raw Normal View History

2023-11-27 23:34:30 +00:00
#!/usr/bin/env nix-shell
#! nix-shell -i bash
2024-02-17 22:35:53 +00:00
#! nix-shell -p nix
2024-01-06 18:10:47 +00:00
2023-11-27 23:34:30 +00:00
set -euo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2023-11-27 23:34:30 +00:00
# Parse arguments
function help {
2024-02-17 22:35:53 +00:00
echo "Usage: $0 [-h|-e] [flake-uri#]name"
echo "Install a NixOS configuration from another installation on the same machine."
echo
echo "Arguments:"
2024-02-17 22:35:53 +00:00
echo " flake-uri: Path to flake containing the system configuration. Default: the one where the script is located."
echo " name: Hostname of the configuration to install. The flake must contain an output named 'nixosConfigurations.*name*'"
2023-11-27 23:34:30 +00:00
echo
echo "Options:"
echo " -h: Display this help message."
2023-12-02 17:50:31 +00:00
echo " -e: Erase the disk. For cases where the partition scheme doesn't match the existing one."
2023-11-27 23:34:30 +00:00
}
disko_mode=mount
while getopts "he" OPTION
2023-11-27 23:34:30 +00:00
do
case "$OPTION" in
h)
help
exit 0
;;
e)
disko_mode=disko
;;
?)
help
exit 2
;;
esac
done
2024-02-17 22:35:53 +00:00
shift "$((OPTIND -1))"
2023-11-27 23:34:30 +00:00
if [ "$#" -ne 1 ]
2023-11-27 23:34:30 +00:00
then
help
exit 2
fi
2024-02-17 22:35:53 +00:00
if [[ "$1" == *"#"* ]]
then
2024-02-17 22:35:53 +00:00
flake_uri="$(echo "$1" | cut -d'#' -f1)"
flake_uri=$( cd -- "$flake_uri" &> /dev/null && pwd )
name="$(echo "$1" | cut -d'#' -f2)"
else
flake_uri="$SCRIPT_DIR"
name="$1"
fi
2024-02-17 22:35:53 +00:00
if [ ! -f "$flake_uri/flake.nix" ]
then
2024-02-17 22:35:53 +00:00
echo "Flake not found."
fi
2024-02-17 22:35:53 +00:00
flake="${flake_uri}#${name}"
2023-11-29 13:27:29 +00:00
mountpoint="/mnt/nixos"
2024-02-17 22:35:53 +00:00
luks_pass_path="luks/$(basename "${name}")"
2023-11-27 23:34:30 +00:00
set -x
2023-11-29 13:27:29 +00:00
sudo mkdir -p "$mountpoint"
2023-12-08 23:09:36 +00:00
# Load encryption password
2024-02-17 22:35:53 +00:00
luks_pass_file="/tmp/dotfiles_${name}_password";
trap 'rm "$luks_pass_file"' ERR
touch "$luks_pass_file"
chmod "u=rw" "$luks_pass_file"
pass "$luks_pass_path" | head -n1 | tr -d '\n' > "$luks_pass_file"
2023-12-08 23:09:36 +00:00
2023-11-27 23:34:30 +00:00
# Format or mount disk
2024-02-17 22:35:53 +00:00
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#disko" -- --mode "$disko_mode" --flake "$flake"
# --root-mountpoint is ignored with flakes, so this is set in os/common.nix
2023-12-08 23:09:36 +00:00
# Unload encryption password
2024-02-17 22:35:53 +00:00
trap - ERR
2023-12-08 23:09:36 +00:00
rm "$luks_pass_file"
2023-11-27 23:34:30 +00:00
# Generate hardware-config.nix
2024-02-17 22:35:53 +00:00
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-generate-config" -- --no-filesystems --root "$mountpoint"
2023-11-27 23:34:30 +00:00
# --no-filesystems because they are imported via disko
2024-02-17 22:35:53 +00:00
sudo rm "$mountpoint/etc/nixos/configuration.nix"
2023-11-27 23:34:30 +00:00
# Plug system configuration into this git repo
2023-11-29 13:27:29 +00:00
sudo mkdir -p "${mountpoint}/etc/nixos"
2024-02-17 22:35:53 +00:00
echo "{
description = \"$name system config\";
inputs.entrypoint.url = \"git+file:$flake_uri\";
outputs = { self, entrypoint, ... }:
{
nixosConfigurations.$name = entrypoint.nixosConfigurations.$name.extendModules {
modules = [ ./hardware-configuration.nix ];
};
};
2024-02-17 23:09:08 +00:00
}" | sudo tee "${mountpoint}/etc/nixos/flake.nix" > /dev/null
2023-11-27 23:34:30 +00:00
# Everything there should be covered by (and conflicts with) the repo anyways.
# Install NixOS! Or create a new generation.
2024-02-17 23:09:08 +00:00
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-install" -- --no-root-password --root "$mountpoint" --flake "${mountpoint}/etc/nixos#${name}"
2023-11-27 23:34:30 +00:00
2023-12-09 22:51:04 +00:00
set +x
# Signal the installation is done!
2023-11-27 23:34:30 +00:00
echo 
2023-12-09 22:51:04 +00:00
echo "Manual post-installation instructions:"
echo "- Boot into the system"
echo "- Transfer necessary private keys (or use ssh -A for testing)"
2024-02-17 22:35:53 +00:00
echo "- Run git-sync"
echo "- Run install-passwords"
2024-02-18 12:38:01 +00:00
echo "- Run install-wifi"
2024-02-17 22:35:53 +00:00
echo "- Run rb"
2023-12-16 22:43:01 +00:00
echo "- Change root and user password"
2024-02-17 22:35:53 +00:00
# TODO Use update-local-flakes?