Difference between revisions of "ZFS"

From NixOS Wiki
Jump to: navigation, search
(remove zfs legacy crypto migration guide)
 
(142 intermediate revisions by 59 users not shown)
Line 1: Line 1:
NixOS has native support for ZFS.
+
[https://zfsonlinux.org/ ZFS], also known as [https://openzfs.org/ OpenZFS], is a modern filesystem[[category:filesystem]] which is well supported on [[NixOS]].
It uses the code from the [http://zfsonlinux.org/ ZFS on Linux project], including kernel modules and userspace utilities.
 
  
== What works ==
+
== Guides ==
  
All functionality supported by ZFS on Linux, including:
+
=== Installation ===
* Using ZFS as the root filesystem (using either MS-DOS or GPT partitions)
 
* Encrypted ZFS pools (using either native encryption or Linux's dm-crypt)
 
* All the other ZFS goodies (cheap snapshotting, checksumming, compression, RAID-Z, ...)
 
* Auto-snapshotting service
 
  
== Known issues ==
+
Start from here in the NixOS manual [https://nixos.org/manual/nixos/stable/#sec-installation-manual] and under manual partitioning [https://nixos.org/manual/nixos/stable/#sec-installation-manual-partitioning] do this instead:
  
* As of 2014-03-04, you shouldn't use a ZVol as a swap device, as it can deadlock under memory pressure
+
'''Partition your disk with an boot and an zfs partition with your favorite partition tool.'''
* As of 2014-03-04, you should set the <code>mountpoint</code> property of your ZFS filesystems to be <code>legacy</code> and let NixOS mount them like any other filesystem (such as ext4 or btrfs), otherwise some filesystems may fail to mount due to ordering issues
 
* As of 2014-03-04, all ZFS pools available to the system will be forcibly imported during boot, regardless if you had imported them before or not. You should be careful not to have any other system accessing them at the same time, otherwise it will corrupt your pools. Normally (for the common desktop user) this should not be a problem, as a hard disk is usually only directly connected to one machine.
 
  
== How to use it ==
+
Eg. 1G for boot partion, rest for zfs.
  
Just add the following to your <code>configuration.nix</code> file:
+
Example output from fdisk:
  
<syntaxhighlight lang="nix">
+
<syntaxhighlight lang="bash">
boot.supportedFilesystems = [ "zfs" ];
+
 
 +
fdisk /dev/nvme0n1
 +
Command (m for help): p
 +
 
 +
Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
 +
...
 +
Device          Start        End    Sectors  Size Type
 +
/dev/nvme0n1p1    2048    2099199    2097152    1G EFI System
 +
/dev/nvme0n1p2 2099200 1953523711 1951424512 930.5G Linux filesystem
 +
</syntaxhighlight>
 +
'''Note:''' Remember to set boot partition (first partition) to "EFI System" type
 +
 
 +
'''Make 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 [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.
 +
<syntaxhighlight lang="bash">
 +
zpool create -O encryption=on -O keyformat=passphrase -O keylocation=prompt -O compression=on -O mountpoint=none -O xattr=sa -O acltype=posixacl -o ashift=12 zpool /dev/nvme0n1p2
 +
 
 +
zfs create -o mountpoint=legacy zpool/root
 +
zfs create -o mountpoint=legacy zpool/nix
 +
zfs create -o mountpoint=legacy zpool/var
 +
zfs create -o mountpoint=legacy zpool/home
 +
 
 +
mkdir /mnt/root
 +
mount -t zfs zpool/root /mnt
 +
mkdir /mnt/nix /mnt/var /mnt/home
 +
 
 +
mount -t zfs zpool/nix /mnt/nix
 +
mount -t zfs zpool/var /mnt/var
 +
mount -t zfs zpool/home /mnt/home
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Be sure to also set networking.hostId, see https://nixos.org/nixos/manual/options.html#opt-networking.hostId
+
Output from <syntaxhighlight lang="bash" inline>zpool status</syntaxhighlight>:
 +
<syntaxhighlight >
 +
zpool status
 +
  pool: zpool
 +
state: ONLINE
 +
...
 +
config:
  
To activate the configuration and load the ZFS kernel module, run:
+
NAME                              STATE    READ WRITE CKSUM
 +
zpool                              ONLINE      0    0    0
 +
  nvme-eui.0025384b21406566-part2  ONLINE      0    0    0
  
 +
</syntaxhighlight>
 +
 +
'''Make fat filesystem on boot partition'''
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
nixos-rebuild switch
+
mkfs.fat -F 32 -n boot /dev/nvme0n1p1
 
</syntaxhighlight>
 
</syntaxhighlight>
  
All ZFS functionality should now be available.
+
'''Installation:'''
  
If you want NixOS to auto-mount your ZFS filesystems during boot, you should set their <code>mountpoint</code> property to <code>legacy</code> and treat it like if it were any other filesystem, i.e.: mount the filesystem manually and regenerate your list of filesystems, as such:
+
Install: [https://nixos.org/manual/nixos/stable/#sec-installation-manual-installing]
 +
 
 +
Jump to "2. UEFI systems"
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
zfs set mountpoint=legacy <pool>/<fs>
+
mkdir -p /mnt/boot
mount -t zfs <pool>/<fs> <mountpoint>
+
mount /dev/disk/by-label/boot /mnt/boot
 +
</syntaxhighlight>
  
# This will regenerate your /etc/nixos/hardware-configuration.nix file:
+
Jump to "4." ... /mnt/etc/nixos/configuration.nix ...
nixos-generate-config
 
  
nixos-rebuild switch
+
Continue from here and add this boot loader and filesystems config to your configuration.nix:
</syntaxhighlight>
 
  
NixOS will now make sure that your filesystem is always mounted during boot.
 
The <code>nixos-generate-config</code> command regenerates your <code>/etc/nixos/hardware-configuration.nix</code> file, which includes the list of filesystems for NixOS to mount during boot, e.g.:
 
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
 +
# Boot loader config for configuration.nix:
 +
  boot.loader.grub = {
 +
    enable = true;
 +
    zfsSupport = true;
 +
    efiSupport = true;
 +
    efiInstallAsRemovable = true;
 +
    mirroredBoots = [
 +
      { devices = [ "nodev"]; path = "/boot"; }
 +
    ];
 +
  };
 +
 +
  fileSystems."/" =
 +
    { device = "zpool/root";
 +
      fsType = "zfs";
 +
    };
 +
 +
  fileSystems."/nix" =
 +
    { device = "zpool/nix";
 +
      fsType = "zfs";
 +
    };
 +
 +
  fileSystems."/var" =
 +
    { device = "zpool/var";
 +
      fsType = "zfs";
 +
    };
 +
 
   fileSystems."/home" =
 
   fileSystems."/home" =
     { device = "rpool/home";
+
     { device = "zpool/home";
 
       fsType = "zfs";
 
       fsType = "zfs";
 
     };
 
     };
  
   fileSystems."/backup" =
+
   fileSystems."/boot" =
     { device = "rpool/backup";
+
     { device = "/dev/disk/by-uuid/2A11-F4EF";
       fsType = "zfs";
+
       fsType = "vfat";
 
     };
 
     };
 +
 +
  swapDevices = [ ];
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Changing the Cache Size ==
+
'''Note:''' Remove / comment out <syntaxhighlight lang="nix" inline>boot.loader.systemd-boot.enable = true;</syntaxhighlight> config if added by<syntaxhighlight lang="nix" inline>nixos-generate-config</syntaxhighlight>
 +
 
 +
=== Installation (alternative) ===
 +
 
 +
Alternative 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'')]
 +
 
 +
{{warning|It 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 guide's support channels.}}
 +
 
 +
== Tips ==
 +
 
 +
=== Importing pools at boot ===
  
ZFS has a complicated cache system.  The cache you're most likely to want to fiddle with is the called Adaptive Replacement Cache, usually abbreviated ARC. This is the first-level (fastest) of ZFS's caches.
+
If you create a zpool, it will not be imported on the next boot unless you either add the zpool name to <syntaxhighlight lang="nix" inline>boot.zfs.extraPools</syntaxhighlight>:
  
You can increase or decrease a parameter which represents approximately the maximum size of the ARC cache. You can't set its actual size (ZFS does that adaptively according to its workload), nor can you set its exact maximum size.
+
<syntaxhighlight lang="nix">
 +
## In /etc/nixos/configuration.nix:
 +
boot.zfs.extraPools = [ "zpool_name" ];
 +
</syntaxhighlight>
  
To change the maximum size of the ARC cache to (for example) 12 GB, add this to your NixOS configuration:
+
or if you are using legacy mountpoints, add a <syntaxhighlight lang="nix" inline>fileSystems</syntaxhighlight> entry and NixOS will automatically detect that the pool needs to be imported:
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
boot.kernelParams = ["zfs.zfs_arc_max=12884901888"];
+
## In /etc/nixos/configuration.nix:
 +
fileSystems."/mount/point" = {
 +
  device = "zpool_name";
 +
  fsType = "zfs";
 +
};
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In some versions of ZFS, you can change the maximum size of the ARC on the fly, but in NixOS 18.03 this is not possible. (Nor is it possible in other versions of ZFS on Linux yet, according to Stack Exchange.)
+
=== Mounting pools at boot ===
 +
 
 +
zfs-mount service is enabled by default on NixOS 22.05.
 +
 
 +
To automatically mount a dataset at boot, you only need to set <code>canmount=on</code> and <code>mountpoint=/mount/point</code> on the respective datasets.
 +
 
 +
=== Reservations ===
 +
 
 +
ZFS's performance will deteriorate significantly when more than 80% of the available space is used - to avoid this, reserve disk space beforehand.
 +
 
 +
To reserve space, create a new unused dataset that gets a guaranteed size of e.g. 10GB.
 +
 
 +
<syntaxhighlight lang="bash">
 +
zfs create -o refreservation=10G -o mountpoint=none zroot/reserved
 +
</syntaxhighlight>
  
== Automatic Scrubbing ==
+
=== Automatic scrubbing ===
  
 
Regular scrubbing of ZFS pools is recommended and can be enabled in your NixOS configuration via:
 
Regular scrubbing of ZFS pools is recommended and can be enabled in your NixOS configuration via:
 +
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
 
services.zfs.autoScrub.enable = true;
 
services.zfs.autoScrub.enable = true;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You can tweak the interval (defaults to once a week) and which pools should be scrubbed (defaults to all).
+
By default, the service will scrub pools once a week.
  
== Reservations ==
+
=== Automatic snapshots ===
  
Since zfs is a copy-on-write filesystem even for deleting files disk space is needed. Therefore it should be avoided
+
You can setup automatic snapshots by enabling the <code>zfs.autoSnapshot</code> service:
to run out of disk space. Luckily it is possible to reserve disk space for datasets to prevent this.
 
To enable reservations pick any dataset of your and do:
 
  
<syntaxhighlight lang="console">
+
<syntaxhighlight lang="nix" inline>services.zfs.autoSnapshot.enable = true;</syntaxhighlight>.
$ zfs set reservation=1G zroot # reserves enough disk space to have room for cleanups/deletion
 
</syntaxhighlight>
 
  
where <code>zroot</code> should be replaced by a dataset in your pool.
+
Alternatively, see <code>services.sanoid</code>.
  
== How to use the auto-snapshotting service ==
+
=== Trimming ===
  
To auto-snapshot a ZFS filesystem or a ZVol, set its <code>com.sun:auto-snapshot</code> property to <code>true</code>, like this:
+
Trimming is enabled by default - it's controlled by this option, if you'd like to disable it:
  
<syntaxhighlight lang="bash">
+
<syntaxhighlight lang="nix">
$ zfs set com.sun:auto-snapshot=true <pool>/<fs>
+
services.zfs.trim.enable
 
</syntaxhighlight>
 
</syntaxhighlight>
  
(Note that by default this property will be inherited by all descendent datasets, but you can set their properties to false if you prefer.)
+
Note that this is different from the <code>autotrim</code> pool property - for further information, see the <code>zpool-trim</code> and <code>zpoolprops</code> man pages.
 +
 
 +
=== Tuning Adaptive Replacement Cache size ===
 +
 
 +
To change the maximum size of the ARC to (for example) 12 GB, add this to your NixOS configuration:
 +
<syntaxhighlight lang="nix">
 +
boot.kernelParams = [ "zfs.zfs_arc_max=12884901888" ];
 +
</syntaxhighlight>
  
Then, to enable the auto-snapshot service, add this to your <code>configuration.nix</code>:
+
=== Tuning other parameters ===
  
 +
To tune other attributes of ARC, L2ARC or of ZFS itself via runtime modprobe config, add this to your NixOS configuration (keys and values are examples only!):
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
services.zfs.autoSnapshot.enable = true;
+
    boot.extraModprobeConfig = ''
 +
      options zfs l2arc_noprefetch=0 l2arc_write_boost=33554432 l2arc_write_max=16777216 zfs_arc_max=2147483648
 +
    '';
 
</syntaxhighlight>
 
</syntaxhighlight>
  
And finally, run <code>nixos-rebuild switch</code> to activate the new configuration!
+
You can confirm whether any specified configuration/tuning got applied via commands like <code>arc_summary</code> and <code>arcstat -a -s " "</code>.
 +
 
 +
=== Remote unlocking ===
 +
 
 +
{{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}}
  
By default, the auto-snapshot service will keep the latest four 15-minute, 24 hourly, 7 daily, 4 weekly and 12 monthly snapshots.
+
In case you want unlock a machine remotely (after an update), having an ssh service in initrd for the password prompt is handy:
You can globally override this configuration by setting the desired number of snapshots in your <code>configuration.nix</code>, like this:
 
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
services.zfs.autoSnapshot = {
+
boot = {
  enable = true;
+
  initrd.network = {
  frequent = 8; # keep the latest eight 15-minute snapshots (instead of four)
+
    # This will use udhcp to get an ip address.
  monthly = 1; # keep only one monthly snapshot (instead of twelve)
+
    # Make sure you have added the kernel module for your network driver to `boot.initrd.availableKernelModules`,
 +
    # so your initrd can load it!
 +
    # Static ip addresses might be configured using the ip argument in kernel command line:
 +
    # https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
 +
    enable = true;
 +
    ssh = {
 +
      enable = true;
 +
      # To prevent ssh clients from freaking out because a different host key is used,
 +
      # a different port for ssh is useful (assuming the same host has also a regular sshd running)
 +
      port = 2222;  
 +
      # hostKeys paths must be unquoted strings, otherwise you'll run into issues with boot.initrd.secrets
 +
      # the keys are copied to initrd from the path specified; multiple keys can be set
 +
      # you can generate any number of host keys using
 +
      # `ssh-keygen -t ed25519 -N "" -f /path/to/ssh_host_ed25519_key`
 +
      hostKeys = [ /path/to/ssh_host_ed25519_key ];
 +
      # public ssh key used for login
 +
      authorizedKeys = [ "ssh-rsa AAAA..." ];
 +
    };
 +
  };
 
};
 
};
 
</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.
 +
* 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.initrd.kernelModules = [ "r8169" ];</syntaxhighlight>
  
You can also disable a given type of snapshots on a per-dataset basis by setting a ZFS property, like this:
+
After that you can unlock your datasets using the following ssh command:
  
<syntaxhighlight lang="console">
+
<syntaxhighlight>
$ zfs set com.sun:auto-snapshot:weekly=false <pool>/<fs>
+
ssh -p 2222 root@host "zpool import -a; zfs load-key -a && killall zfs"
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This would disable only weekly snapshots on the given filesystem.
+
Alternatively you could also add the commands as postCommands to your configuration.nix, then you just have to ssh into the initrd:
  
== How to install NixOS on a ZFS root filesystem ==
+
<syntaxhighlight>
 +
boot = {
 +
  initrd.network = {
 +
    postCommands = ''
 +
    # Import all pools
 +
    zpool import -a
 +
    # Or import selected pools
 +
    zpool import pool2
 +
    zpool import pool3
 +
    zpool import pool4
 +
    # Add the load-key command to the .profile
 +
    echo "zfs load-key -a; killall zfs" >> /root/.profile
 +
    '';
 +
  };
 +
};
 +
</syntaxhighlight>
  
Here's an example of how to create a ZFS root pool using 4 disks in RAID-10 mode (striping+mirroring), create a ZFS root+home filesystems and install NixOS on them:
+
After that you can unlock your datasets using the following ssh command:
(thanks to Danny Wilson for the instructions)
 
  
<syntaxhighlight lang="bash">
+
<syntaxhighlight>
# Add the zfs filesystem to the install environment:
+
ssh -p 2222 root@host
nano /etc/nixos/configuration.nix
+
</syntaxhighlight>
  
## ---8<-------------------------8<---
+
=== NFS shares ===
  boot.supportedFilesystems = [ "zfs" ];
 
## ---8<-------------------------8<---
 
  
nixos-rebuild switch
+
With the <code>sharenfs</code> property, ZFS has build-in support for generating <code>/etc/exports.d/zfs.exports</code> file, which in turn is processed by NFS service automatically.
  
# Load the just installed ZFS kernel module
+
{{warning|If you are intending on defining an IPv6 subnet as part of your sharenfs rule, as of ZFS 2.0.6 (2021-09-23) please note that due to a bug in openzfs '''your rule will not correctly apply''', and may result in a security vulnerability (CVE-2013-20001). A fix has been implemented in the next yet-to-be-released upstream version - [https://github.com/openzfs/zfs/pull/11939 openzfs/zfs#11939]}}
modprobe zfs
 
  
# Create boot partition and (zfs) data partition
+
To enable NFS share on a dataset, only two steps are needed:
# See: https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-install-Ubuntu-to-a-Native-ZFS-Root-Filesystem#step-2-disk-partitioning
 
fdisk /dev/sda
 
  
# Copy the partition table to the other disks
+
First, enable [[NFS|NFS service]]:
sfdisk --dump /dev/sda | sfdisk /dev/sdb
+
<syntaxhighlight lang="nix">
sfdisk --dump /dev/sda | sfdisk /dev/sdc
+
services.nfs.server.enable = true;
sfdisk --dump /dev/sda | sfdisk /dev/sdd
+
</syntaxhighlight>
 +
Only this line is needed. Configure firewall if necessary, as described in [[NFS]] article.
  
# Create a RAID-10 ZFS pool. Use "-o ashift=12" to create your ZFS pool with 4K sectors
+
Then, set <code>sharenfs</code> property:
zpool create -o ashift=12 -o altroot=/mnt rpool mirror /dev/sda2 /dev/sdb2 mirror /dev/sdc2 /dev/sdd2
+
<syntaxhighlight lang="bash">
 +
zfs set sharenfs="ro=192.168.1.0/24,all_squash,anonuid=70,anongid=70" rpool/myData
 +
</syntaxhighlight>
 +
For more options, see <code>man 5 exports</code>.
  
# Create the filesystems
+
=== Samba Shares ===
zfs create -o mountpoint=none rpool/root
 
zfs create -o mountpoint=legacy rpool/root/nixos
 
zfs create -o mountpoint=legacy rpool/home
 
zfs set compression=lz4 rpool/home    # compress the home directories automatically
 
  
# Mount the filesystems manually
+
With the <code>sharesmb</code> property, ZFS can generate the necessary configuration exporting zfs filesystems over SMB. This functionality requires you to run the [[Samba|Samba service]] with support for the "usershares" feature configured.
mount -t zfs rpool/root/nixos /mnt
+
Possible configuration options for the usershares feature can be viewed in smb.conf(5)
  
mkdir /mnt/home
+
Enable the samba service configure it to enable usershare support.
mount -t zfs rpool/home /mnt/home
 
  
# Create a raid mirror of the first partitions for /boot (GRUB)
+
<syntaxhighlight lang=nix>
mdadm --create /dev/md127 --metadata=0.90 --level=1 --raid-devices=4 /dev/sd[a,b,c,d]1
+
  services.samba = {
mkfs.ext4 -m 0 -L boot -j /dev/md127
+
    enable = true;
 +
    settings = {
 +
      global = {
 +
        "usershare path" = "/var/lib/samba/usershares";
 +
        "usershare max shares" = "100";
 +
        "usershare allow guests" = "yes";
 +
        "usershare owner only" = "no";
 +
      };
 +
    };
 +
    openFirewall = true;
 +
  };
 +
</syntaxhighlight>
  
mkdir /mnt/boot
+
Note that the usershare path *must* be "/var/lib/samba/usershares". This is the location ZFS expects. This value, I'm afraid, is hard-coded in ZFS. [https://github.com/openzfs/zfs/blob/zfs-2.3.99/lib/libshare/smb.h#L34]
mount /dev/md127 /mnt/boot
 
  
# Generate the NixOS configuration, as per the NixOS manual
+
Next, you must create the usershares directory and apply the permission samba expects.
nixos-generate-config --root /mnt
 
  
# Now edit the generated hardware config:
+
<syntaxhighlight lang=bash>
nano /mnt/etc/nixos/hardware-configuration.nix
+
sudo mkdir /var/lib/samba/usershares
 +
sudo chmod 1775 /var/lib/samba/usershares
 +
sudo chmod +t /var/lib/samba/usershares
 +
</syntaxhighlight>
  
## ---8<-------------------------8<---
+
Set the <code>sharesmb</code> property to any filesystems you wish to export this way.
# This is what you want:
+
<syntaxhighlight lang=bash>
 +
zfs set sharesmb=on rpool/myData
 +
</syntaxhighlight>
  
  fileSystems."/" =
+
If all things are going well, you will see a new share configuration file in the usershares directory.
    { device = "rpool/root/nixos";
 
      fsType = "zfs";
 
    };
 
  
  fileSystems."/home" =
+
Optional:
    { device = "rpool/home";
 
      fsType = "zfs";
 
    };
 
  
  fileSystems."/boot" =
+
See [[Samba|Samba]] for information about setting up avahi, wsdd, or managing usernames, passwords, and permissions.
    { device = "/dev/md127";
 
      fsType = "ext4";
 
    };
 
## ---8<-------------------------8<---
 
  
# configuration.nix needs an adjustment:
+
=== Mail notifications (ZFS Event Daemon) ===
nano /mnt/etc/nixos/configuration.nix
 
  
## ---8<-------------------------8<---
+
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]
# This is some more of what you want:
 
  
  boot.loader.grub.devices = [ "/dev/sda" "/dev/sdb" "/dev/sdc" "/dev/sdd" ];
+
==== Option A: Enable Mail Notification without Re-compliation ====
  boot.supportedFilesystems = [ "zfs" ];
 
## ---8<-------------------------8<---
 
  
# Ready to go!
+
First, we need to configure a mail transfer agent, the program that sends email:
nixos-install
+
<syntaxhighlight lang="nix">
 +
{
 +
  programs.msmtp = {
 +
    enable = true;
 +
    setSendmail = true;
 +
    defaults = {
 +
      aliases = "/etc/aliases";
 +
      port = 465;
 +
      tls_trust_file = "/etc/ssl/certs/ca-certificates.crt";
 +
      tls = "on";
 +
      auth = "login";
 +
      tls_starttls = "off";
 +
    };
 +
    accounts = {
 +
      default = {
 +
        host = "mail.example.com";
 +
        passwordeval = "cat /etc/emailpass.txt";
 +
        user = "user@example.com";
 +
        from = "user@example.com";
 +
      };
 +
    };
 +
  };
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Encrypted ZFS ==
+
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">
 +
tee -a /etc/aliases <<EOF
 +
root: user@example.com
 +
EOF
 +
</syntaxhighlight>
  
Native encryption is only available in the <code>zfsUnstable</code> package of NixOS, which was added in [https://github.com/NixOS/nixpkgs/pull/29426 PR-29426] in <code>unstable</code>
+
Finally, override default zed settings with a custom one:
and will be part of <code>18.03</code>. In older versions it is also possible to use full disk encryption by creating zfs top of cryptsetup.
+
<syntaxhighlight lang="nix">
 +
{
 +
  services.zfs.zed.settings = {
 +
    ZED_DEBUG_LOG = "/tmp/zed.debug.log";
 +
    ZED_EMAIL_ADDR = [ "root" ];
 +
    ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";
 +
    ZED_EMAIL_OPTS = "@ADDRESS@";
  
In the unstable channel at the moment it is necessary to set <code>boot.zfs.enableUnstable = true;</code> to get zfs version based on master branch as zfsStable does not yet have this feature.
+
    ZED_NOTIFY_INTERVAL_SECS = 3600;
 +
    ZED_NOTIFY_VERBOSE = true;
  
Assuming that a zpool named <code>zroot</code> has been already created as described.
+
    ZED_USE_ENCLOSURE_LEDS = true;
Encrypted datasets can be added on top as follow:
+
    ZED_SCRUB_AFTER_RESILVER = true;
 +
  };
 +
  # this option does not work; will return error
 +
  services.zfs.zed.enableMail = false;
 +
}
 +
</syntaxhighlight>
  
<syntaxHighlight lang=console>
+
You can now test this by performing a scrub
$ zfs create -o encryption=aes-256-gcm -o keyformat=passphrase -o mountpoint=none zroot/root
+
<syntaxhighlight lang="bash">
</syntaxHighlight>
+
zpool scrub $pool
 +
</syntaxhighlight>
  
Instead of encrypting just a dataset (and all its child datasets) you can also directly encrypt the whole pool upon creation:
+
==== Option B: Rebuild ZFS with Mail Support ====
<syntaxHighlight lang=console>
+
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.
$ zpool create -o ashift=12 -o altroot="/mnt" -O encryption=aes-256-gcm -O keyformat=passphrase zroot /dev/sdxy
 
</syntaxHighlight>
 
  
 +
An alternative solution that does not involve recompliation can be found above.
  
All child datasets will inherit the encryption.
+
The following override is needed as <code>zfs</code> is implicitly used in partition mounting:
Note that using grub to boot directly from zfs with encryption enabled might not work at the moment,
 
so a separate boot partition is required.
 
A full encrypted nixos installation on an UEFI system could look like this:
 
  
<syntaxHighlight lang=console>
+
<syntaxhighlight lang="nix">
$ zfs create -o mountpoint=legacy -o sync=disabled zroot/root/tmp
+
nixpkgs.config.packageOverrides = pkgs: {
$ zfs create -o mountpoint=legacy -o com.sun:auto-snapshot=true zroot/root/home
+
  zfsStable = pkgs.zfsStable.override { enableMail = true; };
$ zfs create -o mountpoint=legacy -o com.sun:auto-snapshot=true zroot/root/nixos
+
};
$ mount -t zfs zroot/root/nixos /mnt
+
</syntaxhighlight>
$ mkdir /mnt/{home,tmp,boot}
 
$ # assuming that /dev/sda1 is the boot partition
 
$ mkfs.vfat /dev/sda1
 
$ mount /dev/sda1 /mnt/boot/
 
$ mount -t zfs zroot/root/home /mnt/home/
 
$ mount -t zfs zroot/root/tmp /mnt/tmp/
 
$ nixos-generate-config  --root /mnt
 
</syntaxHighlight>
 
  
=== Unlock encrypted zfs via ssh on boot ===
+
A mail sender like [[msmtp]] or [[postfix]] is required.
  
In case you want unlock a machine remotely (after an update),
+
A minimal, testable ZED configuration example:
having a dropbear ssh service in initrd for the password prompt
 
is handy:
 
  
<syntaxHighlight lang=nix>
+
<syntaxhighlight lang="nix">
boot = {
+
services.zfs.zed.enableMail = true;
  initrd.network = {
+
services.zfs.zed.settings = {
    # This will use udhcp to get an ip address.
+
  ZED_EMAIL_ADDR = [ "root" ];
    # Make sure you have added the kernel module for your network driver to `boot.initrd.availableKernelModules`,
+
  ZED_NOTIFY_VERBOSE = true;
    # so your initrd can load it!
 
    # Static ip addresses might be configured using the ip argument in kernel command line:
 
    # https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
 
    enable = true;
 
    ssh = {
 
        enable = true;
 
        # To prevent ssh from freaking out because a different host key is used,
 
        # a different port for dropbear is useful (assuming the same host has also a normal sshd running)
 
        port = 2222;
 
        # dropbear uses key format different from openssh; can be generated by using:
 
        # $ nix-shell -p dropbear --command "dropbearkey -t ecdsa -f /tmp/initrd-ssh-key"
 
        hostECDSAKey = /run/keys/initrd-ssh-key;
 
        # public ssh key used for login
 
        authorizedKeys = [ "ssh-rsa AAAA..." ];
 
    };
 
    # this will automatically load the zfs password prompt on login
 
    # and kill the other prompt so boot can continue
 
    postCommands = ''
 
      echo "zfs load-key -a; killall zfs" >> /root/.profile
 
    '';
 
  };
 
 
};
 
};
</syntaxHighlight>
+
</syntaxhighlight>
* In order to use DHCP in the initrd, network manager must not be enabled and <code>networking.useDHCP = true;</code> must be set.
 
* If your network card isn't started, you'll need to add the according kernel module to the initrd as well, e.g. <code>boot.initrd.kernelModules = [ "r8169" ];</code>
 
 
 
=== Import and unlock multiple encrypted pools/dataset at boot ===
 
  
If you have not only one encrypted pool/dataset but multiple ones and you want to import and unlock them at boot, so that they can be automounted using the hardware-configuration.nix, you could just amend the <code>boot.initrd.network.postCommands</code> option.
+
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>
  
Unfortunately having an unlock key file stored in an encrypted zfs dataset cannot be used directly, so the pool must use <code>keyformat=password</code> and <code>keylocation=prompt</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>.
  
The following example follows the remote unlocking with dropbear, but imports another pool also and prompts for unlocking (either when at the machine itself or when logging in remotely:
+
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>
  
<syntaxHighlight lang=nix>
+
== Limitations ==
boot = {
 
  initrd.network = {
 
    enable = true;
 
    ssh = {
 
        enable = true;
 
        port = 2222;
 
        hostECDSAKey = /run/keys/initrd-ssh-key;
 
        authorizedKeys = [ "ssh-rsa AAAA..." ];
 
    };
 
    postCommands = ''
 
      zpool import tankXXX
 
      echo "zfs load-key -a; killall zfs" >> /root/.profile
 
    '';
 
  };
 
};
 
</syntaxHighlight>
 
  
When you login by SSH into dropbear or when you have physical access to the machine itself, you will be prompted to supply the unlocking password for your zroot and tankXXX pools.
+
=== ZFS support for newer kernel versions ===
  
==Regarding installation of NixOS to ZFS direct from installation media==
+
Newest kernels are often not supported by ZFS yet. If you are running a newer kernel which is not yet officially supported by ZFS, the ZFS module will refuse to evaluate and show up as ''broken''. It is thus recommended to use the latest LTS kernel version (usually also the NixOS default kernel) to ensure compatibility. If you understand the risks of running pre-release ZFS, using pre-release `pkgs.zfs_unstable` may support a newer kernel version, use it by setting `boot.zfs.package = pkgs.zfs_unstable`.
  
* Since [https://github.com/NixOS/nixpkgs/pull/51090 18.09] the installation iso comes with zfs by default again.
+
=== Missing support for SWAP on ZFS ===
* For older versions it is still possible to enable it in the existing ISO at runtime adding:
 
  
<syntaxHighlight lang=nix>
+
ZFS does not support swapfiles. Hibernation must be either disabled with <code><nowiki>boot.kernelParams = [ "nohibernate" ];</nowiki></code>, or be enabled with a separate, non-ZFS swap partition.
 
  boot.supportedFilesystems = [ "zfs" ];
 
}
 
</syntaxHighlight>
 
  
to the iso's configuration.nix followed by a <code>nixos-rebuild switch</code>
+
=== boot.zfs.devNodes in virtual machines ===
([https://discourse.nixos.org/t/install-report-from-new-user/1390/9 source])
 
  
 +
If you are running within a VM and NixOS fails to import the zpool on reboot, you may need to add <syntaxhighlight lang="nix" inline>boot.zfs.devNodes = "/dev/disk/by-path";</syntaxhighlight> or <syntaxhighlight lang="nix" inline>boot.zfs.devNodes = "/dev/disk/by-partuuid";</syntaxhighlight> to your configuration.nix file.
  
== Need more info? ==
+
=== Declarative mounting of ZFS datasets ===
  
Feel free to ask your questions on the NixOS mailing list or the IRC channel: http://nixos.org/development/
+
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>.
  
 
[[Category:Guide]]
 
[[Category:Guide]]

Latest revision as of 23:33, 4 April 2025

ZFS, also known as OpenZFS, is a modern filesystem which is well supported on NixOS.

Guides

Installation

Start from here in the NixOS manual [1] and under manual partitioning [2] do this instead:

Partition your disk with an boot and an zfs partition with your favorite partition tool.

Eg. 1G for boot partion, rest for zfs.

Example output from fdisk:

fdisk /dev/nvme0n1
Command (m for help): p

Disk /dev/nvme0n1: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
...
Device           Start        End    Sectors   Size Type
/dev/nvme0n1p1    2048    2099199    2097152     1G EFI System
/dev/nvme0n1p2 2099200 1953523711 1951424512 930.5G Linux filesystem

Note: Remember to set boot partition (first partition) to "EFI System" type

Make 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.

zpool create -O encryption=on -O keyformat=passphrase -O keylocation=prompt -O compression=on -O mountpoint=none -O xattr=sa -O acltype=posixacl -o ashift=12 zpool /dev/nvme0n1p2

zfs create -o mountpoint=legacy zpool/root
zfs create -o mountpoint=legacy zpool/nix
zfs create -o mountpoint=legacy zpool/var
zfs create -o mountpoint=legacy zpool/home

mkdir /mnt/root
mount -t zfs zpool/root /mnt
mkdir /mnt/nix /mnt/var /mnt/home

mount -t zfs zpool/nix /mnt/nix
mount -t zfs zpool/var /mnt/var
mount -t zfs zpool/home /mnt/home

Output from zpool status:

zpool status
  pool: zpool
 state: ONLINE
...
config:

	NAME                               STATE     READ WRITE CKSUM
	zpool                              ONLINE       0     0     0
	  nvme-eui.0025384b21406566-part2  ONLINE       0     0     0

Make fat filesystem on boot partition

mkfs.fat -F 32 -n boot /dev/nvme0n1p1

Installation:

Install: [3]

Jump to "2. UEFI systems"

mkdir -p /mnt/boot
mount /dev/disk/by-label/boot /mnt/boot

Jump to "4." ... /mnt/etc/nixos/configuration.nix ...

Continue from here and add this boot loader and filesystems config to your configuration.nix:

# Boot loader config for configuration.nix:
  boot.loader.grub = {
    enable = true;
    zfsSupport = true;
    efiSupport = true;
    efiInstallAsRemovable = true;
    mirroredBoots = [
      { devices = [ "nodev"]; path = "/boot"; }
    ];
  };

  fileSystems."/" =
    { device = "zpool/root";
      fsType = "zfs";
    };

  fileSystems."/nix" =
    { device = "zpool/nix";
      fsType = "zfs";
    };

  fileSystems."/var" =
    { device = "zpool/var";
      fsType = "zfs";
    };

  fileSystems."/home" =
    { device = "zpool/home";
      fsType = "zfs";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/2A11-F4EF";
      fsType = "vfat";
    };

  swapDevices = [ ];

Note: Remove / comment out boot.loader.systemd-boot.enable = true; config if added bynixos-generate-config

Installation (alternative)

Alternative guide for a NixOS installation with ZFS is maintained at OpenZFS Documentation (Getting Started for NixOS)

Warning: It 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 guide's support channels.

Tips

Importing pools at boot

If you create a zpool, it will not be imported on the next boot unless you either add the zpool name to boot.zfs.extraPools:

## In /etc/nixos/configuration.nix:
boot.zfs.extraPools = [ "zpool_name" ];

or if you are using legacy mountpoints, add a fileSystems entry and NixOS will automatically detect that the pool needs to be imported:

## In /etc/nixos/configuration.nix:
fileSystems."/mount/point" = {
  device = "zpool_name";
  fsType = "zfs";
};

Mounting pools at boot

zfs-mount service is enabled by default on NixOS 22.05.

To automatically mount a dataset at boot, you only need to set canmount=on and mountpoint=/mount/point on the respective datasets.

Reservations

ZFS's performance will deteriorate significantly when more than 80% of the available space is used - to avoid this, reserve disk space beforehand.

To reserve space, create a new unused dataset that gets a guaranteed size of e.g. 10GB.

zfs create -o refreservation=10G -o mountpoint=none zroot/reserved

Automatic scrubbing

Regular scrubbing of ZFS pools is recommended and can be enabled in your NixOS configuration via:

services.zfs.autoScrub.enable = true;

By default, the service will scrub pools once a week.

Automatic snapshots

You can setup automatic snapshots by enabling the zfs.autoSnapshot service:

services.zfs.autoSnapshot.enable = true;.

Alternatively, see services.sanoid.

Trimming

Trimming is enabled by default - it's controlled by this option, if you'd like to disable it:

services.zfs.trim.enable

Note that this is different from the autotrim pool property - for further information, see the zpool-trim and zpoolprops man pages.

Tuning Adaptive Replacement Cache size

To change the maximum size of the ARC to (for example) 12 GB, add this to your NixOS configuration:

boot.kernelParams = [ "zfs.zfs_arc_max=12884901888" ];

Tuning other parameters

To tune other attributes of ARC, L2ARC or of ZFS itself via runtime modprobe config, add this to your NixOS configuration (keys and values are examples only!):

    boot.extraModprobeConfig = ''
      options zfs l2arc_noprefetch=0 l2arc_write_boost=33554432 l2arc_write_max=16777216 zfs_arc_max=2147483648
    '';

You can confirm whether any specified configuration/tuning got applied via commands like arc_summary and arcstat -a -s " ".

Remote unlocking

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

In case you want unlock a machine remotely (after an update), having an ssh service in initrd for the password prompt is handy:

boot = {
  initrd.network = {
    # This will use udhcp to get an ip address.
    # Make sure you have added the kernel module for your network driver to `boot.initrd.availableKernelModules`, 
    # so your initrd can load it!
    # Static ip addresses might be configured using the ip argument in kernel command line:
    # https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
    enable = true;
    ssh = {
      enable = true;
      # To prevent ssh clients from freaking out because a different host key is used,
      # a different port for ssh is useful (assuming the same host has also a regular sshd running)
      port = 2222; 
      # hostKeys paths must be unquoted strings, otherwise you'll run into issues with boot.initrd.secrets
      # the keys are copied to initrd from the path specified; multiple keys can be set
      # you can generate any number of host keys using 
      # `ssh-keygen -t ed25519 -N "" -f /path/to/ssh_host_ed25519_key`
      hostKeys = [ /path/to/ssh_host_ed25519_key ];
      # public ssh key used for login
      authorizedKeys = [ "ssh-rsa AAAA..." ];
    };
  };
};
  • In order to use DHCP in the initrd, network manager must not be enabled and networking.useDHCP = true; 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.
    boot.kernelModules = [ "r8169" ];
    boot.initrd.kernelModules = [ "r8169" ];
    

After that you can unlock your datasets using the following ssh command:

ssh -p 2222 root@host "zpool import -a; zfs load-key -a && killall zfs"

Alternatively you could also add the commands as postCommands to your configuration.nix, then you just have to ssh into the initrd:

boot = {
  initrd.network = {
    postCommands = ''
    # Import all pools
    zpool import -a
    # Or import selected pools
    zpool import pool2
    zpool import pool3
    zpool import pool4
    # Add the load-key command to the .profile
    echo "zfs load-key -a; killall zfs" >> /root/.profile
    '';
  };
};

After that you can unlock your datasets using the following ssh command:

ssh -p 2222 root@host

NFS shares

With the sharenfs property, ZFS has build-in support for generating /etc/exports.d/zfs.exports file, which in turn is processed by NFS service automatically.

Warning: If you are intending on defining an IPv6 subnet as part of your sharenfs rule, as of ZFS 2.0.6 (2021-09-23) please note that due to a bug in openzfs your rule will not correctly apply, and may result in a security vulnerability (CVE-2013-20001). A fix has been implemented in the next yet-to-be-released upstream version - openzfs/zfs#11939

To enable NFS share on a dataset, only two steps are needed:

First, enable NFS service:

services.nfs.server.enable = true;

Only this line is needed. Configure firewall if necessary, as described in NFS article.

Then, set sharenfs property:

zfs set sharenfs="ro=192.168.1.0/24,all_squash,anonuid=70,anongid=70" rpool/myData

For more options, see man 5 exports.

Samba Shares

With the sharesmb property, ZFS can generate the necessary configuration exporting zfs filesystems over SMB. This functionality requires you to run the Samba service with support for the "usershares" feature configured. Possible configuration options for the usershares feature can be viewed in smb.conf(5)

Enable the samba service configure it to enable usershare support.

  services.samba = {
    enable = true;
    settings = {
      global = {
        "usershare path" = "/var/lib/samba/usershares";
        "usershare max shares" = "100";
        "usershare allow guests" = "yes";
        "usershare owner only" = "no";
      };
    };
    openFirewall = true;
  };

Note that the usershare path *must* be "/var/lib/samba/usershares". This is the location ZFS expects. This value, I'm afraid, is hard-coded in ZFS. [4]

Next, you must create the usershares directory and apply the permission samba expects.

sudo mkdir /var/lib/samba/usershares
sudo chmod 1775 /var/lib/samba/usershares
sudo chmod +t /var/lib/samba/usershares

Set the sharesmb property to any filesystems you wish to export this way.

zfs set sharesmb=on rpool/myData

If all things are going well, you will see a new share configuration file in the usershares directory.

Optional:

See Samba for information about setting up avahi, wsdd, or managing usernames, passwords, and permissions.

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. zed options

Option A: Enable Mail Notification without Re-compliation

First, we need to configure a mail transfer agent, the program that sends email:

{
  programs.msmtp = {
    enable = true;
    setSendmail = true;
    defaults = {
      aliases = "/etc/aliases";
      port = 465;
      tls_trust_file = "/etc/ssl/certs/ca-certificates.crt";
      tls = "on";
      auth = "login";
      tls_starttls = "off";
    };
    accounts = {
      default = {
        host = "mail.example.com";
        passwordeval = "cat /etc/emailpass.txt";
        user = "user@example.com";
        from = "user@example.com";
      };
    };
  };
}

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.

tee -a /etc/aliases <<EOF
root: user@example.com
EOF

Finally, override default zed settings with a custom one:

{
  services.zfs.zed.settings = {
    ZED_DEBUG_LOG = "/tmp/zed.debug.log";
    ZED_EMAIL_ADDR = [ "root" ];
    ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";
    ZED_EMAIL_OPTS = "@ADDRESS@";

    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;
}

You can now test this by performing a scrub

zpool scrub $pool

Option B: Rebuild ZFS with Mail Support

The zfs 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 zfs is implicitly used in partition mounting:

nixpkgs.config.packageOverrides = pkgs: {
  zfsStable = pkgs.zfsStable.override { enableMail = true; };
};

A mail sender like msmtp or postfix is required.

A minimal, testable ZED configuration example:

services.zfs.zed.enableMail = true;
services.zfs.zed.settings = {
  ZED_EMAIL_ADDR = [ "root" ];
  ZED_NOTIFY_VERBOSE = true;
};

Above, ZED_EMAIL_ADDR is set to root, which most people will have an alias for in their mailer. You can change it to directly mail you: ZED_EMAIL_ADDR = [ "you@example.com" ];

ZED pulls in mailutils and runs mail by default, but you can override it with ZED_EMAIL_PROG. If using msmtp, you may need ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";.

You can customize the mail command with ZED_EMAIL_OPTS. For example, if your upstream mail server requires a certain FROM address: ZED_EMAIL_OPTS = "-r 'noreply@example.com' -s '@SUBJECT@' @ADDRESS@";

Limitations

ZFS support for newer kernel versions

Newest kernels are often not supported by ZFS yet. If you are running a newer kernel which is not yet officially supported by ZFS, the ZFS module will refuse to evaluate and show up as broken. It is thus recommended to use the latest LTS kernel version (usually also the NixOS default kernel) to ensure compatibility. If you understand the risks of running pre-release ZFS, using pre-release `pkgs.zfs_unstable` may support a newer kernel version, use it by setting `boot.zfs.package = pkgs.zfs_unstable`.

Missing support for SWAP on ZFS

ZFS does not support swapfiles. Hibernation must be either disabled with boot.kernelParams = [ "nohibernate" ];, or be enabled with a separate, non-ZFS swap partition.

boot.zfs.devNodes in virtual machines

If you are running within a VM and NixOS fails to import the zpool on reboot, you may need to add boot.zfs.devNodes = "/dev/disk/by-path"; or boot.zfs.devNodes = "/dev/disk/by-partuuid"; to your configuration.nix file.

Declarative mounting of ZFS datasets

When using legacy mountpoints (created with egzfs create -o mountpoint=legacy) mountpoints must be specified with fileSystems."/mount/point" = {};. 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 systemd.services.zfs-mount.enable = false;.