chore: nf-fmt-nix

This commit is contained in:
arcnmx 2024-06-23 11:48:55 -07:00
parent e4bbddb004
commit 32ff0a8e0c
7 changed files with 152 additions and 138 deletions

View file

@ -62,7 +62,7 @@ let
in {
enable = mkDefault true;
port = mkDefault 9091;
extraFlags = [ "--collector.disable-defaults" ];
extraFlags = ["--collector.disable-defaults"];
enabledCollectors = mkIf cfg.defaultCollectors (mkMerge [
[
"systemd"

View file

@ -1,11 +1,17 @@
let
deviceModule = {config, lib, ...}: let
deviceModule = {
config,
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption;
in {
options = with lib.types; {
enable = mkEnableOption "adb device" // {
default = true;
};
enable =
mkEnableOption "adb device"
// {
default = true;
};
uphold = mkOption {
type = bool;
default = true;
@ -15,123 +21,125 @@ let
};
};
};
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";
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 = {};
};
};
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;
config = let
confService.services.adb = {
settings = mapOptionDefaults {
H = config.networking.hostName;
P = cfg.port;
};
});
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.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
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
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
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)
];
}
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)
];
}

View file

@ -15,7 +15,7 @@ in {
services.prometheus.exporters = {
node = {
port = 9091;
extraFlags = [ "--collector.disable-defaults" ];
extraFlags = ["--collector.disable-defaults"];
enabledCollectors = mkMerge [
(mkIf config.boot.supportedFilesystems.xfs or false [
"xfs"

View file

@ -295,14 +295,12 @@ in {
hasService = system: system.exports.services.${service}.enable;
notFound = throw "no system found serving ${service}";
multiple = throw "multiple systems found serving ${service}";
in
(findSingle hasService notFound multiple (attrValues systems));
in (findSingle hasService notFound multiple (attrValues systems));
systemForServiceId = serviceId: let
hasService = system: findSingle (service: service.id == serviceId && service.enable) null multiple (attrValues system.exports.services) != null;
notFound = throw "no system found serving ${serviceId}";
multiple = throw "multiple systems found serving ${serviceId}";
in
(findSingle hasService notFound multiple (attrValues systems));
in (findSingle hasService notFound multiple (attrValues systems));
};
};
}