install.nix (3734B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | { lib, pkgs, files, ... }: with files.path; let inherit (lib) licenses recursiveUpdate; in recursiveUpdate { meta = { mainProgram = "os-install"; description = "System Install Script"; homepage = repo; license = licenses.gpl3Only; maintainers = ["maydayv7"]; }; } ( pkgs.writeShellApplication { name = "os-install"; runtimeInputs = with pkgs; [ coreutils disko.disko git gptfdisk netcat nixFlakes nixos-install-tools util-linux zfs ]; text = '' set -euo pipefail ${files.scripts.commands} TRIES=5 cleanup() { umount -R /mnt 2> /dev/null || true zpool export fspool 2> /dev/null || true } install_system() { local attempt=0 while [ "$attempt" -lt "$TRIES" ] do attempt=$((attempt + 1)) if nixos-install --no-root-passwd --root /mnt --flake "$URL#$HOST" then return 0 fi newline info "Couldn't finish installation (attempt $attempt/$TRIES)" done return 1 } internet if [ "$EUID" -ne 0 ] then error "This Command must be Executed as 'root'" fi # Target Device and Repository read -rp "Enter Name of Device to Install: " HOST if [ -z "$HOST" ] then error "Device name cannot be empty" fi read -rp "Enter Path to Repository (path/URL): " URL if [ -z "$URL" ] then URL=${flake} fi # Target Disk echo "Resolving target disk for '$HOST'..." if ! DISK=$(nix eval --raw "$URL#nixosConfigurations.$HOST.config.system.fs.disk" 2> /dev/null) then error "Couldn't resolve 'system.fs.disk' for '$HOST'" fi if [ ! -b "$DISK" ] then error "'$DISK' is not a valid block device" "Set 'system.fs.disk' for '$HOST' to this device's disk" fi # Confirm Destructive Wipe warn "ALL DATA on '$DISK' will be PERMANENTLY ERASED for Automatic Partitioning" read -rp "Type the Disk path to confirm: " CONFIRM if [ "$CONFIRM" != "$DISK" ] then error "Confirmation did not match '$DISK', aborting" fi # Wipe Stale Signatures echo "Wiping existing signatures on '$DISK'..." swapoff --all 2> /dev/null || true zpool labelclear -f "$DISK" 2> /dev/null || true for part in "$DISK"* do if [ -b "$part" ] then zpool labelclear -f "$part" 2> /dev/null || true fi done wipefs --all --force "$DISK" 2> /dev/null || true sgdisk --zap-all "$DISK" 2> /dev/null || true newline # Partition, Format and Mount Target echo "Partitioning and formatting '$DISK'..." disko --mode destroy,format,mount --flake "$URL#$HOST" newline # Install echo "Installing System '$HOST' from '$URL' onto '$DISK'..." newline until install_system do read -rp "Installation failed after $TRIES attempts. Try again? (Y/*): " choice case $choice in [Yy]*) newline; info "Retrying installation...";; *) cleanup; error "Installation cancelled";; esac done newline cleanup info "Run 'os setup' after rebooting to finish the install" info "Select the (recovery) boot menu option and run the above script as the 'recovery' user" restart ''; } ) |