nix config for my personal machines
1{ pkgs, lib, ... }:
2
3{
4 # provisioning script builder
5 provision = {
6 name,
7 serial ? null,
8 disable ? [],
9 uninstall ? [],
10 install ? [], # list of { name, src }
11 files ? {} # src -> dest mapping
12 }:
13 let
14 adb = "${pkgs.android-tools}/bin/adb";
15
16 # Helper to generate disable commands
17 disableCmds = lib.concatMapStringsSep "\n" (pkg: ''
18 echo " - Disabling ${pkg}..."
19 ${adb} ${if serial != null then "-s ${serial}" else ""} shell pm disable-user --user 0 ${pkg} || echo " (Keep going, might be already disabled)"
20 '') disable;
21
22 # Helper to generate uninstall commands
23 uninstallCmds = lib.concatMapStringsSep "\n" (pkg: ''
24 echo " - Uninstalling ${pkg}..."
25 ${adb} ${if serial != null then "-s ${serial}" else ""} shell pm uninstall -k --user 0 ${pkg} || echo " (Keep going, might be already uninstalled)"
26 '') uninstall;
27
28 # Helper to generate install commands
29 installCmds = lib.concatMapStringsSep "\n" (app: ''
30 echo " - Installing ${app.name}..."
31 ${adb} ${if serial != null then "-s ${serial}" else ""} install -r "${app.src}"
32 '') install;
33
34 # Helper to generate push commands
35 pushCmds = lib.concatStringsSep "\n" (lib.mapAttrsToList (src: dest: ''
36 echo " - Pushing ${baseNameOf src} to ${dest}..."
37 ${adb} ${if serial != null then "-s ${serial}" else ""} push "${src}" "${dest}"
38 '') files);
39
40 in pkgs.writeShellScriptBin "provision-${name}" ''
41 set -euo pipefail
42
43 echo "📱 Provisioning Android Device: ${name} ${if serial != null then "(${serial})" else ""}"
44
45 # Check for ADB
46 if ! command -v ${adb} &> /dev/null; then
47 echo "Error: adb not found. This should not happen if built with nix."
48 exit 1
49 fi
50
51 # Check device connection
52 echo " - Checking for device..."
53 ${if serial != null then ''
54 if ! ${adb} devices | grep -q "${serial}"; then
55 echo "Error: Device ${serial} not found or not authorized."
56 echo "Please connect the device and authorize USB debugging."
57 exit 1
58 fi
59 '' else ''
60 DEVICE_COUNT=$(${adb} devices | grep -v "List" | grep "device$" | wc -l)
61 if [ "$DEVICE_COUNT" -eq 0 ]; then
62 echo "Error: No devices found."
63 echo "Please connect a device and authorize USB debugging."
64 exit 1
65 elif [ "$DEVICE_COUNT" -gt 1 ]; then
66 echo "Error: Multiple devices found but no serial specified."
67 ${adb} devices
68 exit 1
69 fi
70 ''}
71
72 echo "✅ Device connected."
73
74 echo "📦 Managing Packages (Debloat)..."
75 ${disableCmds}
76 ${uninstallCmds}
77
78 echo "📥 Installing Applications..."
79 ${installCmds}
80
81 echo "📂 Syncing Files..."
82 ${pushCmds}
83
84 echo "🎉 Provisioning Complete for ${name}!"
85 '';
86}