diff --git a/configuration.nix b/configuration.nix index 1716f0b..61ed7e7 100644 --- a/configuration.nix +++ b/configuration.nix @@ -89,6 +89,38 @@ # Enable the OpenSSH daemon. services.openssh.enable = true; + # Auto-grow root filesystem on boot (for live disk resize support) + # Works with VIRTIO-SCSI (/dev/sda) - detects partition layout automatically + systemd.services.auto-grow-root = { + description = "Auto-grow root filesystem"; + wantedBy = [ "multi-user.target" ]; + after = [ "local-fs.target" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + path = with pkgs; [ cloud-utils parted e2fsprogs btrfs-progs util-linux ]; + script = '' + set -euo pipefail + + ROOT_DEV=$(findmnt -n -o SOURCE /) + ROOT_DISK=$(lsblk -no PKNAME "$ROOT_DEV" | head -1) + + # If partitioned, grow partition first + if [[ "$ROOT_DEV" =~ [0-9]$ ]]; then + PART_NUM=$(echo "$ROOT_DEV" | grep -oE '[0-9]+$') + growpart "/dev/$ROOT_DISK" "$PART_NUM" 2>/dev/null || true + fi + + # Grow filesystem + FSTYPE=$(findmnt -n -o FSTYPE /) + case "$FSTYPE" in + ext4) resize2fs "$ROOT_DEV" 2>/dev/null || true ;; + btrfs) btrfs filesystem resize max / 2>/dev/null || true ;; + esac + ''; + }; + # This value determines the NixOS release from which the default # settings for stateful data, like file locations and database versions