Using nixos-generate-config with zfs

I’m running a NixOS system with a ZFS data array (RAIDZ1).

I recently re-ran nixos-generate-config which should be safe - and it seemed so, but my ZFS data array was now listed as a filesystem in my /etc/nixos/hardware-configuration.nixfile

This seemed fine, until I tried to reboot the machine. It got hung up

Dec 10 14:03:29 lead zfs-import-dpool-start\[1005\]: importing ZFS pool “dpool”…Successfully imported dpool
Dec 10 14:03:29 lead systemd\[1\]: Finished Import ZFS pool “dpool”.
Dec 10 14:03:29 lead systemd\[1\]: Reached target ZFS pool import target.
Dec 10 14:03:29 lead systemd\[1\]: Mounting /data…
Dec 10 14:03:29 lead systemd\[1\]: Starting Mount ZFS filesystems…
Dec 10 14:03:29 lead mount\[1223\]: filesystem ‘dpool’ cannot be mounted using ‘mount’.
Dec 10 14:03:29 lead mount\[1223\]: Use ‘zfs set mountpoint=legacy’ or ‘zfs mount dpool’.
Dec 10 14:03:29 lead mount\[1223\]: See zfs(8) for more information.

This is because in the /etc/nixos/hardware-configuration.nix file I have

  fileSystems."/data" =
    { device = "dpool";
      fsType = "zfs";
    };

  fileSystems."/data/backup" =
    { device = "dpool/backup";
      fsType = "zfs";
    };

By commenting this section out of my hardware-configuration.nix – and doing a nixos-rebuild-switch followed by a reboot.. I’m back just fine

Is this a bug? In my /etc/nixos/configuration.nix file I have

  # Load up the zfs filesystem
  boot.zfs.extraPools = [ "dpool" ];

which causes my zfs filesystem to get mounted..

It feels a bit like nixos-generate-config should ignore zfs filesystems.. or I’m doing something wrong.

That command is an easy footgun and all outputs must be checked when you use it. Presumably you’re using ZFS’ automount which means you won’t be adding the results to your config, which is reasonable. However the logic isn’t that advanced, it just scans and add things. It’s a hella of an output if you ran that on a system with impermanence, as the litany of bind mounts would be inappropriately added as well.

1 Like

Whether it should or not depends, among other things, on whether you’ve got them configured (in ZFS) as legacy mounts or not. I feel like it’s an insoluble problem because there are roughly equal pros and cons of having NixOS or ZFS configure the mounting of your datasets.

Personally, I find that a partial solution is to put all my mounting into my main configuration.nix file, and delete ALL mounting from hardware-configuration.nix by hand every time I re-run nixos-generate-config. That’s clearly not ideal, but it has the huge advantage of reminding me of what I’ve got configured each time. And it’s simple.

1 Like

I’ve tagged @Michael-C-Buckley’s post as the solution, this runs counter to the other ‘advice’ I’ve seen posted saying that it is safe to run nix-generate-config multiple times and just “trust it”. I will say that I particularly liked the “footgun” comment.

I will thank @Mapybara for their comment on this post as well, which appears to share the same guidance but with additional details on how they manage using the command.

In summary: Yes - it is “safe” to run nix-generate-config multiple times, it will not over-write your /etc/nixos/configuration.nix file if it detects one. However, you do need to be aware that it may capture elements of your system (say mounted zfs file systems that are not legacy mounts) which you do not want included.

1 Like

Trust, but verify! Especially inherently impure boundaries like physical hardware and network/filesystems.

1 Like