NixOS specialisations

23 points by nobe4 17 hours ago on lobsters | 11 comments

akavel | 4 hours ago

My brain kept getting derailed from focus by seeming AI-sms in the text, but it felt like there's some core in there that is interesting and I'd like to understand maybe. Between the article and the NixOS Wiki page it links to, a summary/tl;dr I'd like to have available for myself instead half an hour ago, that I think I'd have found clearer. No offence intended, sorry, I just sincerely personally would prefer such a shorter version, at least that's what I feel, and the seeming AI-sms sincerely confused me and made it hard for me to focus and understand.

tl;dr IIUC:

  • You can put more than one Nix Expression, each describing a somewhat different variant of your system, together in one configuration.nix file (in specialisation subpath).
  • You can then switch between them with: nixos-rebuild switch --specialisation $NAME. This will (probably) swap systemd services & files on the disk on the spot, but (as usual) won't magically swap your kernel version/build flags without a reboot.
  • This can be used to easily switch between e.g. an "I'm videogaming" profile (most background services disabled for raw performance) and an "I'm coding" profile (tons of crap running in background for coding faster and easier).
  • Also it can e.g. swap VPN configs at the same time as well (say, one for "I'm videogaming" with my close friends scattered around the world, vs. another for "I'm coding" with my employer and coworkers).
  • Sincerely cool! Not sure I have a real use for it personally now, but will keep it in mind, maybe something will show up. Thanks for sharing!

k749gtnc9l3w | 4 hours ago

They are also switchable through boot menu, if desired. (Like in the driver setup situation I mentioned in another comment)

viperML | 3 hours ago

You are correct! Additionally:

  • Activating different spesialisations is done by the same mechanism as activating different generations. In the end you just have a NixOS system closure that is being activated. This allows for rollbacks, etc.
  • You can select a different spesialisation at boot with Grub/systemd-boot (same as selecting a different generation). I used to have a different spesialisation for different Nvidia drivers, so I could just bundle all of them and pick one or the other at boot.
  • It is also useful for trying different DE/WM's. For example, a base system with Gnome enabled, and a spesialisation with gnome disabled and sway enabled. This way, you avoid a bloated base system with conflicting services or applications.

[OP] nobe4 | 47 minutes ago

In the end you just have a NixOS system closure that is being activated. This allows for rollbacks, etc.

Nice! This is good info. I did read that the switch-to-configuration is what nixos-rebuild switch calls under the hood, so that makes sense. But them being the same entity ("system closure") is great to know.

I don't claim to be a Nix expert and use those writings mainly as a way to understand more a topic. (you can see the other posts for such explorations).

[OP] nobe4 | 50 minutes ago

No offense taken, all words are mine but I have heard that I write with an AI vibe sometimes. And I appreciate the review, it'll help me write better in the future!

A TL;DR is definitely a good idea, I didn't feel that it was needed because each section was relatively short, but I'll keep the idea for future articles.

jaculabilis | 12 hours ago

Ah, so that's what you would use specializations for. Most of my configuration is stuff that I don't need to not be in my system when I'm not using it, but changing the whole networking setup makes sense as a use case.

Makes me want to set up a systemd service that switches into a Lenten mode automatically.

k749gtnc9l3w | 5 hours ago

There are also some kernel module choices that have no clear least bad answer, and need to be made relatively early in the boot sequence.

[OP] nobe4 | 9 hours ago

In my real config (omitted for brevity) I also have a couple services that I don't want to always have running.

My initial plan was to have a script to turn them all on/off, but this feels slightly nicer.

juuso | 7 hours ago

My initial plan was to have a script to turn them all on/off

Here's one approach which does it by extending the NixOS configuration at runtime: https://github.com/tupakkatapa/nixos-runtime-modules

I use this in my tmpfs configuration because I need to keep the base image under 4Gb.

[OP] nobe4 | 41 minutes ago

Uh interesting! Do I understand correctly that the benefits of that over specialisations is that you don't rebuild everything/make a duplication.

For my main computer I don't particularly care (any games takes GB already, so I have space haha), but for smaller systems that makes sense!

viccuad | 2 hours ago

This seems quite useful.

I have a gaming PC that is dualbooting with Bazzite as a local LLM testbed. Right now, I WoL the Bazzite distro, and do efibootmgr --bootnext nixos to start the local LLM testbed. With NixOS specializations, I could move the gaming side to Nixos, and swap all inside NixOS.