How to add a swap after NixOs installation

Hi,

I’ve just installed NixOs 23.11 on a old laptop with only 6Gb of RAM (btw I still didn’t a solution for my sound card) and just realize that with only Thunderbird and Firefox it get exhausted at the speed of light ! :astonished:

The problem is that I didn’t specify any swap partition when installing NixOs (I just ran the default option) so I got a / ext4 partition which take all the space.

How can I “fix” that ?

Usually, I would use a tool like gparted to reduce the size of my partition, and then create a swap, but I assume it doesn’t work like that on NixOs ?

My laptop did already freeze a couple of time because of missing memory space (I assume), so I would be glad to have a solution !

Thanks in advance for your help !
Andréas

I think the easiest thing for you to do now would be to create a swapfile:

{
  swapDevices = [{
    device = "/swapfile";
    size = 16 * 1024; # 16GB
  }];
}

Just switch configurations and it should be created automatically.

Thanks for your reply @lelgenio .
Will this configuration create the swapfile and automatically resize my main partition ?
I doubt that, or should I manually create the partition (with gparted for example) as I suggested above ?

Here you can read about swap partition and swap file.

It will create the swap-file automatically. It’s a regular file, there’s no partitioning involved, you won’t need to do anything else.

NixOs is a pathway to many abilities some consider… unnatural.

1 Like

I can confirm this works just fine.
Used it myself on all my systems a few weeks ago.

Another nice thing to do on devices with little RAM is ZRAM swap btw.
It uses a part of your RAM for swap but compresses it before storing.

https://search.nixos.org/options?channel=23.11&show=zramSwap.enable&from=0&size=50&sort=relevance&type=packages&query=Zram

The Linux Matters podcast talked about it as well:

Ok nice, I didn’t know the swap was not a partition…

:rofl: You’re right, it’s quite an astonish Os ! To be honest I don’t understand why all the distribution out there don’t use a similar system for managing package and dependencies. It makes such sense !

1 Like

It can be but doesn’t have to.