refactor: use zram as swap

This commit is contained in:
Price Hiller 2023-10-27 01:45:07 -05:00
parent 74651f60ad
commit 96f35ee16c
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8
3 changed files with 21 additions and 50 deletions

View File

@ -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" = {

View File

@ -22,7 +22,7 @@
extraModulePackages = [ ];
};
swapDevices = [{ device = "/dev/disk/by-label/NixOS-Swap"; }];
zramSwap.enable = true;
fileSystems = {
"/" = {

View File

@ -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 <int> | --swap <int> [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 "${@}"