dotfiles/install_os.sh

123 lines
3.4 KiB
Bash
Raw Normal View History

2023-11-28 00:34:30 +01:00
#!/usr/bin/env nix-shell
#! nix-shell -i bash
2024-02-17 23:35:53 +01:00
#! nix-shell -p nix
2024-01-06 19:10:47 +01:00
2023-11-28 00:34:30 +01:00
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 {
2024-02-17 23:35:53 +01: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 23:35:53 +01: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-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
2024-02-17 23:35:53 +01:00
shift "$((OPTIND -1))"
2023-11-28 00:34:30 +01:00
if [ "$#" -ne 1 ]
2023-11-28 00:34:30 +01:00
then
help
exit 2
fi
2024-02-17 23:35:53 +01:00
if [[ "$1" == *"#"* ]]
then
2024-02-17 23:35:53 +01: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 23:35:53 +01:00
if [ ! -f "$flake_uri/flake.nix" ]
then
2024-02-17 23:35:53 +01:00
echo "Flake not found."
fi
2024-02-17 23:35:53 +01:00
flake="${flake_uri}#${name}"
2023-11-29 14:27:29 +01:00
mountpoint="/mnt/nixos"
2024-02-17 23:35:53 +01:00
mountpoint_flake="${mountpoint}/etc/nixos/flake.nix"
luks_pass_path="luks/$(basename "${name}")"
2023-11-28 00:34:30 +01:00
set -x
2023-11-29 14:27:29 +01:00
sudo mkdir -p "$mountpoint"
2023-12-09 00:09:36 +01:00
# Load encryption password
2024-02-17 23:35:53 +01: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-09 00:09:36 +01:00
2023-11-28 00:34:30 +01:00
# Format or mount disk
2024-02-17 23:35:53 +01: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-09 00:09:36 +01:00
# Unload encryption password
2024-02-17 23:35:53 +01:00
trap - ERR
2023-12-09 00:09:36 +01:00
rm "$luks_pass_file"
2023-11-28 00:34:30 +01:00
# Generate hardware-config.nix
2024-02-17 23:35:53 +01:00
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-generate-config" -- --no-filesystems --root "$mountpoint"
2023-11-28 00:34:30 +01:00
# --no-filesystems because they are imported via disko
2024-02-17 23:35:53 +01:00
sudo rm "$mountpoint/etc/nixos/configuration.nix"
2023-11-28 00:34:30 +01:00
# Plug system configuration into this git repo
2023-11-29 14:27:29 +01:00
sudo mkdir -p "${mountpoint}/etc/nixos"
2024-02-17 23:35:53 +01: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 ];
};
};
}" | sudo tee "$mountpoint_flake" > /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.
2024-02-17 23:35:53 +01:00
sudo nix --extra-experimental-features "nix-command flakes" run "${SCRIPT_DIR}#nixos-install" -- --no-root-password --root "$mountpoint" --flake "${mountpoint_flake}#${name}"
# TODO Add this? --flake "$flake"
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)"
2024-02-17 23:35:53 +01:00
echo "- Run git-sync"
echo "- Run install-passwords"
echo "- Run rb"
2023-12-16 23:43:01 +01:00
echo "- Change root and user password"
2024-02-17 23:35:53 +01:00
# TODO Use update-local-flakes?