From 2f7bbf3e1c8a8d7aa5a34aedd5ee033cef58efc4 Mon Sep 17 00:00:00 2001 From: Leyla Becker Date: Sat, 7 Mar 2026 14:33:08 -0600 Subject: [PATCH] feat: fixed more missing datasets --- configurations/nixos/emergent/default.nix | 1 + .../nixos/emergent/legacy-storage.nix | 51 +++++++++++++++++++ modules/nixos-modules/storage/storage.nix | 2 +- modules/nixos-modules/storage/zfs.nix | 8 ++- modules/nixos-modules/users.nix | 7 ++- 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 configurations/nixos/emergent/legacy-storage.nix diff --git a/configurations/nixos/emergent/default.nix b/configurations/nixos/emergent/default.nix index 452334a..3acaeda 100644 --- a/configurations/nixos/emergent/default.nix +++ b/configurations/nixos/emergent/default.nix @@ -3,5 +3,6 @@ imports = [ ./configuration.nix ./hardware-configuration.nix + ./legacy-storage.nix ]; } diff --git a/configurations/nixos/emergent/legacy-storage.nix b/configurations/nixos/emergent/legacy-storage.nix new file mode 100644 index 0000000..2b24729 --- /dev/null +++ b/configurations/nixos/emergent/legacy-storage.nix @@ -0,0 +1,51 @@ +# Legacy storage configuration for emergent +# This file manually defines ZFS datasets matching the existing on-disk layout +# to allow incremental migration to the new storage module (generateBase = true). +# +# Current on-disk dataset layout: +# rpool/local/ - parent (canmount=off) +# rpool/local/system/nix - nix store +# rpool/local/system/root - root filesystem +# +# Migration plan: +# Phase 1: Rename datasets on disk (boot from live USB) +# zfs rename -p rpool/local/system/nix rpool/persist/local/nix +# zfs rename rpool/local rpool/persist/local +# # This moves: local/system/root -> persist/local/root (need to rename after) +# # Actually, since local/system/root needs to become persist/local/root: +# zfs rename rpool/persist/local/system/root rpool/persist/local/root +# zfs destroy rpool/persist/local/system # now empty +# # Recreate blank snapshot: +# zfs destroy rpool/persist/local/root@blank +# zfs snapshot rpool/persist/local/root@blank +# +# Phase 2: Delete this file, remove its import from default.nix, rebuild. +{...}: { + # Disable automatic base dataset generation so we can define them manually + storage.generateBase = false; + + # Manually define ZFS datasets matching the existing on-disk layout + storage.zfs.datasets = { + "local" = { + type = "zfs_fs"; + mount = null; + }; + "local/system/nix" = { + type = "zfs_fs"; + mount = "/nix"; + atime = "off"; + relatime = "off"; + snapshot = { + autoSnapshot = false; + }; + }; + "local/system/root" = { + type = "zfs_fs"; + mount = "/"; + snapshot = { + blankSnapshot = true; + autoSnapshot = true; + }; + }; + }; +} diff --git a/modules/nixos-modules/storage/storage.nix b/modules/nixos-modules/storage/storage.nix index a0b4fc9..771d661 100644 --- a/modules/nixos-modules/storage/storage.nix +++ b/modules/nixos-modules/storage/storage.nix @@ -153,7 +153,7 @@ in { config.storage.datasets.replicate) ]; }) - (lib.mkIf (config.storage.zfs.enable && !config.storage.impermanence.enable) { + (lib.mkIf (config.storage.zfs.enable && !config.storage.impermanence.enable && config.storage.generateBase) { storage.datasets = { # Base organizational datasets (only needed when impermanence is disabled) local = { diff --git a/modules/nixos-modules/storage/zfs.nix b/modules/nixos-modules/storage/zfs.nix index 0d6ca18..2fc6cb4 100644 --- a/modules/nixos-modules/storage/zfs.nix +++ b/modules/nixos-modules/storage/zfs.nix @@ -9,6 +9,12 @@ args @ { # Hash function for disk names (max 27 chars to fit GPT limitations) hashDisk = drive: (builtins.substring 0 27 (builtins.hashString "sha256" drive)); + # Map "stripe" to "" for disko compatibility (disko uses "" for stripe mode) + diskoPoolMode = + if config.storage.zfs.pool.mode == "stripe" + then "" + else config.storage.zfs.pool.mode; + # Helper to flatten vdevs into list of devices with names allVdevDevices = lib.lists.flatten (builtins.map ( vdev: @@ -260,7 +266,7 @@ in { type = "topology"; vdev = builtins.map (vdev: { - mode = config.storage.zfs.pool.mode; + mode = diskoPoolMode; members = builtins.map (device: hashDisk device.device) vdev; }) config.storage.zfs.pool.vdevs; diff --git a/modules/nixos-modules/users.nix b/modules/nixos-modules/users.nix index 8a384e3..9cef952 100644 --- a/modules/nixos-modules/users.nix +++ b/modules/nixos-modules/users.nix @@ -409,10 +409,13 @@ in { ); # Post resume commands to rollback user home datasets to blank snapshots - boot.initrd.postResumeCommands = lib.mkAfter ( + # Only add these when generateBase is true -- when false, the legacy + # storage config is responsible for providing rollback commands with + # the correct (old) dataset paths. + boot.initrd.postResumeCommands = lib.mkIf config.storage.generateBase (lib.mkAfter ( lib.strings.concatLines (builtins.map (user: "zfs rollback -r rpool/ephemeral/home/${user.name}@blank") normalUsers) - ); + )); # TODO: I don't think we need this anymore but I have not tested it # Create persist home directories with proper permissions