#+TITLE: Infini-OPTIPLEX Setup
#+AUTHOR: Infinidoge
#+OPTIONS: toc:nil
#+LaTeX_CLASS_OPTIONS: [12pt]
#+LATEX_HEADER: \usepackage[margin=1in]{geometry}

* Partition Scheme

| Position | Label             | FS Type          | Size | Type                 |
|----------+-------------------+------------------+------+----------------------|
|        1 | =boot=            | =vfat= / =fat32= | 511M | EFI System Partition |
|        2 | =Infini-OPTIPLEX= | =btrfs=          | Rest | Root Partition       |
|        3 | =swap=            | =swap=           | 24G  | Swap Space           |

* Configuration

Setup for another server hosting computer, which I keep with me at college. Using the =server= form factor from =hardware/form=.

* Setup Scripts

#+NAME: preparation
#+BEGIN_SRC bash :tangle prep.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<boilerplate>>

<<mount_check>>

<<partitioning>>

<<filesystems>>

<<subvolumes>>
#+END_SRC

#+NAME: install
#+BEGIN_SRC bash :tangle install.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<mount>>

<<installing>>

<<install_extra>>

<<cleanup>>
#+END_SRC

#+NAME: mount
#+BEGIN_SRC bash :tangle mount.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<boilerplate>>

<<mount_check>>

<<mounting>>
#+END_SRC

#+NAME: bare_install
#+BEGIN_SRC bash :tangle bare_install.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<installing>>
#+END_SRC

#+NAME: install_extras
#+BEGIN_SRC bash :tangle install_extras.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<install_extra>>
#+END_SRC

#+NAME: data_setup
#+BEGIN_SRC bash :tangle data_setup.bash :shebang "#!/usr/bin/env bash" :noweb yes :comments noweb
<<boilerplate>>

<<mount_check>>

<<data_partitioning>>

<<data_filesystems>>

<<data_subvolumes>>
#+END_SRC

** Script Boilerplate

#+NAME: boilerplate
#+BEGIN_SRC bash
DISK=$1
PART=$DISK$2

sudo mkdir -p /mnt
#+END_SRC

#+NAME: mount_check
#+BEGIN_SRC bash
if mountpoint -q -- "/mnt"; then
    echo "ERROR: /mnt is a mounted filesystem, aborting"
    exit 1
fi
#+END_SRC

** Partitioning

#+NAME: partitioning
#+BEGIN_SRC bash
echo "LOG: Partitioning $DISK"
sudo parted $DISK -- mktable gpt
sudo parted $DISK -s -- mkpart ESP fat32 1MiB 512MiB
sudo parted $DISK -s -- mkpart primary btrfs 512MiB -12GiB
sudo parted $DISK -s -- mkpart primary linux-swap -12GiB 100%
sudo parted $DISK -s -- set 1 esp on
#+END_SRC

** Making Filesystems

#+NAME: filesystems
#+BEGIN_SRC bash
echo "LOG: Making filesystems"
echo "- Making fat32 filesystem on ${PART}1"
sudo mkfs.fat -F 32 -n boot "${PART}1"
echo "- Making btrfs filesystem on ${PART}2"
sudo mkfs.btrfs "${PART}2" -L "Infini-OPTIPLEX" -f
echo "- Making swap space on ${PART}3"
sudo mkswap -L swap "${PART}3"
#+END_SRC

** Making Subvolumes

#+NAME: subvolumes
#+BEGIN_SRC bash
echo "LOG: Making subvolumes on ${PART}2"
sudo mount "${PART}2" /mnt
sudo btrfs subvolume create /mnt/root
sudo btrfs subvolume create /mnt/root/home
sudo mkdir -p /mnt/root/etc
sudo btrfs subvolume create /mnt/root/etc/nixos
sudo btrfs subvolume create /mnt/root/etc/nixos-private
sudo btrfs subvolume create /mnt/root/etc/ssh
sudo btrfs subvolume create /mnt/boot
sudo btrfs subvolume create /mnt/nix
sudo btrfs subvolume create /mnt/nix/store
sudo umount /mnt
#+END_SRC

** Mounting Volumes

#+NAME: mounting
#+BEGIN_SRC bash
echo "LOG: Mounting tmpfs"
sudo mount -t tmpfs root /mnt

mntopts="autodefrag,noatime,compress=zstd:1"

echo "LOG: - Mounting persistent directories"
sudo mkdir -p /mnt/persist /mnt/nix /mnt/boot /mnt/etc/ssh
sudo mount -o subvol=root,$mntopts "${PART}2" /mnt/persist
sudo mount -o subvol=nix,$mntopts "${PART}2" /mnt/nix
sudo mount -o subvol=boot,$mntopts "${PART}2" /mnt/boot
sudo mount -o subvol=root/etc/ssh,$mntopts "${PART}2" /mnt/etc/ssh

echo "LOG: - - Mounting persistent subdirectories"
sudo mkdir -p /mnt/home
sudo mount --bind /mnt/persist/home /mnt/home

echo "LOG: - Mounting EFI System Partition"
sudo mkdir -p /mnt/boot/efi
sudo mount "${PART}1" /mnt/boot/efi
#+END_SRC

** Installing

#+NAME: installing
#+BEGIN_SRC bash
echo "LOG: Installing NixOS"
sudo nixos-install --flake /etc/nixos#Infini-OPTIPLEX --no-root-password
#+END_SRC

** Extra (Install)

#+NAME: install_extra
#+BEGIN_SRC bash
echo "LOG: Cloning configuration"
sudo chown -R infinidoge /mnt/persist/etc/nixos /mnt/persist/etc/nixos-private

git clone --no-hardlinks --progress ssh://git@github.com/infinidoge/universe.git /mnt/persist/etc/nixos
git clone --no-hardlinks --progress ssh://git@github.com/infinidoge/universe-private.git /mnt/persist/etc/nixos-private
#+END_SRC

** Cleanup

#+NAME: cleanup
#+BEGIN_SRC bash
echo "LOG: Unmounting all"
sudo umount -R /mnt
#+END_SRC

** Setup Data Drive

*** Partitioning

#+NAME: data_partitioning
#+BEGIN_SRC bash
echo "LOG: Partitioning $DISK for data storage"
sudo parted $DISK -- mktable gpt
sudo parted $DISK -s -- mkpart primary btrfs 0% 100%
#+END_SRC

*** Making Filesystems

#+NAME: data_filesystems
#+BEGIN_SRC bash
echo "LOG: Making data filesystems"
echo "- Making btrfa filesystem on ${PART}1"
sudo mkfs.btrfs "${PART}1" --csum xxhash -L "data" -f
#+END_SRC

*** Making Subvolumes

#+NAME: data_subvolumes
#+BEGIN_SRC bash
echo "LOG: Making data subvolumes on ${PART}1"
sudo mount "${PART}1" /mnt
sudo btrfs subvolume create /mnt/root
sudo btrfs subvolume create /mnt/root/srv
sudo btrfs subvolume create /mnt/root/srv/jellyfin
#+END_SRC
