From 1a76be671093d14129fc2eb0638f495a7afe8b62 Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Mon, 17 Jul 2023 18:40:39 +0200
Subject: [PATCH] More zfs setup

---
 flake.nix                          | 10 ++++++++++
 home/lapetus.nix                   | 22 ++++++++++------------
 hosts/nixos/lapetus/default.nix    | 16 +++++++++++++++-
 hosts/nixos/lapetus/partitions.nix |  2 ++
 4 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/flake.nix b/flake.nix
index 3cd3a0c..72e8949 100644
--- a/flake.nix
+++ b/flake.nix
@@ -162,6 +162,12 @@
             hostname = "euporie";
             user = "guest";
           };
+
+          lapetus = nixos {
+            system = "x86_64-linux";
+            hostname = "lapetus";
+            user = "adrielus";
+          };
         };
       # }}}
       # {{{ Home manager
@@ -187,6 +193,10 @@
             system = "x86_64-linux";
             hostname = "euporie";
           };
+          "adrielus@lapetus" = mkHomeConfig {
+            system = "x86_64-linux";
+            hostname = "lapetus";
+          };
         };
       # }}}
     };
diff --git a/home/lapetus.nix b/home/lapetus.nix
index c9e1bb9..18d37cd 100644
--- a/home/lapetus.nix
+++ b/home/lapetus.nix
@@ -7,20 +7,18 @@
     inputs.agenix.packages.${pkgs.system}.agenix # Secret encryption
   ];
 
-  satellite = {
-    # Set up my custom imperanence wrapper
-    persistence = {
-      enable = true;
+  # Set up my custom imperanence wrapper
+  satellite.persistence = {
+    enable = true;
 
-      # Actual data/media (eg: projects, images, videos, etc)
-      at.data.path = "/persist/data";
-      at.data.prefixDirectories = false;
+    # Actual data/media (eg: projects, images, videos, etc)
+    at.data.path = "/persist/data";
+    at.data.prefixDirectories = false;
 
-      # App state I want to keep
-      at.state.path = "/persist/state";
+    # App state I want to keep
+    at.state.path = "/persist/state";
 
-      # App state which I should be able to delete at any point
-      at.cache.path = "/persist/local/cache";
-    };
+    # App state which I should be able to delete at any point
+    at.cache.path = "/persist/local/cache";
   };
 }
diff --git a/hosts/nixos/lapetus/default.nix b/hosts/nixos/lapetus/default.nix
index 20cf223..da48c73 100644
--- a/hosts/nixos/lapetus/default.nix
+++ b/hosts/nixos/lapetus/default.nix
@@ -1,4 +1,4 @@
-{
+{ config, ... }: {
   imports = [
     ../common/global
     ../common/users/adrielus.nix
@@ -14,6 +14,20 @@
   # https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion
   system.stateVersion = "22.11";
 
+  # Configure ZFS
+  boot.supportedFilesystems = [ "zfs" ];
+  boot.zfs.extraPools = [ "zroot" ];
+  boot.kernelPackages = config.boot.zfs.package.latestCompatibleLinuxPackages;
+
+  # We use non-legacy mountpoints
+  # See [this wiki link](https://nixos.wiki/wiki/ZFS)
+  # systemd.services.zfs-mount.enable = false;
+
+  # Roll back to blank snapshot on boot
+  # boot.initrd.postDeviceCommands = lib.mkAfter ''
+  #   zfs rollback -r zroot@blank
+  # '';
+
   # Boot
   boot.loader.grub = {
     enable = true;
diff --git a/hosts/nixos/lapetus/partitions.nix b/hosts/nixos/lapetus/partitions.nix
index 81ff83b..08405bd 100644
--- a/hosts/nixos/lapetus/partitions.nix
+++ b/hosts/nixos/lapetus/partitions.nix
@@ -39,10 +39,12 @@
     zpool = {
       zroot = {
         type = "zpool";
+
         postCreateHook = ''
           zfs snapshot zroot@blank
           zfs set keylocation="prompt" "zroot";
         '';
+
         rootFsOptions = {
           compression = "lz4";
           "com.sun:auto-snapshot" = "false";