bluetooth keys: use sane.fs instead of activationScripts

also auto-determines the device ID, which was previously broken
This commit is contained in:
2023-01-07 03:43:28 +00:00
parent 70a43c770d
commit c063ecd047
2 changed files with 23 additions and 25 deletions

View File

@@ -1,18 +1,12 @@
{ lib, pkgs, ... }: { lib, pkgs, ... }:
{ {
# TODO: don't need to depend on binsh if we were to use a nix-style shebang sane.fs."/var/lib/bluetooth/.secrets.stamp" = {
system.activationScripts.linkBluetoothKeys = let wantedBeforeBy = [ "bluetooth.service" ];
unwrapped = ../../scripts/install-bluetooth; # XXX: install-bluetooth uses sed, but that's part of the default systemd unit path, it seems
install-bluetooth = pkgs.writeShellApplication { generated.script.script = builtins.readFile ../../scripts/install-bluetooth + ''
name = "install-bluetooth"; touch "/var/lib/bluetooth/.secrets.stamp"
runtimeInputs = with pkgs; [ coreutils gnused ]; '';
text = ''${unwrapped} "$@"''; generated.script.scriptArgs = [ "/run/secrets/bt" ];
}; };
in (lib.stringAfter
[ "setupSecrets" "binsh" ]
''
${install-bluetooth}/bin/install-bluetooth /run/secrets/bt
''
);
} }

View File

@@ -1,25 +1,29 @@
#!/bin/sh #!/bin/sh
# usage: install-bluetooth <source_dir> <dest_dir> # usage: install-bluetooth <source_dir> <destdir>
# source_dir contains plain-text files of any filename. # source_dir contains plain-text files of any filename.
# for each file, this extracts the MAC and creates a symlink in dest_dir which # for each file, this extracts the MAC and creates a symlink in destdir which
# points to the original file, using the MAC name as file path # points to the original file, using the MAC name as file path
# #
# bluetooth connection structure is /var/lib/bluetooth/<HOST_MAC>/<DEVICE_MAX>/{attributes,info} # bluetooth connection structure is /var/lib/bluetooth/<HOST_MAC>/<DEVICE_MAX>/{attributes,info}
# # bluetoothd/main.conf options can be found here:
# - <https://pythonhosted.org/BT-Manager/config.html>
set -ex set -ex
src_dir="$1" srcdir="$1"
dest_dir="$2" destdir="$2"
if [ "x$dest_dir" = "x" ] if [ "x$destdir" = "x" ]
then then
devmac=$(cat /sys/kernel/debug/bluetooth/hci0/identity | cut -f 1 -d' ' | tr "a-z" "A-Z")
# default to the first MAC address on the host # default to the first MAC address on the host
dest_dir="/var/lib/bluetooth/$(ls /var/lib/bluetooth)" destdir="/var/lib/bluetooth/$devmac"
test -d "$destdir" || mkdir "$destdir" || test -d "$destdir"
fi fi
for f in $(ls "$src_dir") for f in $(ls "$srcdir")
do do
mac=$(sed -rn 's/# MAC=(.*)/\1/p' "$src_dir/$f") mac=$(sed -rn 's/# MAC=(.*)/\1/p' "$srcdir/$f")
mkdir -p "$dest_dir/$mac" condir="$destdir/$mac"
ln -sf "$src_dir/$f" "$dest_dir/$mac/info" test -d "$condir" || mkdir "$condir" || test -d "$condir"
ln -sf --no-dereference "$srcdir/$f" "$condir/info"
done done