# How to add a swap after NixOs installation

**URL:** https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742
**Category:** Help
**Created:** [March 18, 2024, 1:53pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742 "2024-03-18T13:53:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![AndreasL](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@AndreasL](https://discourse.nixos.org/u/AndreasL)
#### Post date: [March 18, 2024, 1:53pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/1 "2024-03-18T13:53:23Z")

</div>

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](https://discourse.nixos.org/t/no-sound-on-speaker-or-hdmi-on-dell-latitude-e5620/41665)) and just realize that with only Thunderbird and Firefox it get exhausted at the speed of light ! 😲

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

---

<div class="post-metadata">

### Author: ![lelgenio](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lelgenio/32/15747_2.png) [@lelgenio](https://discourse.nixos.org/u/lelgenio)
#### Post date: [March 18, 2024, 2:37pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/2 "2024-03-18T14:37:25Z")

</div>

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

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

```

Just switch configurations and it should be created automatically.

---

<div class="post-metadata">

### Author: ![AndreasL](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@AndreasL](https://discourse.nixos.org/u/AndreasL)
#### Post date: [March 18, 2024, 3:10pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/3 "2024-03-18T15:10:58Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![anon54679641](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anon54679641](https://discourse.nixos.org/u/anon54679641)
#### Post date: [March 18, 2024, 3:56pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/4 "2024-03-18T15:56:21Z")

</div>

[Here](https://wiki.archlinux.org/title/Swap#) you can read about swap partition and swap file.

---

<div class="post-metadata">

### Author: ![lelgenio](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lelgenio/32/15747_2.png) [@lelgenio](https://discourse.nixos.org/u/lelgenio)
#### Post date: [March 18, 2024, 5:13pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/5 "2024-03-18T17:13:25Z")

</div>

> [@AndreasL](#):
>
> 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 ?

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.

> [@AndreasL](#):
>
> I doubt that

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

---

<div class="post-metadata">

### Author: ![Nebucatnetzer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nebucatnetzer/32/5770_2.png) [@Nebucatnetzer](https://discourse.nixos.org/u/Nebucatnetzer)
#### Post date: [March 18, 2024, 5:52pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/6 "2024-03-18T17:52:15Z")

</div>

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](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:

> **[A Mini Swap Adventure - Linux Matters](https://pca.st/9ldvio54)**
>
> Join 3 experienced Open Source professionals as they discuss the impact Linux has in their daily lives. Upbeat family-friendly banter, conversation and discussion for Linux enthusiasts and casual observers of all ages. A new episode every two weeks...

---

<div class="post-metadata">

### Author: ![AndreasL](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@AndreasL](https://discourse.nixos.org/u/AndreasL)
#### Post date: [March 18, 2024, 10:36pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/7 "2024-03-18T22:36:24Z")

</div>

> [@lelgenio](#):
>
> 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.

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

> [@lelgenio](#):
>
> NixOs is a pathway to many abilities some consider… unnatural.

🤣 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 !

---

<div class="post-metadata">

### Author: ![Nebucatnetzer](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nebucatnetzer/32/5770_2.png) [@Nebucatnetzer](https://discourse.nixos.org/u/Nebucatnetzer)
#### Post date: [March 19, 2024, 4:26pm UTC](https://discourse.nixos.org/t/how-to-add-a-swap-after-nixos-installation/41742/8 "2024-03-19T16:26:38Z")

</div>

> [@AndreasL](#):
>
> Ok nice, I didn’t know the swap was not a partition…

It can be but doesn’t have to.
