Manually switch to a specialisation

The NixOS Wiki & the Tweag blogpost about specialisations both mention the same way to manually switch to a specialisation:

/run/current-system/specialisation/my-cool-specialisation/bin/switch-to-configuration switch

However, this gives the following error on my system (unstable):

Error: This program does not support being ran outside of the switch-to-configuration environment

Blaming nixpkgs, it looks like this change was introduced about 5 months ago: Blaming nixpkgs/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs at master · NixOS/nixpkgs · GitHub

Any ideas on how to switch to a specialisation without rebooting with this new implementation?

1 Like

If you rebuild it should be possible to run nixos-rebuild --specialization foo switch
thanks to Feature request: add --specialization option to nixos-rebuild · Issue #174065 · NixOS/nixpkgs · GitHub

that works, thank you!
do you also know of a way to activate specialisations without having the config (e.g. the flake with the nixosConfiguration) on the machine?

same as switching to any other config, use --build-host/--target-host

ok let me rephrase that: clearly both specialisations are on the system

$> ls /run/current-system/specialisation/
dark  light

can I activate them without using the flake that contains the nixosConfiguration?

I never tried that but if you want to try it you could try running (on your own rist lol)
/run/current-system/specialisation/<name>/activate

oh hold up that might work

I tried that before, but when trying to go back to the other specialisation, the /run/current-system/specialisation directory was empty…

I thought that was not the correct way to do it, but when switching specialisations using nixos-rebuild --specialisation, /run/current/system/specialisation is empty too, so I guess it is intended (makes sense tbh, the specialisation does not have a sub-specialisation)

the activation scripts for the specialisations run, but there seems to be a difference between activate and switch-to-configuration that I have yet to understand… I’ll post an update here once I figure it out - thanks all of you for the answers so far

Use /nix/var/nix/profiles/system... instead of /run/current-system

1 Like

that’s what I was looking for, thanks! the specialisations listed there don’t change when activating a new one

and in /nix/var/nix/profiles/system I can use switch-to-configuration, which seems to work better than activate when it comes to e.g. replacing config files

TL;DR:
sudo /nix/var/nix/profiles/system/specialisation/my-specialisation/bin/switch-to-configuration switch

for any future readers: the error message I mentioned in my first post seems to have been removed (and replaced by a better way to handle that error) in a commit last week: switch-to-configuration-ng: improve user experience (#349834) · NixOS/nixpkgs@13008b3 · GitHub

(and that link in the first post was not a permalink, so here’s the original error message: nixpkgs/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs at 64ec9ae3dcd9d1daaf9e7470343056ac09ecb577 · NixOS/nixpkgs · GitHub)

1 Like

any possible to just switch specialisation without rebuild in the nixos-rebuild command

you can just do sudo /nix/var/nix/profiles/system/specialisation/my-specialisation/bin/switch-to-configuration switch, like I said 2 posts ago?

Yes, that’s the way.
Since most people use CLI tools like nixos-rebuild or nh, add support within thes tools would be better for consistence