-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid-converge.nix
More file actions
189 lines (178 loc) · 7.24 KB
/
Copy pathandroid-converge.nix
File metadata and controls
189 lines (178 loc) · 7.24 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Always-on nix-android converge agent (mba launchd + sliceanddice systemd).
# Shared module; phone-side adb lease prevents duplicate apply across hosts.
{
flake.modules.homeManager.dendritic =
{
pkgs,
lib,
config,
inputs,
...
}:
let
cfg = config.dendritic.androidConverge;
dendriticBin = lib.getExe (pkgs.callPackage ../../crates/dendritic/_package.nix { });
system = pkgs.stdenv.hostPlatform.system;
defaultDotfiles =
if pkgs.stdenv.isDarwin then "/etc/nix-darwin/.dotfiles" else "/etc/nixos/.dotfiles";
flakeAttr = if pkgs.stdenv.isDarwin then "oneplus6t-darwin" else "oneplus6t-linux";
hasControllerPkgs = builtins.elem system [
"aarch64-darwin"
"x86_64-linux"
];
androidRebuild = if hasControllerPkgs then inputs.self.packages.${system}.android-rebuild else null;
adbWireless = if hasControllerPkgs then inputs.self.packages.${system}.adb-wireless else null;
fleetCfg = config.dendritic.fleet or { };
convergeScript = pkgs.writeShellScriptBin "android-converge" ''
export PATH="${
lib.makeBinPath [
pkgs.coreutils
pkgs.gnused
pkgs.gnugrep
pkgs.android-tools
pkgs.python3
pkgs.gh
pkgs.jq
]
}:$PATH"
export ANDROID_CONVERGE_HOST_ID=${lib.escapeShellArg cfg.hostId}
export ANDROID_CONVERGE_FLAKE=${lib.escapeShellArg "${cfg.dotfilesRoot}#${cfg.flakeAttr}"}
export ANDROID_CONVERGE_DEVICE=${lib.escapeShellArg cfg.device}
export ANDROID_CONVERGE_BIN=${lib.escapeShellArg "${androidRebuild}/bin/android-rebuild"}
export ANDROID_CONVERGE_ADB_WIRELESS_BIN=${
lib.escapeShellArg (if adbWireless != null then "${adbWireless}/bin/adb-wireless" else "")
}
export ANDROID_CONVERGE_LEASE_TTL=${lib.escapeShellArg (toString cfg.leaseTtlSec)}
export ANDROID_CONVERGE_APPLY=${if cfg.apply then "1" else "0"}
export ANDROID_CONVERGE_STATUS=${lib.escapeShellArg "${config.home.homeDirectory}/.cache/android-converge.status"}
export FLEET_STATUS_OWNER=${lib.escapeShellArg (fleetCfg.owner or "aspauldingcode")}
export FLEET_STATUS_REPO=${lib.escapeShellArg (fleetCfg.repo or "dendritic-fleet-status")}
${lib.optionalString ((fleetCfg.enable or false) && (fleetCfg.useSopsToken or false)) ''
export FLEET_STATUS_TOKEN_FILE=${lib.escapeShellArg config.sops.secrets.fleet_status_github_token.path}
''}
exec ${pkgs.bash}/bin/bash ${../../scripts/android-converge-agent.sh}
'';
logDir = "${config.home.homeDirectory}/.cache";
in
{
options.dendritic.androidConverge = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Always-on android-rebuild agent (launchd on Darwin, systemd user timer
on Linux). Enable on controller hosts only (mba, sliceanddice).
'';
};
hostId = lib.mkOption {
type = lib.types.str;
# Prefer fleet host id when set — same identity for the phone lease.
default = config.dendritic.fleet.hostId or "";
defaultText = lib.literalExpression "config.dendritic.fleet.hostId";
description = "Controller identity written into the on-device lease.";
};
device = lib.mkOption {
type = lib.types.str;
default = "oneplus6t";
description = "nix-android device.name (lease file key + docs).";
};
flakeAttr = lib.mkOption {
type = lib.types.str;
default = flakeAttr;
description = "Flake output after # (oneplus6t-darwin / oneplus6t-linux).";
};
dotfilesRoot = lib.mkOption {
type = lib.types.str;
default = config.dendritic.fleet.dotfilesRoot or defaultDotfiles;
defaultText = lib.literalExpression "config.dendritic.fleet.dotfilesRoot";
description = "Checkout path used in --flake ROOT#ATTR.";
};
intervalSec = lib.mkOption {
type = lib.types.ints.positive;
default = 900;
description = "Converge interval in seconds (default 15m).";
};
leaseTtlSec = lib.mkOption {
type = lib.types.ints.positive;
default = 600;
description = "On-device lease TTL so a crashed controller does not block forever.";
};
apply = lib.mkOption {
type = lib.types.bool;
default = true;
description = "If true, run switch; if false, plan-only (dry).";
};
};
config = lib.mkIf cfg.enable (
lib.mkMerge [
{
assertions = [
{
assertion = cfg.hostId != "";
message = "dendritic.androidConverge.hostId must be set (or enable dendritic.fleet with hostId)";
}
{
assertion = hasControllerPkgs && androidRebuild != null;
message = "dendritic.androidConverge requires android-rebuild on ${system} (aarch64-darwin or x86_64-linux)";
}
];
home.packages = [
androidRebuild
pkgs.android-tools
]
++ lib.optional (adbWireless != null) adbWireless;
}
(lib.mkIf pkgs.stdenv.isDarwin {
launchd.agents.android-converge = {
enable = true;
config = {
Label = "com.aspauldingcode.android-converge";
ProgramArguments = [
dendriticBin
"android"
"converge"
];
RunAtLoad = true;
StartInterval = cfg.intervalSec;
StandardOutPath = "${logDir}/android-converge.log";
StandardErrorPath = "${logDir}/android-converge.err.log";
EnvironmentVariables = {
HOME = config.home.homeDirectory;
PATH = "${
lib.makeBinPath [
convergeScript
pkgs.coreutils
]
}:/usr/bin:/bin";
DENDRITIC_ANDROID_CONVERGE = lib.getExe convergeScript;
};
};
};
})
(lib.mkIf pkgs.stdenv.isLinux {
systemd.user.services.android-converge = {
Unit = {
Description = "Dendritic nix-android converge (leased)";
After = [ "default.target" ];
};
Service = {
Type = "oneshot";
ExecStart = "${dendriticBin} android converge";
Environment = [ "DENDRITIC_ANDROID_CONVERGE=${lib.getExe convergeScript}" ];
};
};
systemd.user.timers.android-converge = {
Unit.Description = "Dendritic nix-android converge timer";
Timer = {
OnBootSec = "2min";
OnUnitActiveSec = "${toString cfg.intervalSec}s";
Persistent = true;
Unit = "android-converge.service";
};
Install.WantedBy = [ "timers.target" ];
};
})
]
);
};
}