mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 04:19:19 -08:00
feat(adb): service module
This commit is contained in:
parent
95e827221f
commit
594605ea69
6 changed files with 196 additions and 0 deletions
137
modules/nixos/adb.nix
Normal file
137
modules/nixos/adb.nix
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
let
|
||||
deviceModule = {config, lib, ...}: let
|
||||
inherit (lib.options) mkOption mkEnableOption;
|
||||
in {
|
||||
options = with lib.types; {
|
||||
enable = mkEnableOption "adb device" // {
|
||||
default = true;
|
||||
};
|
||||
uphold = mkOption {
|
||||
type = bool;
|
||||
default = true;
|
||||
};
|
||||
serial = mkOption {
|
||||
type = str;
|
||||
};
|
||||
};
|
||||
};
|
||||
in {
|
||||
config,
|
||||
gensokyo-zone,
|
||||
utils,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}: let
|
||||
inherit (lib.options) mkOption mkPackageOption mkEnableOption;
|
||||
inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault;
|
||||
inherit (lib.attrsets) filterAttrs mapAttrsToList;
|
||||
inherit (lib.cli) toGNUCommandLine;
|
||||
inherit (utils) escapeSystemdExecArgs escapeSystemdPath;
|
||||
inherit (gensokyo-zone.lib) mapOptionDefaults;
|
||||
cfg = config.services.adb;
|
||||
enabledDevices = filterAttrs (_: device: device.enable) cfg.devices;
|
||||
in {
|
||||
options.services.adb = with lib.types; {
|
||||
enable = mkEnableOption "adb server";
|
||||
package = mkPackageOption pkgs "android-tools" {};
|
||||
rulesPackage = mkPackageOption pkgs "android-udev-rules" {};
|
||||
user = mkOption {
|
||||
type = str;
|
||||
default = "adb";
|
||||
};
|
||||
port = mkOption {
|
||||
type = port;
|
||||
default = 5037;
|
||||
};
|
||||
extraArguments = mkOption {
|
||||
type = listOf str;
|
||||
default = [];
|
||||
};
|
||||
settings = mkOption {
|
||||
type = attrsOf (oneOf [ str int (nullOr bool) ]);
|
||||
};
|
||||
devices = mkOption {
|
||||
type = attrsOf (submoduleWith {
|
||||
modules = [deviceModule];
|
||||
specialArgs = {
|
||||
inherit gensokyo-zone;
|
||||
nixosConfig = config;
|
||||
};
|
||||
});
|
||||
default = {};
|
||||
};
|
||||
};
|
||||
config = let
|
||||
confService.services.adb = {
|
||||
settings = mapOptionDefaults {
|
||||
H = config.networking.hostName;
|
||||
P = cfg.port;
|
||||
};
|
||||
};
|
||||
conf.services.udev.packages = [ cfg.rulesPackage ];
|
||||
conf.environment.systemPackages = [ cfg.package ];
|
||||
conf.users.groups.adbusers = {};
|
||||
conf.systemd.services.adb = {
|
||||
upholds = let
|
||||
upheldDevices = filterAttrs (_: device: device.uphold) enabledDevices;
|
||||
in mapAttrsToList (_: device: "adb-device@${escapeSystemdPath device.serial}.service") upheldDevices;
|
||||
after = ["network.target"];
|
||||
wantedBy = ["multi-user.target"];
|
||||
serviceConfig = {
|
||||
Type = mkOptionDefault "forking";
|
||||
ExecStart = let
|
||||
args = toGNUCommandLine { } cfg.settings ++ cfg.extraArguments;
|
||||
in [
|
||||
"${cfg.package}/bin/adb start-server ${escapeSystemdExecArgs args}"
|
||||
];
|
||||
ExecStop = [
|
||||
"${cfg.package}/bin/adb kill-server"
|
||||
];
|
||||
WorkingDirectory = "/var/lib/adb";
|
||||
StateDirectory = "adb";
|
||||
RuntimeDirectory = "adb";
|
||||
User = cfg.user;
|
||||
};
|
||||
};
|
||||
conf.systemd.services."adb-device@" = rec {
|
||||
requisite = [ "adb.service" ];
|
||||
partOf = requisite;
|
||||
after = requisite;
|
||||
environment = mapOptionDefaults {
|
||||
ANDROID_SERIAL = "%I";
|
||||
};
|
||||
path = [cfg.package pkgs.coreutils];
|
||||
serviceConfig = mapOptionDefaults {
|
||||
User = cfg.user;
|
||||
};
|
||||
script = ''
|
||||
set -eu
|
||||
|
||||
while true; do
|
||||
sleep 1
|
||||
DEVICE_ONLINE=
|
||||
if ADB_STATE=$(adb get-state 2>/dev/null); then
|
||||
if [[ $ADB_STATE == device ]]; then
|
||||
DEVICE_ONLINE=1
|
||||
fi
|
||||
fi
|
||||
if [[ -n $DEVICE_ONLINE ]] || timeout 5 adb connect $ANDROID_SERIAL; then
|
||||
sleep 10
|
||||
else
|
||||
sleep 4
|
||||
fi
|
||||
done
|
||||
'';
|
||||
};
|
||||
conf.users.users.adb = mkIf (cfg.user == "adb") {
|
||||
isSystemUser = true;
|
||||
group = mkDefault "adbusers";
|
||||
home = mkDefault "/var/lib/adb";
|
||||
};
|
||||
in
|
||||
mkMerge [
|
||||
confService
|
||||
(mkIf cfg.enable conf)
|
||||
];
|
||||
}
|
||||
|
|
@ -317,4 +317,9 @@ in {
|
|||
)
|
||||
];
|
||||
};
|
||||
config.users.users.hass = mkIf cfg.enable {
|
||||
extraGroups = mkIf (elem "androidtv" cfg.extraComponents && (config.programs.adb.enable || config.services.adb.enable)) [
|
||||
"adbusers"
|
||||
];
|
||||
};
|
||||
}
|
||||
|
|
|
|||
26
modules/system/exports/adb.nix
Normal file
26
modules/system/exports/adb.nix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
lib,
|
||||
gensokyo-zone,
|
||||
...
|
||||
}: let
|
||||
inherit (gensokyo-zone.lib) mkAlmostOptionDefault;
|
||||
inherit (lib.modules) mkIf;
|
||||
in {
|
||||
config.exports.services.adb = {config, ...}: {
|
||||
displayName = mkAlmostOptionDefault "ADB";
|
||||
nixos = {
|
||||
serviceAttr = "adb";
|
||||
assertions = mkIf config.enable [
|
||||
(nixosConfig: {
|
||||
assertion = config.ports.default.port == nixosConfig.services.adb.port;
|
||||
message = "port mismatch";
|
||||
})
|
||||
];
|
||||
};
|
||||
defaults.port.listen = mkAlmostOptionDefault "localhost";
|
||||
ports.default = {
|
||||
port = mkAlmostOptionDefault 5037;
|
||||
transport = "tcp";
|
||||
};
|
||||
};
|
||||
}
|
||||
26
nixos/adb.nix
Normal file
26
nixos/adb.nix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
config,
|
||||
lib,
|
||||
...
|
||||
}: let
|
||||
inherit (lib.modules) mkIf mkDefault;
|
||||
cfg = config.services.adb;
|
||||
in {
|
||||
services.adb = {
|
||||
enable = mkDefault true;
|
||||
settings = {
|
||||
#a = mkDefault true;
|
||||
};
|
||||
devices = {
|
||||
bedroom-tv.serial = "10.1.1.67:5555";
|
||||
};
|
||||
};
|
||||
systemd.services = mkIf cfg.enable {
|
||||
adb = {
|
||||
environment.ADB_TRACE = mkDefault (toString [ "adb" ]);
|
||||
};
|
||||
};
|
||||
networking.firewall.interfaces.lan = mkIf (cfg.enable && cfg.settings.a or false == true) {
|
||||
allowedTCPPorts = [ cfg.port ];
|
||||
};
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ _: {
|
|||
zigbee2mqtt.enable = true;
|
||||
barcodebuddy.enable = true;
|
||||
postgresql.enable = true;
|
||||
adb.enable = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ in {
|
|||
nixos.cloudflared
|
||||
nixos.postgres
|
||||
nixos.nginx
|
||||
nixos.adb
|
||||
nixos.access.home-assistant
|
||||
nixos.access.zigbee2mqtt
|
||||
nixos.access.grocy
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue