2026-07-30 18:32:07 -04:00
2026-07-30 18:13:19 -04:00
2026-07-30 18:32:07 -04:00
2026-07-30 18:13:19 -04:00

zfs-containers

Use ZFS datasets in NixOS containers

zfs zone allows you to attach ZFS datasets to user namespaces. Specific datasets on the host can be used within a systemd-nspawn container, including mounting, creating sub-datasets, and changing properties (with some limitations).

Usage

Add to flake inputs

inputs.zfs-containers.url = "git+https://gitea.mregirouard.com/nix/zfs-containers.git";

Add module

nixosConfigurations.my-system = nixpkgs.lib.nixosSystem {
  modules = [
    inputs.zfs-containers.nixosModules.default

    # ...
  ];

  # ...
};

Create containers

containers.my-container = {
  zfs_datasets = [
    "tank/data"
  ];

  # ...
};

Use ZFS datasets

$ sudo machinectl shell my-container

[root@my-container:~]# zfs list
NAME      USED  AVAIL  REFER  MOUNTPOINT
tank      180G   820G    96K  none
tank/data  96K   820G    96K  none

[root@my-container:~]# zfs create tank/data/files

# ...

Systemd Target

ZFS datasets will not immediately become available on container startup. A systemd target zfs will be created in the container that will become active when all datasets have been attached to the container. For services that depend on dataset availability, set Requires = zfs.target in your unit file.

Relevant Documentation

NixOS Containers
ZFS Zone

Description
NixOS module to allow ZFS dataset use in systemd-nspawn containers
Readme 30 KiB
Languages
Nix 100%