with apologies

Resizing swap partition on NixOS

Richard Mortier · 2 min read · August 13, 2025 · #nixos #linux #tech

I 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:

$ nix profile install nixpkgs#gparted.out
...
$ gparted
localuser:root being added to access control list
Error executing command as another user: No authentication agent found.
localuser:root being removed from access control list
$ sudo gparted
Invalid MIT-MAGIC-COOKIE-1 key

(gpartedbin:360533): Gtk-WARNING **: 19:19:25.626: cannot open display: :0

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.

  1. Find your partition names using lvscan:

    $ sudo lvscan
      ACTIVE            '/dev/vg0/home' [<729.61 GiB] inherit
      ACTIVE            '/dev/vg0/nixos' [94.00 GiB] inherit
      ACTIVE            '/dev/vg0/nixos-swap' [16.00 GiB] inherit
    

    Normally I would then just use lvreduce – but that fails on the swap partition:

    $ sudo lvreduce -r --size 12GiB /dev/vg0/nixos-swap
      File system swap found on vg0/nixos-swap.
      File system size (16.00 GiB) is larger than the requested size (12.00 GiB).
      File system reduce is required and not supported (swap).
    

    So instead…

  2. Delete the swap partition and recreate to the desired size (16GB in my case):

    sudo lvremove /dev/vg0/nixos-swap
    sudo lvcreate -L 16G -n nixos-swap vg0
    sudo mkswap /dev/vg0/nixos-swap
    sudo swapon -v /dev/vg0/nixos-swap
    
  3. Reallocate the now available space to other partitions:

    # `/home` is easy: extend logical volume and resize the filesystem at in one go:
    $ sudo lvextend -r -L +16G /dev/vg0/home
    # however, can't resize the root filesystem as part of the `lvextend` because it's read-only.
    # instead, find the relevant filesystem:
    $ df -Th /
    Filesystem                                             Type  Size  Used Avail Use% Mounted on
    /dev/disk/by-uuid/8dd0d118-375f-4e8b-84bc-1a05b68889b7 ext4   92G   66G   22G  76% /
    # ...and then manually resize it after extending the logical volume:
    $ sudo lvextend -L +12G /dev/vg0/nixos
    $ sudo resize2fs /dev/disk/by-uuid/8dd0d118-375f-4e8b-84bc-1a05b68889b7
    

And that’s it. Swap partition is reduced in size, and the space is reallocated to the other two partitions, /home and /.