refactor(modules/hardware/drives): move disko config to a module

This commit is contained in:
Mohammad Rafiq 2025-05-18 14:33:22 +08:00
parent 67768361c2
commit 2cd82e2e2a
3 changed files with 56 additions and 33 deletions

View file

@ -1,32 +1,12 @@
{lib, device, ...}:
{lib, config, ...}:
let
cfg = config.hardware.drives.btrfs;
in
{
config = lib.mkIf (cfg.enable) (lib.mkMerge [
{
boot.initrd.postDeviceCommands = lib.mkAfter ''
mkdir /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
'';
disko.devices.disk.main = {
inherit device;
device = cfg.drive;
type = "disk";
content.type = "gpt";
content.partitions = {
@ -69,3 +49,31 @@
};
};
}
(lib.mkIf (cfg.ephemeralRoot) {
boot.initrd.postDeviceCommands = lib.mkAfter ''
mkdir /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

@ -0,0 +1,15 @@
{lib,...}:
{
imports = [
./btrfs.nix
];
options = {
hardware.drives.btrfs.enable = lib.mkEnableOption "";
hardware.drives.btrfs.drive = lib.mkOption {
type = lib.types.str;
default = "";
};
hardware.drives.btrfs.ephemeralRoot = lib.mkEnableOption "";
};
}

View file

@ -1,10 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{ config, lib, pkgs, ... }:
{
imports =
[
(modulesPath + "/installer/scan/not-detected.nix")
(import ./disko.nix {inherit lib; device = "/dev/disk/by-id/nvme-CT2000P3SSD8_2325E6E77434";})
];
hardware.drives.btrfs = {
enable = true;
drive = "/dev/disk/by-id/nvme-CT2000P3SSD8_2325E6E77434";
ephemeralRoot = true;
};
boot.initrd.availableKernelModules = [ "nvme" "xhci_pci" "ahci" "usbhid" "usb_storage" "sd_mod" ];
boot.initrd.kernelModules = [ "dm-snapshot" ];