Nonchalant Guidance

About Me·RSS·My Projects·LinkedIn


Added on: Saturday, 27 May, 2023 | Updated on: Monday, 21 October, 2024

Migrating to NixOS (Part 2)

Erase Your Darlings

In the last post, we covered how I ended up doing a NixOS installation to enable greater reproducibility of my system and packages. I hinted at my future plans to extend this reproducibility to implement “erase your darlings”, in which you explicitly define what files and directories you want saved and your entire system is reconstructed from scratch by NixOS using these saved files. Anything not in these specific folders and files is erased.

There are many reasons why you’d want to do this:

Basic Setup

Erase Your Darlings requires 2 things:

Step 1: Installation and Prep

I originally tried to implement these changes in my old install, but after some difficulties (ultimately my mistake to not have /nix be a btrfs subvolume killed this entire attempt), I opted to do a fresh install. This was also a blessing in disguise, since I found that certain packages I’d installed using nix-env were highlighted to me by their absense, so I added them into the config.

I had to modify the installation a lot, and in the end I used the KDE live USB of NixOS to just do a manual install myself, bypassing the graphical Calamares installer.

Roughly speaking, here are the steps to install:

I decided to do a similar setup to my old install: a /boot/efi mountpoint for ESP, and a LUKS volume with btrfs on top. I ended up fixing my old script in my nix-config repo (GitHub mirror) and using that to automate the install.

The script also created the subvolumes required: /nix, /home, /persist

I first checked hardware-configuration.nix to see what the setup is. To my delight, the subvolumes I’d created had been picked up by NixOS, so all I really had to do was tell NixOS to mount / on tmpfs:

  fileSystems."/" =
    { device = "none";
      fsType = "tmpfs";
      options = [ "defaults" "size=1G" "mode=755" ];
    };

Note: if you’re worried that I am just losing 1 GB RAM to /, don’t. The 1G size is a limit on the size, and is not kept reserved explicity for tmpfs. So, that means that I have only ever used at most 150MB of RAM mounting / to tmpfs, and that was after I’d left my machine on for 3-4 days. A fresh boot brings that number down to low double digits.

The subvolumes are mounted as follows:


  fileSystems."/home" =
    { device = "/dev/mapper/cryptroot";
      fsType = "btrfs";
      options = [ "compress=zstd" "noatime" "subvol=home" ];
    };

  fileSystems."/nix" =
    { device = "/dev/mapper/cryptroot";
      fsType = "btrfs";
      options = [ "compress=zstd" "noatime" "subvol=nix" ];
    };

  fileSystems."/persist" =
    { device = "/dev/mapper/cryptroot";
      fsType = "btrfs";
      options = [ "compress=zstd" "noatime" "subvol=persist" ];
      neededForBoot = true;
    };

Note that it also detected the device name I’d set for the LUKS partition in my shell script. Yay!

This was the most time consuming process since it downloaded everything defined in my config, which had by then become quite sizeable.

Impermanence

After booting in and verifiying everything worked, my attention now turned to persisting relevant data.

The NixOS wiki page on Impermanence has a good starting point for this. I ended up following its advice at first and specialized which folders I wanted to save later down the line based on what programs I was using that needed that persistence.

I don’t have much Impermanence code for my home directory, and opted to remove home-manager entirely. I didn’t really like how it worked (read-only linking), and Impermanence would do the job better. I only link a couple important folders and configs for now, but plan on expanding this number.

Quirks

Conclusion

Erase Your Darlings and Impermanence haven’t really changed how I use my system, but they gotten me much closer to ensuring that if my machine burns down, my Internet connection and shipping speed will be the main bottlenecks in getting me back up and running again.

Before NixOS, I could get personal data and user dotfiles restored. With my previous NixOS install, I could get programs installed again, With my latest install, I can also get some vital system configs back.

This was also an opportunity to reset and rebuild. There are (ironically) some steps I’ve left out in this journey which I don’t remember (like Impermanence not persisting stuff in my home folder correctly, so I had to change permissions on that folder manually for my user as root) that will surface when I do another clean install. Till then, this is good enough for me, and the next time, I’ll test this entire stack by doing an install from scratch using my repo and see how close I get to reproducing the old system with as little manual work as possible.


This website was made using Markdown, Pandoc, and a custom program to automatically add headers and footers (including this one) to any document that’s published here.

Copyright © 2024 Saksham Mittal. All rights reserved. Unless otherwise stated, all content on this website is licensed under the CC BY-SA 4.0 International License