Systemd stops network before umounting NFS shares

Hi. I have a number of auto-mounted NFS shares defined like the following:

  # explicitly enable rpcbind dependency
  services.rpcbind.enable = true;

  fileSystems."/home/media/data/bin" = lib.mkIf variables.role.htpc {
    device = "nas:/data/videos";
    fsType = "nfs4";
    options = [ "sec=sys" "x-systemd.automount" "x-systemd.mount-timeout=30" "x-systemd.device-timeout=30" "_netdev" "nofail" ];
  };

This works perfectly fine on startup, but it hangs on shutdown, usually for around 5 minutes but sometimes perpetually. I can see in the shutdown messages that it shuts down the network interface, and then many steps later it appears to start unmounting the NFS shares.

Here’s a picture for reference:

I say appears because I see that it says unset vs. stopping, and there are lines toward the top of the screen that state that it stopped Remote File Systems and NFS client services. I’m not extremely familiar with systemd, to be honest, so I don’t know the significance of unset vs. stop here.

However, it always hangs in this section, and if I manually stop everything and unmount the NFS filesystems in advance, it never hangs, so I’m confident the issue is NFS related. I didn’t have this issue when I initially built my systems on NixOS 23.11, but it began happening with 24.05 and persists with 24.11.

Anyone know of a way to force systemd to unmount these NFS filesystems before stopping the network service? some way to set a dependency that those automount services must be stopped or unset before network can be stopped? or is there maybe a better way to define the NFS shares than I showed above that would avoid this problem?

Have been tinkering with this for a while but not have much luck. Any pointers would be appreciated.

So, the thing to understand about x-systemd.automount is that it actually creates two different units. It creates foo.mount but also foo.automount. The automount unit creates a fake mount at the mountpoint with autofs4, and whenever that mountpoint is accessed, it triggers starting the mount unit. What you see at the end of your photo is the automount unit being stopped (each unit type chooses its own cosmetic term for “Stopped” and automount’s term is “Unset”). The actual mount units, and therefore the NFS mounts themselves, have already been stopped / unmounted, presumably above the lines we can see in the image.

You might be able to check this by looking at the logs from the previous boot with journalctl -b -1 -e. I’m not sure how this would end up being ordered compared to the stopping of systemd-journald or the unmounting of /var/log, but with any luck you’ll be able to see separate Unmounting ... logs for the mountpoints at an earlier time than the Unset automount ... logs.