# karch — Arch Linux Installer Script

A modular, TUI-driven Arch Linux installation script designed to be run directly from the live ISO. It automates the full install process — from disk partitioning to bootloader — while letting you choose between a **Desktop PC** (KDE Plasma) or **Server** (OpenSSH) configuration.

## Features

- **Two-phase design** — Phase 1 runs on the live ISO (networking, disks, pacstrap), then seamlessly continues Phase 2 inside `arch-chroot` (configuration, user setup, profile, bootloader). No heredocs or manual intervention between phases.
- **TUI menus** — all interactive choices use `dialog` for a clean terminal UI. If `dialog` is unavailable on the live ISO, every prompt gracefully falls back to plain text. Once internet is established, `dialog` is auto-installed and the script relaunches with full TUI support.
- **Auto-detection**:
  - Boot mode (UEFI / BIOS) via `/sys/firmware/efi/fw_platform_size`
  - CPU vendor → automatic `intel-ucode` or `amd-ucode` installation
  - Intel Wi-Fi NICs → `linux-firmware-intel`
  - Bluetooth hardware → conditional `bluez` / `bluez-utils` install
  - GPU vendor(s) — NVIDIA, AMD, Intel via PCI class codes (`lspci -d ::0300` / `::0302`)
  - NVIDIA GPU generation — Turing+ vs legacy (Maxwell/Pascal/Volta) for driver routing
- **GPU driver installation**:
  - **NVIDIA Turing+** (RTX 20xx/30xx/40xx/50xx, GTX 16xx) → `nvidia-open` from repos, with early KMS loading (mkinitcpio MODULES) and a pacman hook for automatic initramfs rebuilds
  - **NVIDIA Legacy** (GTX 7xx–10xx, Titan V/X) → helper script for building `nvidia-580xx-dkms` + `nvidia-580xx-utils` from the AUR post-boot, with option for Nouveau in the meantime
  - **AMD** → `mesa`, `vulkan-radeon`, `libva-mesa-driver`, `mesa-vdpau`
  - **Intel** → `mesa`, `vulkan-intel`, `intel-media-driver`
  - Manual override menu when auto-detection fails
- **Display manager choice** — select between **SDDM** (graphical, KDE default) or **tuigreet** (minimal TUI greeter via greetd)
- **Guided or manual partitioning** — guided mode uses `sgdisk` with sensible defaults (swap = half RAM, optional); manual mode drops into `cfdisk` with duplicate-role prevention.
- **Retry / Skip / Abort** on any module failure.
- **Resumable** — re-run with `--resume` to pick up from the last successfully completed module.

## Profiles

### PC (Desktop)

| Component | Packages |
|---|---|
| Desktop | `plasma-meta` (KDE Plasma 6, Wayland default) |
| Display Manager | `sddm` **or** `greetd` + `greetd-tuigreet` (user's choice) |
| GPU Drivers | Auto-detected — NVIDIA (`nvidia-open` or AUR legacy), AMD (`mesa`/`vulkan-radeon`), Intel (`mesa`/`vulkan-intel`), or manual selection |
| Apps | `konsole`, `dolphin`, `firefox` |
| Audio | `pipewire`, `pipewire-audio`, `pipewire-pulse`, `pipewire-alsa` |
| Networking | `networkmanager`, `wpa_supplicant` (+ `plasma-nm` via plasma-meta) |
| Bluetooth | `bluez`, `bluez-utils` (only if hardware detected) |
| Development | `git`, `base-devel` |
| Utilities | `htop`, `fastfetch` |

### Server

| Component | Packages |
|---|---|
| SSH | `openssh` (configurable port & root login policy) |
| Networking | `networkmanager`, `wpa_supplicant` |
| Development | `git`, `base-devel` |
| Utilities | `htop`, `fastfetch` |

## Common Configuration

Both profiles include:

- **Locales**: `en_AU.UTF-8` (system default), `en_US.UTF-8`, `ja_JP.UTF-8`
- **Console font**: Terminus — selectable from 14 options (`ter-116b` through `ter-132n`)
- **Hostname**: validated (alphanumeric + hyphens), written to `/etc/hostname` and `/etc/hosts`
- **Pacman**: `ILoveCandy` enabled
- **User**: non-root account with `sudo` via `wheel` group; root account locked
- **Bootloader**: GRUB (auto-detects UEFI vs BIOS; UEFI uses `--removable` fallback path for maximum firmware compatibility)

## Usage

### 1. Boot the Arch Linux live ISO

### 2. Get the scripts onto the live environment

**Option A — Git (if online):**
```bash
pacman -Sy git
git clone https://github.com/YOUR_USER/karch_scripts.git
```

**Option B — USB drive:**
```bash
mount /dev/sdX1 /mnt
cp -r /mnt/karch_scripts /root/karch_scripts
umount /mnt
```

### 3. Run the installer

```bash
cd karch_scripts
bash install.sh
```

### 4. Resume after failure (if needed)

```bash
bash install.sh --resume
```

## Project Structure

```
karch_scripts/
├── install.sh              # Entry point — phase dispatcher
├── lib/
│   └── common.sh           # Shared helpers (dialog, detection, env, run_module)
└── modules/
    ├── network.sh           # Phase 1: Wi-Fi / connectivity
    ├── disk.sh              # Phase 1: Partition, format, mount
    ├── base.sh              # Phase 1: pacstrap + hardware detection
    ├── locale.sh            # Phase 2: Locale generation
    ├── timezone.sh          # Phase 2: Timezone + hostname
    ├── fonts.sh             # Phase 2: Console & Japanese fonts
    ├── pacman_conf.sh       # Phase 2: Pacman tweaks
    ├── user.sh              # Phase 2: User account + sudo
    ├── profile.sh           # Phase 2: PC or Server packages
    └── bootloader.sh        # Phase 2: mkinitcpio + GRUB
```

## Module Execution Order

| Phase | Module | Description |
|---|---|---|
| 1 | `network` | Check connectivity, connect to Wi-Fi if needed |
| 1 | `disk` | Partition and mount target disk |
| 1 | `base` | `pacstrap` base system + auto-detected firmware |
| 2 | `locale` | Generate and set locales |
| 2 | `timezone` | Set timezone and hostname |
| 2 | `fonts` | Install Terminus + Japanese fonts |
| 2 | `pacman_conf` | Add `ILoveCandy` to pacman.conf |
| 2 | `user` | Create user, configure sudo, lock root |
| 2 | `profile` | Install PC or Server packages & services |
| 2 | `bootloader` | Install GRUB, rebuild initramfs |

## State File

Progress and shared configuration are stored in `/root/karch/karch.env`:

```bash
BOOT_MODE=uefi
DISK=/dev/nvme0n1
PART_EFI=/dev/nvme0n1p1
PART_SWAP=/dev/nvme0n1p2
PART_ROOT=/dev/nvme0n1p3
UCODE_PKG=intel-ucode
HAS_INTEL_WIFI=1
GPU_VENDORS=nvidia intel
NVIDIA_GEN=turing-plus
USERNAME=kris
HOSTNAME=archbox
PROFILE=pc
DISPLAY_MANAGER=tuigreet
COMPLETED_MODULES=network,disk,base,locale,timezone,fonts,pacman_conf,user,profile,bootloader
```

This file is copied into the chroot at `/mnt/root/karch/karch.env` so Phase 2 modules can read values set during Phase 1. It also tracks which modules have completed, enabling `--resume`.

## Error Handling

When any module fails, a dialog presents three options:

- **Retry** — re-run the failed module
- **Skip** — continue to the next module (the skipped module is *not* marked complete, so `--resume` will retry it)
- **Abort** — exit the installer immediately

## NVIDIA Legacy GPU Support

Since December 2025, the mainline `nvidia`/`nvidia-open` packages (590+) dropped support for Maxwell, Pascal, and Volta GPUs. These older cards now need `nvidia-580xx-dkms` from the AUR, which cannot be built during installation (no `makepkg` as root in chroot).

If a legacy NVIDIA GPU is detected, the installer offers to:
1. Create a **helper script** at `~/install-nvidia-legacy.sh` that handles the full AUR build + system configuration post-boot
2. Install **Nouveau** now for a usable desktop immediately
3. Both — Nouveau now + helper script for switching to proprietary later

The helper script handles: AUR clone → `makepkg -si` for both `-utils` and `-dkms` → mkinitcpio early KMS config → pacman hook → initramfs rebuild.

`linux-headers` is pre-installed during the chroot phase so DKMS compilation works on first boot.

## Requirements

- Arch Linux live ISO (any recent version)
- UEFI or BIOS system (auto-detected)
- Internet connection (Ethernet or Wi-Fi)

## License

MIT
