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

19
lib.nix
View file

@ -19,16 +19,21 @@
nibble0 = Str.index part 0; nibble0 = Str.index part 0;
in in
nibble0 + UInt.toHexLower nibble1; nibble0 + UInt.toHexLower nibble1;
in trimAddress6 "${part0 (part 0)}${part 1}:${part 2}ff:fe${part 3}:${part 4}${part 5}"; in
trimAddress6 "${part0 (part 0)}${part 1}:${part 2}ff:fe${part 3}:${part 4}${part 5}";
trimAddress6 = let trimAddress6 = let
matcher = match ''(^|.*:)(0+)([0-9a-fA-F].*)''; matcher = match ''(^|.*:)(0+)([0-9a-fA-F].*)'';
in addr: let in
matched = matcher addr; addr: let
prefix = elemAt matched 0; matched = matcher addr;
postfix = elemAt matched 2; prefix = elemAt matched 0;
addrReplaced = prefix + postfix; postfix = elemAt matched 2;
in if matched == null then addr else trimAddress6 addrReplaced; addrReplaced = prefix + postfix;
in
if matched == null
then addr
else trimAddress6 addrReplaced;
parseUrl = url: let parseUrl = url: let
parts' = Regex.match ''^([^:]+)://(\[[0-9a-fA-F:]+]|[^/:\[]+)(|:[0-9]+)(|/.*)$'' url; parts' = Regex.match ''^([^:]+)://(\[[0-9a-fA-F:]+]|[^/:\[]+)(|:[0-9]+)(|/.*)$'' url;

View file

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

View file

@ -1,11 +1,17 @@
let let
deviceModule = {config, lib, ...}: let deviceModule = {
config,
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
in { in {
options = with lib.types; { options = with lib.types; {
enable = mkEnableOption "adb device" // { enable =
default = true; mkEnableOption "adb device"
}; // {
default = true;
};
uphold = mkOption { uphold = mkOption {
type = bool; type = bool;
default = true; default = true;
@ -15,123 +21,125 @@ let
}; };
}; };
}; };
in { in
config, {
gensokyo-zone, config,
utils, gensokyo-zone,
pkgs, utils,
lib, pkgs,
... lib,
}: let ...
inherit (lib.options) mkOption mkPackageOption mkEnableOption; }: let
inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault; inherit (lib.options) mkOption mkPackageOption mkEnableOption;
inherit (lib.attrsets) filterAttrs mapAttrsToList; inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault;
inherit (lib.cli) toGNUCommandLine; inherit (lib.attrsets) filterAttrs mapAttrsToList;
inherit (utils) escapeSystemdExecArgs escapeSystemdPath; inherit (lib.cli) toGNUCommandLine;
inherit (gensokyo-zone.lib) mapOptionDefaults; inherit (utils) escapeSystemdExecArgs escapeSystemdPath;
cfg = config.services.adb; inherit (gensokyo-zone.lib) mapOptionDefaults;
enabledDevices = filterAttrs (_: device: device.enable) cfg.devices; cfg = config.services.adb;
in { enabledDevices = filterAttrs (_: device: device.enable) cfg.devices;
options.services.adb = with lib.types; { in {
enable = mkEnableOption "adb server"; options.services.adb = with lib.types; {
package = mkPackageOption pkgs "android-tools" {}; enable = mkEnableOption "adb server";
rulesPackage = mkPackageOption pkgs "android-udev-rules" {}; package = mkPackageOption pkgs "android-tools" {};
user = mkOption { rulesPackage = mkPackageOption pkgs "android-udev-rules" {};
type = str; user = mkOption {
default = "adb"; 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 { config = let
type = port; confService.services.adb = {
default = 5037; settings = mapOptionDefaults {
}; H = config.networking.hostName;
extraArguments = mkOption { P = cfg.port;
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.services.udev.packages = [ cfg.rulesPackage ]; conf.environment.systemPackages = [cfg.package];
conf.environment.systemPackages = [ cfg.package ]; conf.users.groups.adbusers = {};
conf.users.groups.adbusers = {}; conf.systemd.services.adb = {
conf.systemd.services.adb = { upholds = let
upholds = let upheldDevices = filterAttrs (_: device: device.uphold) enabledDevices;
upheldDevices = filterAttrs (_: device: device.uphold) enabledDevices; in
in mapAttrsToList (_: device: "adb-device@${escapeSystemdPath device.serial}.service") upheldDevices; mapAttrsToList (_: device: "adb-device@${escapeSystemdPath device.serial}.service") upheldDevices;
after = ["network.target"]; after = ["network.target"];
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
serviceConfig = { serviceConfig = {
Type = mkOptionDefault "forking"; Type = mkOptionDefault "forking";
ExecStart = let ExecStart = let
args = toGNUCommandLine { } cfg.settings ++ cfg.extraArguments; args = toGNUCommandLine {} cfg.settings ++ cfg.extraArguments;
in [ in [
"${cfg.package}/bin/adb start-server ${escapeSystemdExecArgs args}" "${cfg.package}/bin/adb start-server ${escapeSystemdExecArgs args}"
]; ];
ExecStop = [ ExecStop = [
"${cfg.package}/bin/adb kill-server" "${cfg.package}/bin/adb kill-server"
]; ];
WorkingDirectory = "/var/lib/adb"; WorkingDirectory = "/var/lib/adb";
StateDirectory = "adb"; StateDirectory = "adb";
RuntimeDirectory = "adb"; RuntimeDirectory = "adb";
User = cfg.user; User = cfg.user;
};
}; };
}; conf.systemd.services."adb-device@" = rec {
conf.systemd.services."adb-device@" = rec { requisite = ["adb.service"];
requisite = [ "adb.service" ]; partOf = requisite;
partOf = requisite; after = requisite;
after = requisite; environment = mapOptionDefaults {
environment = mapOptionDefaults { ANDROID_SERIAL = "%I";
ANDROID_SERIAL = "%I"; };
}; path = [cfg.package pkgs.coreutils];
path = [cfg.package pkgs.coreutils]; serviceConfig = mapOptionDefaults {
serviceConfig = mapOptionDefaults { User = cfg.user;
User = cfg.user; };
}; script = ''
script = '' set -eu
set -eu
while true; do while true; do
sleep 1 sleep 1
DEVICE_ONLINE= DEVICE_ONLINE=
if ADB_STATE=$(adb get-state 2>/dev/null); then if ADB_STATE=$(adb get-state 2>/dev/null); then
if [[ $ADB_STATE == device ]]; then if [[ $ADB_STATE == device ]]; then
DEVICE_ONLINE=1 DEVICE_ONLINE=1
fi
fi fi
fi if [[ -n $DEVICE_ONLINE ]] || timeout 5 adb connect $ANDROID_SERIAL; then
if [[ -n $DEVICE_ONLINE ]] || timeout 5 adb connect $ANDROID_SERIAL; then sleep 10
sleep 10 else
else sleep 4
sleep 4 fi
fi done
done '';
''; };
}; conf.users.users.adb = mkIf (cfg.user == "adb") {
conf.users.users.adb = mkIf (cfg.user == "adb") { isSystemUser = true;
isSystemUser = true; group = mkDefault "adbusers";
group = mkDefault "adbusers"; home = mkDefault "/var/lib/adb";
home = mkDefault "/var/lib/adb"; };
}; in
in mkMerge [
mkMerge [ confService
confService (mkIf cfg.enable conf)
(mkIf cfg.enable conf) ];
]; }
}

View file

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

View file

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

View file

@ -18,10 +18,12 @@ in {
settings.fail_timeout = mkDefault timeout; settings.fail_timeout = mkDefault timeout;
addr = mkDefault "10.1.1.67"; addr = mkDefault "10.1.1.67";
port = mkDefault nfandroidtv.ports.default.port; port = mkDefault nfandroidtv.ports.default.port;
/*accessService = { /*
accessService = {
system = "bedroomtv"; system = "bedroomtv";
name = "nfandroidtv"; name = "nfandroidtv";
};*/ };
*/
}; };
fallback = let fallback = let
virtualHost = nginx.virtualHosts.nfandroidtv'fallback; virtualHost = nginx.virtualHosts.nfandroidtv'fallback;
@ -70,7 +72,8 @@ in {
config.networking.firewall.interfaces.lan = let config.networking.firewall.interfaces.lan = let
virtualHost = nginx.virtualHosts.nfandroidtv'local; virtualHost = nginx.virtualHosts.nfandroidtv'local;
listen = virtualHost.listen'.nfandroidtv; listen = virtualHost.listen'.nfandroidtv;
in mkIf (virtualHost.enable && listen.enable) { in
allowedTCPPorts = [ listen.port ]; mkIf (virtualHost.enable && listen.enable) {
}; allowedTCPPorts = [listen.port];
};
} }

View file

@ -17,10 +17,10 @@ in {
}; };
systemd.services = mkIf cfg.enable { systemd.services = mkIf cfg.enable {
adb = { adb = {
environment.ADB_TRACE = mkDefault (toString [ "adb" ]); environment.ADB_TRACE = mkDefault (toString ["adb"]);
}; };
}; };
networking.firewall.interfaces.lan = mkIf (cfg.enable && cfg.settings.a or false == true) { networking.firewall.interfaces.lan = mkIf (cfg.enable && cfg.settings.a or false == true) {
allowedTCPPorts = [ cfg.port ]; allowedTCPPorts = [cfg.port];
}; };
} }