Bluetooth Keyboard in Stage 1

Yes, how would I do that? I didn’t find the .nix config receipt specifically for Bluetooth pairing keys in the docs, maybe some common approach would work here?

I’m not sure about what you’re asking exactly.

Are you trying to manage bluetooth associations from the NixOS configuration? If so, there’s no way to do that: pairing a device requires interaction and involves exchanging secrets that are different each time, so you can’t possible do that declatively.

Still, there’s real demand - I don’t want to repair all my BT devices on every reinstall, so prefer to keep the secrets between.
What is my best bet to solve it?

Just… copy that directory over, like the rest of your files.
There’s nothing special about it.

1 Like

Ok, but it would drastically reduce the usability of the system restore procedure, as it includes manual steps now, isn’t it?

I would say it doesn’t actually differ from WiFi password restoration procedure, per se. That one supported declaratively I suppose?

I would say it doesn’t actually differ from WiFi password restoration procedure, per se. That one supported declaratively I suppose?

From a user perspective, maybe: but the protocols don’t work the same way.
Yes, typical WiFi networks use a pre-shared key: you put that into your configuration and you’re done; but Bluetooth connections don’t. On pairing both devices derive a random key using a Diffie Hellman scheme: if you want to preserve the pairing you need to back up that key.

Ok, but it would drastically reduce the usability of the system restore procedure, as it includes manual steps now, isn’t it?

Well, unless you’re trying to build an embedded device you must have some state to manage yourself. DHCP leases, audio controls, bootloader, ssh host keys, printers, and… bluetooth pairings, yes.

I suppose you also have some files in your home directory. Don’t you back them up when reinstalling the OS?

Not per-se, you just treat that directory like data you want to restore from a backup. Use any backup tool with a restore feature, and call that from your installation script.

It’s just impossible to do bluetooth fully declaratively, unfortunately, for the reasons @rnhmjoj outlines.

Seems like this article describes a custom solution to preserve the state (bluetooth pairings included):
https://grahamc.com/blog/erase-your-darlings/

Yeah, that blog post is very cool. The community project that evolved out of that is this one: GitHub - nix-community/impermanence: Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]

Note that this will still not set up anything declaratively, you’re still in charge of maintaining backups yourself, and that will be manual unless you have a proper process for it. It does make it easier, but is fundamentally just a strategy for managing state, nothing magic.

1 Like

I suppose having that setup I can reinstall the system without any backups, and still keep my data.

What do you mean with reinstall?

If shut down the system, boot into the nixos installer and run nixos-install that won’t affect any state on your disk. Of course, if you format the disk, copy your configuration and do nixos-reinstall you will have your system but lost all state: impermanence or not, NixOS is not magic, yet.

I’m not going to format my disk, but rather delete all BTRFS volumes other than /persistent and /boot where the configuration stored.
After that I hope I’ll be able to tweak Nix to reinstall using the impermanence configuration and the data, without extra restoration steps.

The interesting thing about impermanence setups is that NixOS will already “reinstall” itself every time the computer reboots :slight_smile: That’s kind of the point of NixOS - all files on the filesystem that aren’t data should be regenerated on boot.

Hence why some in the community might not consider it a “reinstall” unless the disk is completely wiped, at which point you need a backup process in place for the persistent btrfs volumes (which you should anyway even on non-NixOS hosts).

1 Like

Right, this is the second part of the puzzle.
Some distros have cloud-init support in the installer to address this,
and as I couldn’t find a clear answer if Nix supports this, I added to the issue:
https://github.com/NixOS/nixpkgs/issues/39076#issuecomment-1921437795

It’s turned out the bluetooth keys are needed not only on the installed system, but on the installer itself, too.
As /var/ is tmpfs there, how would I add the keys to .nix configuration, to generate the proper installer .ISO image?

To be clear, you’re trying to make the (let’s say) bluetooth keyboard work in the NixOS installer without any manual step?

I would say it’s possbile to do, but it’s not supported.
You could modify the partition scheme of the ISO image (nixos/modules/installer/cd-dvd/iso-image.nix) to add an extra partition, so that after you write the image onto a USB drive you can copy some files there, and then have it mounted over /var on boot.

It would probably be easier to script the bluetooth pairing, though.

1 Like

The idea is I do want to transplant the keys, so that BT keyboard would work on all the sites without unnecessary repairing again.

That’s right.
The only manual step would be needed to actually run the installer script.
Initially, I intended to avoid that also, but then decided it still would be useful to track the installation process via ssh.

1 Like