Created
September 21, 2026 22:26
-
-
Save alebon/d779b170ccad0ec4e99c76d559c9c8eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # VM installer - run INSIDE the target VM booted from a NixOS ISO. | |
| # | |
| # Usage: sudo ./vm-install.sh [disk] (default disk: /dev/vda) | |
| # | |
| # Partitions the disk (MBR, ext4 root + swap), mounts it and generates | |
| # the initial NixOS config. It does NOT run nixos-install - review | |
| # /mnt/etc/nixos/configuration.nix first, then run nixos-install. | |
| set -euo pipefail | |
| DISK="${1:-/dev/vda}" | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "ERROR: must run as root (sudo $0)" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -b "$DISK" ]]; then | |
| echo "ERROR: $DISK is not a block device" >&2 | |
| exit 1 | |
| fi | |
| echo "Swap size?" | |
| echo " 1) 2GB" | |
| echo " 2) 4GB (default)" | |
| echo " 3) 8GB" | |
| read -r -p "Choice [1-3, default 2]: " choice | |
| case "$choice" in | |
| 1) SWAP="2GB" ;; | |
| 2|"") SWAP="4GB" ;; | |
| 3) SWAP="8GB" ;; | |
| *) echo "ERROR: invalid choice '$choice'" >&2; exit 1 ;; | |
| esac | |
| echo | |
| echo "About to WIPE $DISK:" | |
| echo " - partition 1: ext4, labelled 'nixos' (all space minus $SWAP)" | |
| echo " - partition 2: swap, $SWAP" | |
| read -r -p "Type 'yes' to continue: " confirm | |
| [[ "$confirm" == "yes" ]] || { echo "Aborted."; exit 1; } | |
| echo "Partitioning $DISK ..." | |
| parted --script "$DISK" -- mklabel msdos | |
| parted --script "$DISK" -- mkpart primary 1MB "-$SWAP" | |
| parted --script "$DISK" -- set 1 boot on | |
| parted --script "$DISK" -- mkpart primary linux-swap "-$SWAP" 100% | |
| udevadm settle | |
| echo "Formatting ..." | |
| mkfs.ext4 -L nixos "${DISK}1" | |
| mkswap -L swap "${DISK}2" | |
| echo "Mounting ..." | |
| mount /dev/disk/by-label/nixos /mnt | |
| swapon /dev/disk/by-label/swap | |
| echo "Generating initial NixOS config ..." | |
| nixos-generate-config --root /mnt | |
| cat <<EOF | |
| Done. Next steps: | |
| 1. vim /mnt/etc/nixos/configuration.nix # review (fetched from repo) | |
| 2. nixos-install --root /mnt | |
| 3. reboot (and remove the ISO) | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment