ZFS: Difference between revisions

Tie-ling (talk | contribs)
Take snapshots automatically: add services.zfs.autoSnapshot
 
(37 intermediate revisions by 14 users not shown)
Line 1: Line 1:
[https://zfsonlinux.org/ {{PAGENAME}}] ([[wikipedia:en:{{PAGENAME}}]]) - also known as [https://openzfs.org/ OpenZFS] ([[wikipedia:en:OpenZFS]]) - is a modern filesystem[[category:filesystem]] which is well supported on [[NixOS]].
[https://zfsonlinux.org/ {{PAGENAME}}] ([[wikipedia:en:{{PAGENAME}}]]), also known as [https://openzfs.org/ OpenZFS] ([[wikipedia:en:OpenZFS]]), is a modern filesystem which is well supported on [[NixOS]].
[[category:filesystem]]
Besides the {{nixos:package|zfs}} package (''ZFS Filesystem Linux Kernel module'') itself, there are many packages in the ZFS ecosystem available.


Besides the ''zfs'' package (''ZFS Filesystem Linux Kernel module'') <ref>https://search.nixos.org/packages?channel=unstable&show=zfs&query=zfs</ref> itself there are many packages in the [[{{PAGENAME}}]] ecosystem available.
ZFS integrates into NixOS via the {{nixos:option|boot.zfs}} and {{nixos:option|services.zfs}} options.


[[{{PAGENAME}}]] integrates into NixOS via the ''boot.zfs''<ref>https://search.nixos.org/options?channel=unstable&query=boot.zfs</ref> and ''service.zfs''<ref>https://search.nixos.org/options?channel=unstable&query=services.zfs</ref> options.
== Limitations ==


== Limitations ==
==== Latest Kernel compatible with ZFS ====
ZFS often does not support the latest Kernel versions. It is recommended to use an LTS Kernel version whenever possible; the NixOS default Kernel is generally suitable. See [[Linux kernel|Linux Kernel]] for more information about configuring a specific Kernel version.


==== Latest kernel compatible with ZFS ====
If your config specifies a Kernel version that is not officially supported by upstream ZFS, the ZFS module will fail to evaluate with an error that the ZFS package is "broken". Upstream ZFS changed in 2.3 to refuse to build by default, regardless of Nixpkgs’ broken marking (or ignoring).


Newer kernels might not be supported by ZFS yet. If you are running a kernel which is not officially supported by zfs, the module will refuse to evaluate and show an error.
===== Selecting the latest ZFS-compatible Kernel =====
{{Warning|This will often result in the Kernel version going backwards as Kernel versions become end-of-life and are removed from Nixpkgs. If you need more control over the Kernel version due to hardware requirements, consider simply pinning a specific version rather than calculating it as below.}}
To use the latest ZFS-compatible Kernel currently available, the following configuration may be used.  


You can pin to a newer kernel version explicitly, but note that this version may be dropped by upstream and in nixpkgs prior to zfs supporting the next version. See [[Linux kernel]] for more information.<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   boot.kernelPackages = pkgs.linuxPackages_latest;
   config,
   # OR
   lib,
   boot.kernelPackages = pkgs.linuxPackages_6_6
   pkgs,
}
  ...
</syntaxhighlight>This snippet will configure the latest compatible kernel:
}:


<syntaxhighlight lang="nix">
{ lib, pkgs, ... }:
let
let
   latestZfsCompatibleLinuxPackages = lib.pipe pkgs.linuxKernel.packages [
   zfsCompatibleKernelPackages = lib.filterAttrs (
     builtins.attrValues
     name: kernelPackages:
     (builtins.filter (
     (builtins.match "linux_[0-9]+_[0-9]+" name) != null
      kPkgs:
    && (builtins.tryEval kernelPackages).success
      (builtins.tryEval kPkgs).success
    && (!kernelPackages.${config.boot.zfs.package.kernelModuleAttribute}.meta.broken)
      && kPkgs ? kernel
  ) pkgs.linuxKernel.packages;
      && kPkgs.kernel.pname == "linux"
  latestKernelPackage = lib.last (
      && !kPkgs.zfs.meta.broken
     lib.sort (a: b: (lib.versionOlder a.kernel.version b.kernel.version)) (
    ))
      builtins.attrValues zfsCompatibleKernelPackages
     (builtins.sort (a: b: (lib.versionOlder a.kernel.version b.kernel.version)))
     )
     lib.last
   );
   ];
in
in
{
{
   # Note this might jump back and forth as kernel get added or removed.
   # Note this might jump back and forth as kernels are added or removed.
   boot.kernelPackages = latestZfsCompatibleLinuxPackages;
   boot.kernelPackages = latestKernelPackage;
}
}
</syntaxhighlight>
</syntaxhighlight>


==== Partial support for SWAP on ZFS ====
===== Using unstable, pre-release ZFS =====
{{Warning|Pre-release ZFS versions may be less well-tested, and may have critical bugs that may cause data loss.}}{{Warning|Running ZFS with a Kernel unsupported by upstream “is considered EXPERIMENTAL by the OpenZFS project. Even if it appears to build and run correctly, there may be bugs that can cause SERIOUS DATA LOSS.”}}
In some cases, a pre-release version of ZFS may be available that supports a newer Kernel. Use it with <code>boot.zfs.package = pkgs.zfs_unstable;</code>. Using zfs_unstable may allow the use of an unsupported Kernel; as warned above, [https://github.com/openzfs/zfs/blob/6a2f7b38442b42f4bc9a848f8de10fc792ce8d76/config/kernel.m4#L473-L487 upstream considers this experimental].


ZFS does not support swapfiles. SWAP devices can be used instead. Additionally, hibernation is disabled by default due to a [https://github.com/NixOS/nixpkgs/pull/208037 high risk] of data corruption. Note that even if that pull request is merged, it does not fully mitigate the risk. If you wish to enable hibernation regardless and made sure that not swapfiles on ZFS are used, set <code>boot.zfs.allowHibernation = true</code>.
==== Partial support for swap on ZFS ====
 
ZFS does not support swapfiles. swap devices can be used instead. Additionally, hibernation is disabled by default due to a [https://github.com/NixOS/nixpkgs/pull/208037 high risk] of data corruption. Note that even if that pull request is merged, it does not fully mitigate the risk. If you wish to enable hibernation regardless and made sure that swapfiles on ZFS are not used, set <code>boot.zfs.allowHibernation = true</code>.


==== Zpool not found ====
==== Zpool not found ====
Line 51: Line 57:
The differences can be tested by running <code>zpool import -d /dev/disk/by-id</code> when none of the pools are discovered, eg. a live iso.
The differences can be tested by running <code>zpool import -d /dev/disk/by-id</code> when none of the pools are discovered, eg. a live iso.


==== declarative mounting of ZFS datasets ====
==== ZFS conflicting with systemd ====
 
ZFS will manage mounting non-legacy ZFS filesystems, but NixOS tries to manage mounting with systemd. ZFS native mountpoints are not managed as part of the system configuration (but better support hibernation with a separate swap partition). This can lead to conflicts if the ZFS mount service is also enabled for the same datasets.


When using legacy mountpoints (created with eg<code>zfs create -o mountpoint=legacy</code>) mountpoints must be specified with <code>fileSystems."/mount/point" = {};</code>. ZFS native mountpoints are not managed as part of the system configuration but better support hibernation with a separate swap partition. This can lead to conflicts if ZFS mount service is also enabled for the same datasets. Disable it with <code>systemd.services.zfs-mount.enable = false;</code>.
Disable the mount service with <code>systemd.services.zfs-mount.enable = false;</code> or remove the <code>fileSystems</code> entries in hardware-configuration.nix. Otherwise, use legacy mountpoints (created with e.g. <code>zfs create -o mountpoint=legacy</code>). Mountpoints must be specified with <code>fileSystems."/mount/point" = {};</code> or with <code>nixos-generate-config</code>.


== Guides ==
== Guides ==


==== '''OpenZFS Documentation for installing''' ====
=== Root on ZFS with disko ===
 
{{warning|This guide is not endorsed by NixOS and some features like immutable root do not have upstream support and could break on updates. If an issue arises while following this guide, please consult the guides support channels.}}
 
One guide for a NixOS installation with ZFS is maintained at [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/ OpenZFS Documentation (''Getting Started'' for ''NixOS'')]
 
It is about:
* [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/index.html#installation enabling ZFS on an existing NixOS installation] and
* [https://openzfs.github.io/openzfs-docs/Getting%20Started/NixOS/#root-on-zfs (installing NixOS with) Root on ZFS].


It is not about:
disko[https://github.com/nix-community/disko/blob/master/example/zfs.nix] can partition disks declaratively and handle mount points at install time.
* Give understandable, easy to follow and close to the standard installation guide instructions
* integrating ZFS into your existing config


Don't follow the Root on ZFS guide found in OpenZFS documentation. It was abandoned and has not been updated in years. See commit log for the openzfs-docs repo for details.


==== '''Simple NixOS ZFS in root installation''' ====
=== Simple NixOS ZFS on root installation ===
 
Start from here in the NixOS manual: [https://nixos.org/manual/nixos/stable/#sec-installation-manual].
Start from here in the NixOS manual: [https://nixos.org/manual/nixos/stable/#sec-installation-manual].
Under manual partitioning [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning] do this instead:
Under manual partitioning [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning] do this instead:


'''Partition your disk with your favorite partition tool.'''
==== Partition the disk ====
 
We need the following partitions:
We need the following partitions:


Line 88: Line 85:
In low-memory situations, ZFS therefore might need a bit longer to free up memory from its cache. The swap partition will help with that.
In low-memory situations, ZFS therefore might need a bit longer to free up memory from its cache. The swap partition will help with that.


Example with gdisk:
Example with gdisk using <code>/dev/nvme0n1</code> as the device (use <code>lsblk</code> to find the device</code>):


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 131: Line 128:
The operation has completed successfully.
The operation has completed successfully.
</syntaxhighlight>
</syntaxhighlight>
Final partition table
Final partition table (<code>fdisk -l /dev/nvme0n1</code>):
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
Number  Start (sector)    End (sector)  Size      Code  Name
Number  Start (sector)    End (sector)  Size      Code  Name
Line 139: Line 136:
</syntaxhighlight>
</syntaxhighlight>


'''Let's use variables from now on for simplicity.
'''Let's use variables from now on for simplicity.''' Get the device ID in <code>/dev/disk/by-id/</code> (using {{ic|blkid}}), in our case here it is <code>nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O</code>
Get the device ID in <code>/dev/disk/by-id/</code>, in our case here it is <code>nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O</code>
 
'''
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
BOOT=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1
BOOT=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1
SWAP=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2
SWAP=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2
DISK=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part3
DISK=/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part3
</syntaxhighlight>


'''Make zfs pool with encryption and mount points:'''
{{note|It is often recommended to specify the drive using the device ID/UUID to prevent incorrect configuration, but it is also possible to use the device name (e.g. /dev/sda). See also: [[#Zpool created with bus-based disk names]], [https://wiki.archlinux.org/title/Persistent_block_device_naming Persistent block device naming - ArchWiki]}}


'''Note:''' zpool config can significantly affect performance (especially the ashift option) so you may want to do some research. The [https://jrs-s.net/2018/08/17/zfs-tuning-cheat-sheet/ ZFS tuning cheatsheet] or [https://wiki.archlinux.org/title/ZFS#Storage_pools ArchWiki] is a good place to start.
==== Make a ZFS pool with encryption and mount points ====
 
{{Note|zpool config can significantly affect performance (especially the ashift option) so you may want to do some research. The ZFS tuning cheatsheet or ArchWiki is a good place to start.}}


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 163: Line 162:
zfs create zpool/home
zfs create zpool/home


# Mount root
mkdir -p /mnt
mkdir -p /mnt
mount -t zfs zpool/root /mnt -o zfsutil
mount -t zfs zpool/root /mnt -o zfsutil
# Mount nix, var, home
mkdir /mnt/nix /mnt/var /mnt/home
mkdir /mnt/nix /mnt/var /mnt/home
mount -t zfs zpool/nix /mnt/nix -o zfsutil
mount -t zfs zpool/nix /mnt/nix -o zfsutil
mount -t zfs zpool/var /mnt/var -o zfsutil
mount -t zfs zpool/var /mnt/var -o zfsutil
Line 186: Line 187:
</syntaxhighlight>
</syntaxhighlight>


'''Format boot partition with fat as filesystem'''
==== Format boot partition and enable swap ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
mkfs.fat -F 32 -n boot $BOOT
mkfs.fat -F 32 -n boot $BOOT
</syntaxhighlight>
</syntaxhighlight>


'''Enable swap'''
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
mkswap -L swap $SWAP
mkswap -L swap $SWAP
Line 197: Line 197:
</syntaxhighlight>
</syntaxhighlight>


'''Installation:'''
==== Installation ====
<syntaxhighlight lang="bash">
# Mount boot
# Mount boot
<syntaxhighlight lang="bash">
mkdir -p /mnt/boot
mkdir -p /mnt/boot
mount $BOOT /mnt/boot
mount $BOOT /mnt/boot
Line 214: Line 213:
Now edit the configuration.nix that was just created in <code>/mnt/etc/nixos/configuration.nix</code> and make sure to have at least the following content in it.
Now edit the configuration.nix that was just created in <code>/mnt/etc/nixos/configuration.nix</code> and make sure to have at least the following content in it.


<syntaxhighlight lang="nix">
{{file|/mnt/etc/nixos/configuration.nix|diff|3=
{
{
...
...
Line 221: Line 220:


   # for local disks that are not shared over the network, we don't need this to be random
   # for local disks that are not shared over the network, we don't need this to be random
   networking.hostId = "8425e349";
   # without this, "ZFS requires networking.hostId to be set" will be raised
networking.hostId = "8425e349";
...
...
</syntaxhighlight>
}
}}


Now check the hardware-configuration.nix in <code>/mnt/etc/nixos/hardware-configuration.nix</code> and add whats missing e.g. <code>options = [ "zfsutil" ]</code> for all filesystems except boot and <code>randomEncryption = true;</code> for the swap partition. Also change the generated swap device to the partition we created e.g. <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2</code> in this case and <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1</code> for boot.
Now check the hardware-configuration.nix in <code>/mnt/etc/nixos/hardware-configuration.nix</code> and add whats missing e.g. <code>options = [ "zfsutil" ]</code> for all filesystems except boot and <code>randomEncryption = true;</code> for the swap partition. Also change the generated swap device to the partition we created e.g. <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2</code> in this case and <code>/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part1</code> for boot.


<syntaxhighlight lang="nix">
{{file|/mnt/etc/nixos/configuration.nix|diff|3=
{
...
...
   fileSystems."/" = {  
   fileSystems."/" = {  
Line 233: Line 235:
     fsType = "zfs";
     fsType = "zfs";
     # the zfsutil option is needed when mounting zfs datasets without "legacy" mountpoints
     # the zfsutil option is needed when mounting zfs datasets without "legacy" mountpoints
    options = [ "zfsutil" ];
+    options = [ "zfsutil" ];
   };
   };


Line 239: Line 241:
     device = "zpool/nix";
     device = "zpool/nix";
     fsType = "zfs";
     fsType = "zfs";
    options = [ "zfsutil" ];
+    options = [ "zfsutil" ];
   };
   };


Line 245: Line 247:
     device = "zpool/var";
     device = "zpool/var";
     fsType = "zfs";
     fsType = "zfs";
    options = [ "zfsutil" ];
+    options = [ "zfsutil" ];
   };
   };


Line 251: Line 253:
     device = "zpool/home";
     device = "zpool/home";
     fsType = "zfs";
     fsType = "zfs";
    options = [ "zfsutil" ];
+    options = [ "zfsutil" ];
   };
   };


Line 260: Line 262:


   swapDevices = [{
   swapDevices = [{
    device = "/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2";
+    device = "/dev/disk/by-id/nvme-SKHynix_HFS512GDE9X081N_FNB6N634510106K5O-part2";
    randomEncryption = true;
+    randomEncryption = true;
   }];
   }];
}
}
</syntaxhighlight>
}}


Now you may install nixos with <code>nixos-install</code>
Now you may install NixOS with <code>nixos-install</code>.


== Importing on boot ==
== Importing on boot ==
Line 331: Line 333:


You can tweak the interval (defaults to once a week) and which pools should be scrubbed (defaults to all).
You can tweak the interval (defaults to once a week) and which pools should be scrubbed (defaults to all).
== Remote unlock ==
== Remote unlock ==
=== Unlock encrypted zfs via ssh on boot ===
=== Unlock encrypted ZFS via SSH on boot ===


{{note|As of 22.05, rebuilding your config with the below directions may result in a situation where, if you want to revert the changes, you may need to do some pretty hairy nix-store manipulation to be able to successfully rebuild, see https://github.com/NixOS/nixpkgs/issues/101462#issuecomment-1172926129}}
{{note|As of 22.05, rebuilding your config with the below directions may result in a situation where, if you want to revert the changes, you may need to do some pretty hairy nix-store manipulation to be able to successfully rebuild, see https://github.com/NixOS/nixpkgs/issues/101462#issuecomment-1172926129}}
Line 366: Line 366:
</syntaxhighlight>
</syntaxhighlight>
* In order to use DHCP in the initrd, network manager must not be enabled and <syntaxhighlight lang="nix" inline>networking.useDHCP = true;</syntaxhighlight> must be set.
* In order to use DHCP in the initrd, network manager must not be enabled and <syntaxhighlight lang="nix" inline>networking.useDHCP = true;</syntaxhighlight> must be set.
* If your network card isn't started, you'll need to add the according kernel module to the kernel and initrd as well, e.g. <syntaxhighlight lang="nix">
* If your network card isn't started, you'll need to add the according Kernel module to the Kernel and initrd as well, e.g. <syntaxhighlight lang="nix">
boot.kernelModules = [ "r8169" ];
boot.kernelModules = [ "r8169" ];
boot.initrd.kernelModules = [ "r8169" ];</syntaxhighlight>
boot.initrd.kernelModules = [ "r8169" ];</syntaxhighlight>To know what kernel modules are needed, run <code>nix shell nixpkgs#pciutils --command lspci -v | grep -iA8 'network\|ethernet'</code> .


After that you can unlock your datasets using the following ssh command:
After that you can unlock your datasets using the following ssh command:
Line 419: Line 419:
== Take snapshots automatically ==
== Take snapshots automatically ==


See <code>services.sanoid</code> section in <code>man configuration.nix</code>.
See {{nixos:option|services.zfs.autoSnapshot}} or {{nixos:option|services.sanoid}} section in <code>man configuration.nix</code>.


== NFS share ==
== NFS share ==
Line 434: Line 434:
</syntaxhighlight>
</syntaxhighlight>
Only this line is needed. Configure firewall if necessary, as described in [[NFS]] article.
Only this line is needed. Configure firewall if necessary, as described in [[NFS]] article.
{{warning|<code>zfs share</code> or <code>sharenfs</code> does not work if the <code>mountpoint</code> is set to <code>legacy</code> (or <code>none</code>, of course). I was unable to find a source for this behaviour, but I was stuck on the problem for days, until I realized the problem.  ::Reply: sharenfs controlls what
is written into <code>/etc/exports</code>.  If ZFS does not know the mountpoint, as is the case in
mountpoint legacy or none, the contents of <code>/etc/exports</code> would be wrong}}


Then, set <code>sharenfs</code> property:
Then, set <code>sharenfs</code> property:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# zfs set sharenfs="ro=192.168.1.0/24,all_squash,anonuid=70,anongid=70" rpool/myData
zfs set sharenfs="ro=192.168.1.0/24,all_squash,anonuid=70,anongid=70" rpool/myData
</syntaxhighlight>
</syntaxhighlight>
For more options, see <code>man 5 exports</code>.
For more options, see <code>man 5 exports</code>.
Line 443: Line 447:
Todo: sharesmb property for Samba.
Todo: sharesmb property for Samba.


== Mail notification for ZFS Event Daemon ==
== Mail notifications (ZFS Event Daemon) ==
 
ZFS Event Daemon (zed) monitors events generated by the ZFS kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options]


=== Alternative 1: Enable Mail Notification without Re-compliation ===
ZFS Event Daemon (zed) monitors events generated by the ZFS Kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options]


First, we need to configure a mail transfer agent, the program that sends email:
First, we need to configure a mail transfer agent, the program that sends email:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
  age.secrets.msmtp = {
    file = "${inputs.self.outPath}/secrets/msmtp.age";
  };
  # for zed enableMail, enable sendmailSetuidWrapper
  services.mail.sendmailSetuidWrapper.enable = true;
   programs.msmtp = {
   programs.msmtp = {
     enable = true;
     enable = true;
Line 457: Line 466:
     defaults = {
     defaults = {
       aliases = "/etc/aliases";
       aliases = "/etc/aliases";
       port = 465;
       port = 587;
       tls_trust_file = "/etc/ssl/certs/ca-certificates.crt";
       auth = "plain";
       tls = "on";
       tls = "on";
      auth = "login";
       tls_starttls = "on";
       tls_starttls = "off";
     };
     };
     accounts = {
     accounts = {
       default = {
       default = {
         host = "mail.example.com";
         host = "smtp.mail.example.com";
         passwordeval = "cat /etc/emailpass.txt";
         passwordeval = "cat ${config.age.secrets.msmtp.path}";
         user = "user@example.com";
         user = "myname@example.com";
         from = "user@example.com";
         from = "myname@example.com";
       };
       };
     };
     };
Line 477: Line 485:
Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.
Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="nix">
tee -a /etc/aliases <<EOF
{
root: user@example.com
  environment.etc.aliases.text = ''
EOF
    root: admin@example.com
  '';
}
</syntaxhighlight>
</syntaxhighlight>


Finally, override default zed settings with a custom one:
Finally, enable zed mail notification:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   services.zfs.zed.settings = {
   services.zfs.zed. = {
     ZED_DEBUG_LOG = "/tmp/zed.debug.log";
     enableMail = true;
     ZED_EMAIL_ADDR = [ "root" ];
     settings = {
    ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";
      ZED_EMAIL_ADDR = [ "root" ];
    ZED_EMAIL_OPTS = "@ADDRESS@";
      # send notification if scrub succeeds
 
      ZED_NOTIFY_VERBOSE = true;
    ZED_NOTIFY_INTERVAL_SECS = 3600;
     };
    ZED_NOTIFY_VERBOSE = true;
 
     ZED_USE_ENCLOSURE_LEDS = true;
    ZED_SCRUB_AFTER_RESILVER = true;
   };
   };
  # this option does not work; will return error
  services.zfs.zed.enableMail = false;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 508: Line 512:
</syntaxhighlight>
</syntaxhighlight>


=== Alternative 2: Rebuild ZFS with Mail Support ===
The <code>zfs</code> package can be rebuilt with mail features. However, please note that this will cause Nix to recompile the entire ZFS package on the computer, and on every kernel update, which could be very time-consuming on lower-end NAS systems.
An alternative solution that does not involve recompliation can be found above.
The following override is needed as <code>zfs</code> is implicitly used in partition mounting:
<syntaxhighlight lang="nix">
nixpkgs.config.packageOverrides = pkgs: {
  zfsStable = pkgs.zfsStable.override { enableMail = true; };
};
</syntaxhighlight>
A mail sender like [[msmtp]] or [[postfix]] is required.
A minimal, testable ZED configuration example:
<syntaxhighlight lang="nix">
services.zfs.zed.enableMail = true;
services.zfs.zed.settings = {
  ZED_EMAIL_ADDR = [ "root" ];
  ZED_NOTIFY_VERBOSE = true;
};
</syntaxhighlight>
Above, <code>ZED_EMAIL_ADDR</code> is set to <code>root</code>, which most people will have an alias for in their mailer. You can change it to directly mail you: <code>ZED_EMAIL_ADDR = [ "you@example.com" ];</code>
ZED pulls in <code>mailutils</code> and runs <code>mail</code> by default, but you can override it with <code>ZED_EMAIL_PROG</code>. If using msmtp, you may need <code>ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";</code>.
You can customize the mail command with <code>ZED_EMAIL_OPTS</code>. For example, if your upstream mail server requires a certain FROM address: <code>ZED_EMAIL_OPTS = "-r 'noreply@example.com' -s '@SUBJECT@' @ADDRESS@";</code>


[[Category:Guide]]
[[Category:Guide]]