diff --git a/systems/x86_64-linux/nemesis/disko.nix b/modules/nixos/hardware/btrfs.nix similarity index 52% rename from systems/x86_64-linux/nemesis/disko.nix rename to modules/nixos/hardware/btrfs.nix index 8be163a..5faa3fc 100644 --- a/systems/x86_64-linux/nemesis/disko.nix +++ b/modules/nixos/hardware/btrfs.nix @@ -1,32 +1,12 @@ -{lib, device, ...}: +{lib, config, ...}: +let + cfg = config.hardware.drives.btrfs; +in { - 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 - ''; - + config = lib.mkIf (cfg.enable) (lib.mkMerge [ + { disko.devices.disk.main = { - inherit device; + device = cfg.drive; type = "disk"; content.type = "gpt"; content.partitions = { @@ -68,4 +48,32 @@ "/nix".mountOptions = ["subvol=nix" "noatime"]; }; }; + } + (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 + ''; + }) + ]); } diff --git a/modules/nixos/hardware/default.nix b/modules/nixos/hardware/default.nix new file mode 100644 index 0000000..a53b299 --- /dev/null +++ b/modules/nixos/hardware/default.nix @@ -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 ""; + }; +} diff --git a/systems/x86_64-linux/nemesis/default.nix b/systems/x86_64-linux/nemesis/default.nix index 5ea38e7..e486ab2 100644 --- a/systems/x86_64-linux/nemesis/default.nix +++ b/systems/x86_64-linux/nemesis/default.nix @@ -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" ];