⏱️ Reading time: 8 min

Reinstalling a machine so it works both as a development workstation and a media center runs into a familiar problem: each role expects different tools, services and network rules. Developer nobe4 solved this without dual boot, without a second system user and without running nixos-rebuild every time they switched roles. The tool was NixOS specialisations, a mechanism that declares variants of the same configuration and lets you switch between them in seconds (source).

📑 En este artículo
  1. What NixOS specialisations are
  2. How to switch profiles without rebooting
  3. What else changes between profiles: files, exclusive services and persistent data
  4. What the VPN demo doesn’t show: knowing which variant is running
  5. NixOS specialisations vs dual boot, a separate user and a full config
  6. What this means if you run your own NixOS
  7. Limitations and when it isn’t enough
  8. Frequently Asked Questions about NixOS Specialisations
    1. Can I declare more than two specialisations?
    2. Do I need flakes for this?
    3. Do specialisations take up extra disk space?
  9. References

The case nobe4 documents is simple to follow: a Mullvad VPN that should sometimes be on and sometimes off, without editing the configuration file or redeploying the whole system. The same logic works for separating a work profile from a personal one, or a home network from a corporate one, on the same laptop.

What NixOS specialisations are

A specialisation is an ordinary NixOS configuration block nested inside another one. By default it inherits the entire parent configuration through the inheritParentConfig option, which is on unless explicitly disabled. That means you only need to declare what changes between one profile and another: the rest of the packages, users and services stay the same (nobe4).

The variants are built together with the parent configuration in the same nixos-rebuild. That has a consequence worth spelling out: the mechanism does not remove the rebuild entirely, it just moves it earlier. The first time you add or edit a specialisation, you still have to rebuild the system, exactly like any other NixOS change. What you avoid is rebuilding every time you want to switch profiles: once compiled, the variants are available in the boot menu and can also be activated live, on the system that is already running, without a reboot.

flowchart TD
    P["Parent configuration"] --> A["Special specialisation (VPN active)"]
    P --> B["Private specialisation (VPN inactive)"]
    A -. "do not inherit from each other" .-> B
Each specialisation inherits from the parent, never from another variant. Foto de Trnava University en Unsplash

How to switch profiles without rebooting

To switch from one specialisation to another in an active session, you run the switch-to-configuration script that NixOS generates for each variant, located at /nix/var/nix/profiles/system/specialisation/<name>/bin/. It supports the same actions as nixos-rebuild: test to try the change without it persisting after a reboot, switch to apply it immediately, and boot so it starts that way next time. Running it stops the services that no longer belong to the active variant and starts the ones that do, without going through the boot menu (nobe4).

That service restart is not automatic for every change: it depends on two options of the NixOS systemd module. restartTriggers ties a configuration file to a service, and when that file’s store path changes between one specialisation and another, restartIfChanged lets NixOS restart the service during activation. Neither option watches the file live: they only act at the moment of the switch, not if the file changes outside of it.

What else changes between profiles: files, exclusive services and persistent data

Beyond turning a service on or off, a specialisation can change the content of a file declared in the parent configuration. nobe4 shows this with an example file under /etc: it is defined in the parent configuration and, inside the specialisation block, overridden with lib.mkForce, which gives that value higher priority than the parent’s and avoids a conflict between the two (source). Switching back to the parent configuration restores the original content without touching the file by hand.

To make a service run only in the parent configuration and in no variant, declaring it outside the specialisation block is not enough: since variants inherit everything by default, that service would still be active in them too. The config.isSpecialisation option solves this, since a module can use it to enable or disable a service depending on whether the current evaluation is the parent configuration or a specialisation.

The third case the article documents is what happens to a service’s data when switching variants. A systemd service can declare RuntimeDirectory for data that should disappear when the service stops, under /run, or StateDirectory for data that must survive reboots and specialisation switches, under /var/lib. Choosing one or the other decides whether a profile switch wipes the service’s state or keeps it.

mkForce, isSpecialisation and RuntimeDirectory cover files, services and data. Foto de ThisisEngineering en Unsplash

What the VPN demo doesn’t show: knowing which variant is running

The Mullvad example solves the problem of turning a service on or off, but it doesn’t say which profile the machine is in at a given moment. NixOS doesn’t expose that information on its own: the config.isSpecialisation option only answers yes or no to whether the current evaluation is a specialisation, without saying which one. To tell several apart, nobe4’s own article declares a custom option, specialisationName, and assigns it manually inside each block (source). Any module that needs that information at runtime, a script or a service, has to read this custom option; it isn’t something NixOS resolves out of the box.

It’s a small nuance, but it matters for anyone planning to use specialisations beyond two profiles: once you reach three or four variants, it’s worth planning from the start how to identify which one is active, because that isn’t free or automatic.

NixOS specialisations vs dual boot, a separate user and a full config

nobe4 arrived at specialisations after ruling out three better-known ways to separate roles on the same machine: dual boot, a second system user, or keeping two complete NixOS configurations and switching between them with a rebuild each time (source). The table below summarizes how they differ.

ApproachWhat it isolatesCost of switching profilesWhen it fits
SpecialisationsServices, packages and files declared differently per variantA single switch-to-configuration, no rebuild or rebootProfiles that share almost all the configuration, like a VPN on or off
Separate full config + nixos-rebuild switchThe whole system, including what didn’t changeMinutes: recompiles the entire generation every timeProfiles different enough that they share almost no configuration
Separate system userSession, environment variables, user appsLogging out and back inSeparating application environments, not system-level systemd services
Dual bootEverything: kernel, drivers, disk stateRebooting the whole machineHardware or drivers incompatible between roles that cannot be reconciled in a single kernel

The underlying difference is that a specialisation is not an independent configuration: it is a diff on top of the same parent configuration. That’s why switching variants is fast, but for the same reason it doesn’t work for isolating roles that need, for example, incompatible kernels or hardware drivers.

What this means if you run your own NixOS

If you run a laptop or a personal server on NixOS and split roles between a corporate VPN and a home network, or between a development profile and a media one, nobe4’s approach is reproducible without extra tooling: you don’t need Home Manager, flakes, or anything outside the system module NixOS already ships with. The first thing worth checking is how much of your current configuration actually differs between roles. If the difference is two or three services or files, specialisations fit; if the difference is the kernel, the drivers or the filesystem, they don’t.

Before declaring the first specialisation, it’s also worth deciding how you’ll identify it at runtime if you plan to have more than two, following the specialisationName pattern described above, and checking which services need an explicit restartTriggers so you don’t end up with a service still running the old configuration after switching variants.

Limitations and when it isn’t enough

The most cited limitation in the article itself is that nested specialisations are ignored: a specialisation inside another specialisation does not inherit from its sibling, only from the parent above it. Declaring a hierarchy of variants within variants does not work the way you might expect (source).

It also doesn’t eliminate the rebuild for the first deployment: adding or editing a specialisation still requires compiling the whole system, just like any change to configuration.nix. And since every variant is built alongside the parent configuration, each specialisation you add adds its own closure to /nix/store: more variants means more disk space taken up by generations you might not use every day.

Finally, the mechanism solves a problem within a single machine. If the goal is maintaining different configurations across multiple machines, that’s a fleet management problem, not a matter of profiles within one system, and specialisations doesn’t address it.

📬 Get new articles by email

We only email about big articles (1-2 a month).

Frequently Asked Questions about NixOS Specialisations

Can I declare more than two specialisations?

Yes. Each specialisation.<name>.configuration block is independent and inherits from the same parent; there’s no fixed limit beyond what your system can compile and store.

Do I need flakes for this?

No. Specialisations is an option of the NixOS module system, available both in a traditional configuration.nix and in a flakes-based configuration.

Do specialisations take up extra disk space?

Yes: since they’re built alongside the parent configuration, each variant adds its own package closure to /nix/store. Having many specialisations that are rarely used bloats the system’s generations.

References

Featured image: Foto de Bernd 📷 Dittrich en Unsplash

How each article is made (sources, testing and editorial criteria): How we work.

Did it work for you? Got a different error? Say so below: questions get answered and help the next reader.

Leave a comment
Categories: Tech NewsTutorials

El Solitario Editorial

Article prepared by the El Solitario editorial team from cited sources, under the editorial responsibility of Luis Navarro. More details in "How we work".

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

You can include code inside <code>…</code> or, for several lines, <pre><code>…</code></pre>.

This site uses Akismet to reduce spam. Learn how your comment data is processed.