summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/hardware/bitbox-bridge.nix
blob: e7300803efe66bf0c895120acd1b012ebcf8d56a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.bitbox-bridge;
in
{
  options = {
    services.bitbox-bridge = {
      enable = lib.mkEnableOption "Bitbox bridge daemon, for use with Bitbox hardware wallets.";

      package = lib.mkPackageOption pkgs "bitbox-bridge" { };

      port = lib.mkOption {
        type = lib.types.port;
        default = 8178;
        description = ''
          Listening port for the bitbox-bridge.
        '';
      };

      runOnMount = lib.mkEnableOption null // {
        default = true;
        description = ''
          Run bitbox-bridge.service only when hardware wallet is plugged, also registers the systemd device unit.
          This option is enabled by default to save power, when false, bitbox-bridge service runs all the time instead.
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];
    services.udev.packages = [
      cfg.package
    ]
    ++ lib.optionals (cfg.runOnMount) [
      (pkgs.writeTextFile {
        name = "bitbox-bridge-run-on-mount-udev-rules";
        destination = "/etc/udev/rules.d/99-bitbox-bridge-run-on-mount.rules";
        text = ''
          SUBSYSTEM=="usb", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2403", MODE="0660", GROUP="bitbox", TAG+="systemd", SYMLINK+="bitbox02", ENV{SYSTEMD_WANTS}="bitbox-bridge.service"
        '';
      })
    ];

    systemd.services.bitbox-bridge = {
      description = "BitBox Bridge";
      wantedBy = [ "multi-user.target" ];

      bindsTo = lib.optionals (cfg.runOnMount) [ "dev-bitbox02.device" ];
      after = [ "network.target" ];

      serviceConfig = {
        Type = "simple";
        ExecStart = "${cfg.package}/bin/bitbox-bridge -p ${toString cfg.port}";
        User = "bitbox";
      };
    };

    users.groups.bitbox = { };
    users.users.bitbox = {
      group = "bitbox";
      description = "bitbox-bridge daemon user";
      isSystemUser = true;
      extraGroups = [ "bitbox" ];
    };
  };
}