Code change:
# Create missing swapfiles. systemd.services = let createSwapDevice = sw: <del> assert sw.device != ""; assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-uuid"  sw.device); assert !(sw.randomEncryption.enable && lib.hasPrefix "/dev/disk/by-label" sw.device); </del> let realDevice' = escapeSystemdPath sw.realDevice; in nameValuePair "mkswap-${sw.deviceName}" { description = "Initialisation of swap device ${sw.device}"; wantedBy = [ "${realDevice'}.swap" ]; before = [ "${realDevice'}.swap" ]; <del> path = [ pkgs.util-linux ] ++ optional sw.randomEncryption.enable pkgs.cryptsetup; </del> <ins> path = [ pkgs.util-linux pkgs.e2fsprogs ] ++ optional sw.randomEncryption.enable pkgs.cryptsetup; environment.DEVICE = sw.device; </ins> script = '' ${optionalString (sw.size != null) '' <del> currentSize=$(( $(stat -c "%s" "${sw.device}" 2>/dev/null || echo 0) / 1024 / 1024 )) if [ "${toString sw.size}" != "$currentSize" ]; then dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size} </del> <ins> currentSize=$(( $(stat -c "%s" "$DEVICE" 2>/dev/null || echo 0) / 1024 / 1024 )) if [[ ! -b "$DEVICE" && "${toString sw.size}" != "$currentSize" ]]; then # Disable CoW for CoW based filesystems like BTRFS. truncate --size 0 "$DEVICE" chattr +C "$DEVICE" 2>/dev/null || true dd if=/dev/zero of="$DEVICE" bs=1M count=${toString sw.size} </ins> chmod 0600 ${sw.device} ${optionalString (!sw.randomEncryption.enable) "mkswap ${sw.realDevice}"} fi

Related discussion:
text: When using to create and activate a swapfile on btrfs, it fails with messages like this: The kernel says: I am not very familiar with Nix, but after reading the corresponding and , it seems that creating a swapfile on btrfs requires disabling copy-on-write for that file (via ) as well. However, doesn't do it currently. BTW, manually creating the swapfile and letting NixOS activating it works fine for me. to ( is supposed to be mounted as btrfs). . Swapfile is created and activated without any issue. There is a workaround on NixOS Discourse: system: host os: multi-user?: sandbox: version: channels(root): nixpkgs:
I have the same issue. I already tried disabling copy-on-write for the partition but it still fails: I also still get the warning like even though I the attribute to my mount options (afterwards). My current swap configuration can be found .
Mounting with attribute won't work because: [^1]:
I came up with this yesterday, which seems to work (or at least you can follow the general pattern): I have as a separate subvolume, nothing else on it. Once you've run this to create the swapfile, it seems to mount and function properly from there on out. But yes, it would be nice if this were taken care of automatically. EDIT: Fix link
brilliant, that helped me solve my issue. While your script didn't work out for me, I was able to adjust it so it did. I noticed the following warning when running your script: So, I searched for this and found the following note on ArchWiki: — Replacing with made the warning disappear for me but running still failed with . Then I compared the script to what I've used in and noticed that I used instead of to create the empty swapfile. The full script I ended up with:
Thanks for the update. I originally used , but switched after reading that should be much faster (and have to admit I can't remember if I tested that change yet -- thank you for the feedback). I've switched it back to .