about summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorMichele Guerini Rocco <rnhmjoj@users.noreply.github.com>2017-02-17 19:44:04 +0100
committerJoachim F <joachifm@users.noreply.github.com>2017-02-17 19:44:04 +0100
commit5231d0ac290f387885a20ece0c5cbb89ee223472 (patch)
tree388b3b2665d264cc11acac9c21e75f67749779e9 /nixos
parent89a036506396dd869474a32e984f5cab5c07992a (diff)
downloadnixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar.gz
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar.bz2
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar.lz
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar.xz
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.tar.zst
nixlib-5231d0ac290f387885a20ece0c5cbb89ee223472.zip
bluetooth module: add option to power up bluetooth controller (#22685)
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/hardware/bluetooth.nix49
1 files changed, 36 insertions, 13 deletions
diff --git a/nixos/modules/services/hardware/bluetooth.nix b/nixos/modules/services/hardware/bluetooth.nix
index de0d48032113..71b3a93a2e0d 100644
--- a/nixos/modules/services/hardware/bluetooth.nix
+++ b/nixos/modules/services/hardware/bluetooth.nix
@@ -1,8 +1,11 @@
 { config, lib, pkgs, ... }:
 
 with lib;
+
 let
   bluez-bluetooth = pkgs.bluez;
+  cfg = config.hardware.bluetooth;
+
 in
 
 {
@@ -11,33 +14,53 @@ in
 
   options = {
 
-    hardware.bluetooth.enable = mkOption {
-      type = types.bool;
-      default = false;
-      description = "Whether to enable support for Bluetooth.";
+    hardware.bluetooth.enable = mkEnableOption "support for Bluetooth.";
+
+    hardware.bluetooth.powerOnBoot = mkOption {
+      type    = types.bool;
+      default = true;
+      description = "Whether to power up the default Bluetooth controller on boot.";
     };
 
   };
 
   ###### implementation
 
-  config = mkIf config.hardware.bluetooth.enable {
+  config = mkIf cfg.enable {
 
     environment.systemPackages = [ bluez-bluetooth pkgs.openobex pkgs.obexftp ];
 
     services.udev.packages = [ bluez-bluetooth ];
-
     services.dbus.packages = [ bluez-bluetooth ];
+    systemd.packages       = [ bluez-bluetooth ];
+
+    services.udev.extraRules = optionalString cfg.powerOnBoot ''
+      ACTION=="add", KERNEL=="hci[0-9]*", ENV{SYSTEMD_WANTS}="bluetooth-power@%k.service"
+    '';
+
+    systemd.services = {
+      bluetooth = {
+        wantedBy = [ "bluetooth.target" ];
+        aliases  = [ "dbus-org.bluez.service" ];
+      };
+
+      "bluetooth-power@" = mkIf cfg.powerOnBoot {
+        description = "Power up bluetooth controller";
+        after = [
+          "bluetooth.service"
+          "suspend.target"
+          "sys-subsystem-bluetooth-devices-%i.device"
+        ];
+        wantedBy = [ "suspend.target" ];
+
+        serviceConfig.Type      = "oneshot";
+        serviceConfig.ExecStart = "${pkgs.bluez.out}/bin/hciconfig %i up";
+      };
 
-    systemd.packages = [ bluez-bluetooth ];
-
-    systemd.services.bluetooth = {
-      wantedBy = [ "bluetooth.target" ];
-      aliases = [ "dbus-org.bluez.service" ];
     };
 
-    systemd.user.services.obex = {
-      aliases = [ "dbus-org.bluez.obex.service" ];
+    systemd.user.services = {
+      obex.aliases = [ "dbus-org.bluez.obex.service" ];
     };
 
   };