Finally fixed hibernation

This commit is contained in:
Aleksandr Lebedev 2025-08-17 03:37:51 +02:00
parent 86eafdc904
commit 466cdb10b3
8 changed files with 20 additions and 418 deletions

View file

@ -1,63 +0,0 @@
{
device,
swapSize ? "16G",
}: {
disko.devices = {
disk.${device} = {
type = "disk";
inherit device;
content = {
type = "gpt"; # Initialize the disk with a GPT partition table
partitions = {
ESP = {
# Setup the EFI System Partition
type = "EF00"; # Set the partition type
size = "1000M"; # Make the partition a gig
content = {
type = "filesystem";
format = "vfat"; # Format it as a FAT32 filesystem
mountpoint = "/boot"; # Mount it to /boot
};
};
primary = {
# Setup the LVM partition
size = "100%"; # Fill up the rest of the drive with it
content = {
type = "lvm_pv"; # pvcreate
vg = "vg1";
};
};
};
};
};
lvm_vg = {
# vgcreate
vg1 = {
# /dev/vg1
type = "lvm_vg";
lvs = {
# lvcreate
swap = {
# Logical Volume = "swap", /dev/vg1/swap
size = swapSize;
content = {
type = "swap";
};
};
root = {
# Logical Volume = "root", /dev/vg1/root
size = "100%FREE"; # Use the remaining space in the Volume Group
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
mountOptions = [
"defaults"
];
};
};
};
};
};
};
}

View file

@ -1,26 +0,0 @@
{
device,
mountpoint ? "/run/extraDrive",
}: {
disko.devices = {
disk = {
"${device}" = {
inherit device;
type = "disk";
content = {
type = "gpt";
partitions = {
root = {
size = "100%";
content = {
type = "filesystem";
format = "ext4";
inherit mountpoint;
};
};
};
};
};
};
};
}

View file

@ -1,84 +0,0 @@
{
device ? throw "Set this to your disk device, e.g. /dev/sda",
lib,
...
}:
# IMPORTANT
# Calculate offset using https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate#Acquire_swap_file_offset
# AND create this config
# {
# boot = {
# kernelParams = [
# "resume_offset=YOUR_OFFSET"
# ];
# resumeDevice = "/dev/disk/by-label/nixos";
# };
#}
{
disko.devices = {
disk = {
nvme0n1 = {
type = "disk";
inherit device;
content = {
type = "gpt";
partitions = {
ESP = {
label = "boot";
name = "ESP";
size = "512M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = [
"defaults"
];
};
};
luks = {
size = "100%";
label = "luks";
content = {
type = "luks";
name = "root_vg";
extraOpenArgs = [
"--allow-discards"
"--perf-no_read_workqueue"
"--perf-no_write_workqueue"
];
# https://0pointer.net/blog/unlocking-luks2-volumes-with-tpm2-fido2-pkcs11-security-hardware-on-systemd-248.html
settings = {crypttabExtraOpts = ["fido2-device=auto" "token-timeout=10"];};
content = {
type = "btrfs";
extraArgs = ["-L" "nixos" "-f"];
subvolumes = {
"/root" = {
mountpoint = "/";
mountOptions = ["subvol=root" "compress=zstd" "noatime"];
};
"/nix" = {
mountpoint = "/nix";
mountOptions = ["subvol=nix" "compress=zstd" "noatime"];
};
"/persist" = {
mountpoint = "/persist";
mountOptions = ["subvol=persist" "compress=zstd" "noatime"];
};
"/swap" = {
mountpoint = "/swap";
swap.swapfile.size = "64G";
};
};
};
};
};
};
};
};
};
};
fileSystems."/persist".neededForBoot = true;
}

View file

@ -1,97 +0,0 @@
{device ? throw "Set this to your disk device, e.g. /dev/sda", ...}: {
disko.devices = {
disk.main = {
inherit device;
type = "disk";
content = {
type = "gpt";
partitions = {
boot = {
name = "boot";
size = "1M";
type = "EF02";
};
esp = {
name = "ESP";
size = "500M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
swap = {
size = "32G";
content = {
type = "swap";
resumeDevice = true;
};
};
root = {
name = "root";
size = "100%";
content = {
type = "lvm_pv";
vg = "root_vg";
};
};
};
};
};
lvm_vg = {
root_vg = {
type = "lvm_vg";
lvs = {
root = {
size = "100%FREE";
content = {
type = "btrfs";
extraArgs = ["-f"];
subvolumes = {
"/root" = {
mountpoint = "/";
};
"/persist" = {
mountOptions = ["subvol=persist" "noatime"];
mountpoint = "/persist";
};
"/nix" = {
mountOptions = ["subvol=nix" "noatime"];
mountpoint = "/nix";
};
};
};
};
};
};
};
};
boot.initrd.postDeviceCommands = ''
mkdir -p /btrfs_tmp
mount /dev/root_vg/root /btrfs_tmp
if [[ -e /btrfs_tmp/root ]]; then
mkdir -p /btrfs_tmp/old_roots
timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%-d_%H:%M:%S")
mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp"
fi
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
delete_subvolume_recursively "/btrfs_tmp/$i"
done
btrfs subvolume delete "$1"
}
for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +30); do
delete_subvolume_recursively "$i"
done
btrfs subvolume create /btrfs_tmp/root
umount /btrfs_tmp
'';
}

View file

@ -1,80 +0,0 @@
{device ? throw "Set this to your disk device, e.g. /dev/sda", ...}: {
disko.devices = {
disk = {
main = {
type = "disk";
device = device;
content = {
type = "gpt";
partitions = {
ESP = {
label = "boot";
name = "ESP";
size = "512M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = [
"defaults"
];
};
};
luks = {
size = "100%";
label = "luks";
content = {
type = "luks";
name = "cryptroot";
extraOpenArgs = [
"--allow-discards"
"--perf-no_read_workqueue"
"--perf-no_write_workqueue"
];
# https://0pointer.net/blog/unlocking-luks2-volumes-with-tpm2-fido2-pkcs11-security-hardware-on-systemd-248.html
settings = {crypttabExtraOpts = ["fido2-device=auto" "token-timeout=10"];};
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/persist";
};
};
};
};
};
};
};
nodev = {
"/" = {
fsType = "tmpfs";
mountOptions = ["defaults" "size=8G" "mode=755"];
};
};
};
fileSystems."/persist" = {
depends = ["/"];
neededForBoot = true;
};
fileSystems."/nix" = {
device = "/persist/nix";
options = ["bind"];
depends = ["/persist"];
neededForBoot = true;
};
fileSystems."/tmp" = {
device = "/persist/tmp";
options = ["bind"];
depends = ["/persist"];
neededForBoot = true;
};
swapDevices = [
{
device = "/persist/swapfile";
size = 64 * 1024; # 64 GB
}
];
boot.resumeDevice = "/persist/swapfile";
}

View file

@ -1,48 +0,0 @@
{device ? throw "Set this to your disk device, e.g. /dev/sda", ...}: {
disko.devices = {
disk.main = {
inherit device;
type = "disk";
content = {
type = "gpt";
partitions = {
boot = {
name = "boot";
size = "1M";
type = "EF02";
};
esp = {
name = "ESP";
size = "500M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
name = "root";
size = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/persist";
};
};
};
};
};
nodev = {
"/" = {
fsType = "tmpfs";
};
"/nix" = {
device = "/persist/nix";
};
"/tmp" = {
device = "/persist/tmp";
};
};
};
}

View file

@ -28,8 +28,8 @@ in {
"mem_sleep_default=deep" "mem_sleep_default=deep"
] ]
#https://github.com/nix-community/disko/issues/651#issuecomment-2383741717 #https://github.com/nix-community/disko/issues/651#issuecomment-2383741717
++ optional (!config.boot.initrd.systemd.enable) "resume_offset=${builtins.toString cfg.swapFileOffset}"; ++ optional (cfg.swapFileOffset != null) "resume_offset=${builtins.toString cfg.swapFileOffset}";
inherit (cfg) resumeDevice; resumeDevice = mkDefault cfg.resumeDevice;
}; };
services.logind = { services.logind = {
lidSwitch = mkDefault "suspend-then-hibernate"; lidSwitch = mkDefault "suspend-then-hibernate";