Hacker News new | ask | show | jobs
by SkyMarshal 1032 days ago
There’s an even better setup than Erase your Darlings. Instead of putting / on a zfs pool and erasing and rewriting it every reboot, just put tmpfs on / instead.

With / all in memory it automatically gets wiped and recreated every reboot, without needing to actively erase a disk, and thus with much less drive wear (depending on how frequently you reboot). It’s also faster on some things when loading from RAM instead of the disk. And it’s overall a cleaner, simpler setup.

https://elis.nu/blog/2020/05/nixos-tmpfs-as-root/

You can also put tmpfs on home as well, using Impermanence and Home Manager to persist things like ~/.config and whatever other files or folders need to persist between reboots:

https://elis.nu/blog/2020/06/nixos-tmpfs-as-home/

tmpfs on / is enabled by NixOS’s unique design, in which it keeps the entire system in /nix/store and then softlinks all the paths into their appropriate place in /. With tmpfs on /, NixOS automatically recreates those softlinks in tmpfs on reboot. Very little setup effort is required to make this work.

4 comments

I was aware of the possibility of just using tmpfs, but I went with ZFS for / anyway for mostly one reason: I can erase root on every boot, but still keep snapshots of the last few boots. That means, that if I mess up some configuration and I fail to persist some important data, I can still go back and recover it if I need to.
I'm not sure that's necessary for / at least, though /home is a different matter.

Everything that needs to be persisted for / is done so either in configuration.nix (or equivalent flake), or using Impermanence. Once you've persisted things in one of those, it will remain so for all future derivations.

You can also create separate ZFS pools for things like LXC/LXD that need to persist between boots, but don't naturally go in / or /home.

So far I haven't run into a case where I forgot to add something to my impermanence config, so you may be right: it might not be necessary. However, just knowing that I could fix it if I forget something (for example when setting up a new service) somewhere down the line just gives me peace of mind.
I don’t think the point about disk wear is true. ZFS is a block-based CoW filesystem already, so the “erase” is actually something more like “make a new metadata entry that points to an earlier state snapshot”. Plus, I’d expect normal disk usage (ie. chrome disk cache) probably far outweighs any disk wear you’d get from the (relatively) small size of files you get written into /.

Unless you’re working on a server with a ton of ram, I also think using tmpfs is more likely to shoot yourself in the foot with excess memory pressure. I don’t know of a way for the kernel to free memory if you write a huge file to the tmpfs partition by mistake, unless you use swap, and then you have the problems that come with that.

Tmpfs might be faster though!

> tmpfs is more likely to shoot yourself in the foot with excess memory pressure

It defaults to 50% of physical RAM, but you can set the size option to whatever you want: https://www.kernel.org/doc/html/latest/filesystems/tmpfs.htm...

(So yes, something to consider, but totally possible to mitigate)

TIL!
You do need enough RAM for this, though RAM is relatively cheap these days.
You can also use have tmpfs on / as an overlay file system. This is quite common for network boot devices that all use a shared nfs root.
That's exactly what this HN post is about. The wiki page being linked to has instructions for doing that.