Swapfile start fails on reboot

NixOS v23.05

When I reboot my server I am getting a swapfile start failure during boot. I tried both encrypted and unencrypted and get the same error message. If I run systemct restart mkswap-var-cache-swapfile manually, it succeeds and swap is activated. Journal for the service:

Nov 15 10:50:59 vps mkswap-var-cache-swapfile-start[832]: Attaching loopback device failed (loop device with autoclear flag is required>
Nov 15 10:50:59 vps systemd[1]: mkswap-var-cache-swapfile.service: Main process exited, code=exited, status=1/FAILURE
Nov 15 10:50:59 vps systemd[1]: mkswap-var-cache-swapfile.service: Failed with result 'exit-code'.
Nov 15 10:50:59 vps systemd[1]: Failed to start Initialisation of swap device /var/cache/swapfile.

Swap config:

swapDevices = [ {
    device = "/var/cache/swapfile";
    size = 4*1024;
    randomEncryption.enable = true; 
  } ];

Filesystem is ext4.

Any ideas? Thanks!

Well, I seem to have solved this based information here. Added this to the configuration.nix:

systemd.services.mkswap-var-cache-swapfile.unitConfig = {
  Requires = "systemd-tmpfiles-setup-dev.service";
  After = "systemd-tmpfiles-setup-dev.service";
};

Not sure if this a bug worth reporting or an oddity of running on a cloud server (Hetzner)?

I would very much like to know why that’s necessary. If the problem can be reproduced, it’d be worth opening an issue.

Here is my basic config. Didn’t include the various services cause I doubt they’re relevant. I installed on a Hetzner vps using nixos-infect.

My other nix machines either use zram or a swap partition. This is the only one with a swapfile. Maybe I’ll give it a try in a VM. There were no other similar issues that I could find anywhere!

This seems to be fixed in 23.11. I no longer need the extra snippet.