As I reinstalled one of my machines, I decided I would use it for dev work and media management. Those two roles have slightly different expectations in terms of available tools, services, settings, etc.
While looking for a solution that didn’t involve a dual boot, a different user, or a full nixos-rebuild, I stumbled upon specialisations.
In this post, I’ll use local VPN configuration to show how specialisations work.
Goal ¶
I want to switch VPN configurations without editing my Nix config and deploying it every time. The machine stays mostly the same. Only a few settings and services change.
Mullvad is configured very simply with an optional service for a more rigorous locking down of the network’s permissions:
# default.nix
services.mullvad-vpn.enable = true;
# service.nix
imports = [ ./default.nix ];
services.mullvad-vpn.enableEarlyBootBlocking = true;
The goal would be to reuse the configuration as much as possible, and keep the diff minimal.
One system, several configurations ¶
A specialisation is a block containing ordinary NixOS configuration. Its inheritParentConfig option defaults to true, so it inherits the parent configuration. Only the different values need to be declared.
The variants are built alongside the parent configuration. They can appear in the boot menu, and they can be activated on the running machine, without rebuilding or rebooting.1
With inheritance enabled, variants inherit the parent configuration, not each other. Nested specialisations are ignored.
parent
├── special
└── private <- doesn't inherit from 'special'
Let’s see a couple of things you could do with such a setup.
Updating a file ¶
Define the file in the parent configuration, then override its contents inside the specialisation:
environment.etc."run-on-both.conf".text = "parent";
specialisation.special.configuration = {
environment.etc."run-on-both.conf".text = lib.mkForce "special";
};
Both configurations manage the same /etc/run-on-both.conf file. Activating special changes its contents to special. Returning to the parent configuration changes them back to parent.
lib.mkForce gives the specialisation’s value a higher priority than the value in the parent configuration, avoiding a conflict.
Restarting a service ¶
If a service reads a config file, it may need to restart when switching configurations.
restartTriggers links the file to the service. When its store path changes between configurations, restartIfChanged allows NixOS to restart the service during activation. These options do not watch files for live changes.
systemd.services.run-on-both = {
wantedBy = [ "multi-user.target" ];
restartIfChanged = true;
restartTriggers = [
config.environment.etc."run-on-both.conf".source
];
};
Running a service in only the parent configuration ¶
Because the parent configuration is inherited, putting a service outside specialisation.special.configuration would not, by itself, make it exclusive to the parent configuration.
For this, one can use config.isSpecialisation and enable/disable a service. The services can run independently:
systemd.services.run-only-on-parent = {
enable = !config.isSpecialisation;
# ...
};
specialisation.special.configuration = {
systemd.services.run-only-on-special = {
# ...
};
};
Identifying the configuration in Nix ¶
config.isSpecialisation tells a module whether the configuration being evaluated is a specialisation, but not which one. To get the name of the configuration being evaluated, one can declare a custom option:
options.specialisationName = lib.mkOption {
type = lib.types.str;
default = "parent";
};
config.specialisation = {
special.configuration.specialisationName = "special";
private.configuration.specialisationName = "private";
};
Other modules can read config.specialisationName.
Keeping data ¶
To keep a service’s data across switches, use a persistent state directory. For files that should disappear when the service stops, use a runtime directory.
A service can declare both:
serviceConfig = {
RuntimeDirectory = "specialisation-test"; # temporary
StateDirectory = "specialisation-test"; # permanent
};
Systemd’s directory options create them before run-on-both starts:
- RuntimeDirectory creates /run/specialisation-test, available as $RUNTIME_DIRECTORY. By default, systemd removes it when the service stops.
- StateDirectory creates /var/lib/specialisation-test, available as $STATE_DIRECTORY. It survives service stops and reboots.
Switching configurations ¶
To switch to a specialisation, run its corresponding switch-to-configuration. Like nixos-rebuild, it accepts actions such as test, switch, and boot.
$ /nix/var/nix/profiles/system/specialisation/special/bin/switch-to-configuration test
stopping the following units: run-on-both.service, run-only-on-parent.service
# ...
starting the following units: run-on-both.service
the following new units were started: run-only-on-special.service
The active services and configuration now match special:
$ systemctl list-units 'run-on-both.service' 'run-only-on-*.service' --all --no-pager
UNIT LOAD ACTIVE SUB
run-on-both.service loaded active exited
run-only-on-special.service loaded active exited
$ cat /etc/run-on-both.conf
special
$ cat /run/specialisation-test/events
run-on-both special started
run-only-on-special started
Returning to the parent configuration ¶
Use the parent configuration’s activation script to switch back:
$ /nix/var/nix/profiles/system/bin/switch-to-configuration test
stopping the following units: run-on-both.service, run-only-on-special.service
# ...
starting the following units: run-on-both.service
the following new units were started: run-only-on-parent.service
The services and files return to the parent configuration:
$ systemctl list-units 'run-on-both.service' 'run-only-on-*.service' --all --no-pager
UNIT LOAD ACTIVE SUB
run-on-both.service loaded active exited
run-only-on-parent.service loaded active exited
$ cat /etc/run-on-both.conf
parent
$ cat /run/specialisation-test/events
run-on-both parent started
run-only-on-parent started
The persistent history, however, kept the whole round trip:
$ cat /var/lib/specialisation-test/history
run-on-both parent started
run-only-on-parent started
run-only-on-parent stopped # <- switch to 'special'
run-on-both parent stopped
run-on-both special started
run-only-on-special started
run-only-on-special stopped # <- switch to 'parent'
run-on-both special stopped
run-on-both parent started
run-only-on-parent started
Full example ¶
Here’s the complete example used above:
{ config, lib, ... }:
let
runOnlyOn = name: {
wantedBy = [ "run-on-both.service" ];
after = [ "run-on-both.service" ];
script = ''
echo "run-only-on-${name} started" >> /run/specialisation-test/events
echo "run-only-on-${name} started" >> /var/lib/specialisation-test/history
'';
preStop = ''
echo "run-only-on-${name} stopped" >> /run/specialisation-test/events
echo "run-only-on-${name} stopped" >> /var/lib/specialisation-test/history
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
};
in
{
environment.etc."run-on-both.conf".text = "parent\n";
systemd.services.run-on-both = {
restartIfChanged = true;
restartTriggers = [
config.environment.etc."run-on-both.conf".source
];
script = ''
read -r mode < /etc/run-on-both.conf
echo "$mode" > "$RUNTIME_DIRECTORY/mode"
echo "run-on-both $mode started" >> "$RUNTIME_DIRECTORY/events"
echo "run-on-both $mode started" >> "$STATE_DIRECTORY/history"
'';
preStop = ''
read -r mode < "$RUNTIME_DIRECTORY/mode"
echo "run-on-both $mode stopped" >> "$RUNTIME_DIRECTORY/events"
echo "run-on-both $mode stopped" >> "$STATE_DIRECTORY/history"
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
RuntimeDirectory = "specialisation-test";
StateDirectory = "specialisation-test";
};
};
systemd.services.run-only-on-parent = (runOnlyOn "parent") // {
enable = !config.isSpecialisation;
};
specialisation.special.configuration = {
environment.etc."run-on-both.conf".text = lib.mkForce "special\n";
systemd.services.run-only-on-special = runOnlyOn "special";
};
}
Applying this to the VPN ¶
My real configuration already enables Mullvad. The locked specialisation now imports a lockdown module:
specialisation.locked.configuration = {
imports = [ ./mullvad/lockdown.nix ];
};
This module changes the Mullvad settings to enable automatic connection and lockdown mode, and enables early boot blocking. The shared Mullvad service already has a restart trigger for its settings file, just like run-on-both.
For me, the useful part is keeping everything declared in Nix. I still rebuild when I change configuration, but choosing between the available modes is much faster just a command away.
- Some changes, such as a different kernel, still need a reboot. ↩︎