Resizing swap partition on NixOS
Richard Mortier · 2 min read · August 13, 2025 · #nixos #linux #techI recently decided that I had over-egged the pudding and allocated too much space to my swap partition (mounted as /tmp
) under NixOS. At the same time, it had got to the point where I was bouncing off the ceiling a little with the other two partitions, /home
and /
(wherein lives the Nix store).
I tried gparted
first for a handy GUI but got nowhere due to (I think) Wayland:
()
So the obvious solution was to reduce the size of the swap partition and increase the other two. However, doing this for my setup – a LUKS-encrypted device partitioned using Logical Volume Manager (LVM) – turned out to be a little less obvious than it might have been, and searching the interwebs turned up several not quite correct answers, I thought I’d make a note here.
NB. Don’t pay too much attention to the sizes of partitions below: I cribbed these notes from shell history but re-ran some commands to get the output as I’d already closed that shell so they may not be entirely coherent. The commands should be right though.
Find your partition names using
lvscan
:Normally I would then just use
lvreduce
– but that fails on the swap partition:) ) )
So instead…
Delete the swap partition and recreate to the desired size (16GB in my case):
Reallocate the now available space to other partitions:
# `/home` is easy: extend logical volume and resize the filesystem at in one go: # however, can't resize the root filesystem as part of the `lvextend` because it's read-only. # instead, find the relevant filesystem: # ...and then manually resize it after extending the logical volume:
And that’s it. Swap partition is reduced in size, and the space is reallocated to the other two partitions, /home
and /
.