From 96f35ee16cc41d7f87986c77feb9a34c4c451055 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 27 Oct 2023 01:45:07 -0500 Subject: [PATCH] refactor: use zram as swap --- hosts/luna/os/filesystem.nix | 2 ++ hosts/orion/os/filesystem.nix | 2 +- install.bash | 67 ++++++++++------------------------- 3 files changed, 21 insertions(+), 50 deletions(-) diff --git a/hosts/luna/os/filesystem.nix b/hosts/luna/os/filesystem.nix index 17bfecc..decd922 100644 --- a/hosts/luna/os/filesystem.nix +++ b/hosts/luna/os/filesystem.nix @@ -17,6 +17,7 @@ }; swapDevices = [{ device = "/dev/disk/by-label/NixOS-Swap"; }]; + swapDevices = [{ device = "/swap/swapfile"; }]; fileSystems = { "/" = { @@ -39,6 +40,7 @@ }; }; + zramSwap.enable = true; environment.persistence = { "/nix/persist" = { diff --git a/hosts/orion/os/filesystem.nix b/hosts/orion/os/filesystem.nix index 0e5968d..de1c23e 100644 --- a/hosts/orion/os/filesystem.nix +++ b/hosts/orion/os/filesystem.nix @@ -22,7 +22,7 @@ extraModulePackages = [ ]; }; - swapDevices = [{ device = "/dev/disk/by-label/NixOS-Swap"; }]; + zramSwap.enable = true; fileSystems = { "/" = { diff --git a/install.bash b/install.bash index 1cf3b79..3505a11 100644 --- a/install.bash +++ b/install.bash @@ -1,24 +1,15 @@ #!/usr/env/bin bash - install() { set -euo pipefail local disk="${1}" local encrypt_disk="${2}" - local swap_size="${3}" local boot_partition="${disk}1" - local swap_partition="${disk}2" - local primary_partition="${disk}3" + local primary_partition="${disk}2" # The size is large because I'd like to be able to hibernate my laptop in its entirety. I have 64 GB of ram. - if [[ -z "${swap_size}" ]]; then - swap_size="$(grep MemTotal /proc/meminfo | awk '{print int(sqrt($2 / 1024 / 1024) * 2)}')" - fi - local swap_offset="$((swap_size + 1))" - local label_crypt_luks="NixOS-Crypt" - local label_swap="NixOS-Swap" local label_primary="NixOS-Primary" local label_boot="NixOS-Boot" @@ -26,8 +17,8 @@ install() { umount /mnt/**/* >/dev/null 2>&1 || true umount /mnt/* >/dev/null 2>&1 || true umount /mnt >/dev/null 2>&1 || true - cryptsetup close >/dev/null 2>&1 enc || true - dd if=/dev/zero of="${disk}" bs=512 count=1024 || true + cryptsetup close enc >/dev/null 2>&1 || true + wipefs -a "${disk}" || true ### Partition The Disk parted "${disk}" -- mklabel gpt @@ -36,12 +27,8 @@ install() { parted "${disk}" -- set 1 boot on mkfs.vfat "${boot_partition}" fatlabel "${boot_partition}" "${label_boot}" - # Swap Partition - parted -a optimal "${disk}" -- mkpart "${label_swap}" linux-swap 1Gib "${swap_offset}GB" - mkswap -L "${label_swap}" "${swap_partition}" - swapon "${swap_partition}" - # Nix Partition, where the OS will reside with our data - parted -a optimal "${disk}" -- mkpart "${label_primary}" "${swap_offset}GiB" 100% + # Primary Partition + parted -a optimal "${disk}" -- mkpart "${label_primary}" 1Gib 100% ### Encrypt if [[ "${encrypt_disk}" == "yes" ]]; then @@ -54,7 +41,7 @@ install() { fi ### BTRFS Setup - # Go ahead and make the unerypted BTRFS + # Go ahead and make the unencrypted BTRFS mkfs.btrfs -f -L "${label_primary}" "${primary_partition}" # Mount it @@ -79,9 +66,12 @@ install() { # WARN: ZLO *may* be a good solution, it can be VERY slow on incompressible data. Something to keep in mind. mount -t btrfs -o noatime,compress=zstd,subvol=@nix "${primary_partition}" /mnt/nix + # Persistence dir, dirs not to be wiped on reboot stored here mkdir -p /mnt/nix/persist - # Finally, actually install NixOS + # Actually install NixOS nixos-install --flake "git+file:.#orion" + # Finally, clone the flake into the persistent nixos config + git clone . /mnt/nix/persist/etc/nixos/ } usage() { @@ -103,17 +93,11 @@ usage() { Default: disabled Example: $(basename "${0}") -e - - -s | --swap [OPTIONAL] - The size of the swap partition in gigabytes. - Default: sqrt(Current system ram in GB) * 2 - Example: - $(basename "${0}") -s 32 __EOF__ } running_as_root() { - (( EUID == 0 )) + ((EUID == 0)) } main() { @@ -124,7 +108,6 @@ main() { local disk="" local encrypt_disk="no" - local swap_size="" while :; do case ${1} in @@ -148,20 +131,6 @@ main() { encrypt_disk="yes" printf "Enabled disk encryption\n" ;; - -s | --swap) - shift - if [[ "${1}" =~ ^[0-9]+$ ]]; then - swap_size="${1}" - if ((swap_size <= 0)); then - printf "Invalid value passed for swap! Expected a non-zero positive number, got: %s\n" "${swap_size}" - exit 1 - fi - printf "Set swap size to '%s'\n" "${swap_size}" - else - printf "Invalid value passed for swap! Expected a number got: %s\n" "${1}" - exit 1 - fi - ;; -?*) printf 'Unknown option: %s\n' "$1" >&2 usage @@ -179,13 +148,13 @@ main() { exit 1 fi read -r -s -n 1 -p "Press any key to begin NixOS installation to '${disk}'!" - printf "Installing in " - for i in {3..1}; do - printf "%s.." "${i}" - sleep 1 - done - printf "\n" - install "${disk}" "${encrypt_disk}" "${swap_size}" + # printf "\nInstalling in " + # for i in {3..1}; do + # printf "%s.." "${i}" + # sleep 1 + # done + # printf "\n" + install "${disk}" "${encrypt_disk}" } main "${@}"