From 36a8b4fa1f441c7ffb09fa312a9540fc101c9d44 Mon Sep 17 00:00:00 2001 From: damocles Date: Sun, 22 Nov 2026 11:58:59 +0100 Subject: [PATCH] vm-tests: build a sample android app offline in the android-dev archetype --- .forgejo/workflows/vm-nightly.yml | 1 + docs/5-project/process/ci-checks.md | 2 +- nix/archetypes/android-sdk-args.nix | 11 ++ nix/archetypes/default.nix | 11 +- nix/vm-test-typecheck.nix | 1 + nix/vm-tests.nix | 1 + nix/vm-tests/agent-android-dev.nix | 145 ++++++++++++++++++ nix/vm-tests/android-dev-sample/build.gradle | 17 ++ .../android-dev-sample/offline.init.gradle | 11 ++ .../android-dev-sample/online.init.gradle | 12 ++ .../android-dev-sample/settings.gradle | 3 + .../src/main/AndroidManifest.xml | 11 ++ .../test/hyperhive/sample/MainActivity.java | 15 ++ .../src/main/res/values/strings.xml | 4 + 14 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 nix/archetypes/android-sdk-args.nix create mode 100644 nix/vm-tests/agent-android-dev.nix create mode 100644 nix/vm-tests/android-dev-sample/build.gradle create mode 100644 nix/vm-tests/android-dev-sample/offline.init.gradle create mode 100644 nix/vm-tests/android-dev-sample/online.init.gradle create mode 100644 nix/vm-tests/android-dev-sample/settings.gradle create mode 100644 nix/vm-tests/android-dev-sample/src/main/AndroidManifest.xml create mode 100644 nix/vm-tests/android-dev-sample/src/main/java/test/hyperhive/sample/MainActivity.java create mode 100644 nix/vm-tests/android-dev-sample/src/main/res/values/strings.xml diff --git a/.forgejo/workflows/vm-nightly.yml b/.forgejo/workflows/vm-nightly.yml index 043882b9d..0cf876d33 100644 --- a/.forgejo/workflows/vm-nightly.yml +++ b/.forgejo/workflows/vm-nightly.yml @@ -46,6 +46,7 @@ jobs: matrix: test: - agent-ephemeral-rootfs + - agent-android-dev - hive-squid - hive-egress-antispoof - swarm-archive diff --git a/docs/5-project/process/ci-checks.md b/docs/5-project/process/ci-checks.md index 4875e8ed7..4ce6068c1 100644 --- a/docs/5-project/process/ci-checks.md +++ b/docs/5-project/process/ci-checks.md @@ -85,7 +85,7 @@ Tests that boot NixOS VMs are the `vmTests..` flake output builds them. It does build the `vm-test-typecheck-` checks (`nix/vm-test-typecheck.nix`): the type check and lint the test driver runs over a test script, on the script text alone. They evaluate no node configuration -and build no VM. They cover all seven tests. `semi-spread` and `swarm-builder-offline` +and build no VM. They cover all eight tests. `semi-spread` and `swarm-builder-offline` read node config or an agent toplevel into their script, so each builds the script with stand-in values (`passthru.typecheckScript`), from the same function as the real one. diff --git a/nix/archetypes/android-sdk-args.nix b/nix/archetypes/android-sdk-args.nix new file mode 100644 index 000000000..f6c6274f2 --- /dev/null +++ b/nix/archetypes/android-sdk-args.nix @@ -0,0 +1,11 @@ +# The arguments the `android-dev` archetype passes to `composeAndroidPackages`, +# in one place so the VM test that builds against the same SDK reads them too. +# build-tools 36.1.0 and platform 35: the pair the damocles agent builds with. No +# emulator and no system images; they are large, and a build is run against a +# device or an emulator elsewhere. +{ + buildToolsVersions = [ "36.1.0" ]; + platformVersions = [ "35" ]; + includeEmulator = false; + includeSystemImages = false; +} diff --git a/nix/archetypes/default.nix b/nix/archetypes/default.nix index 545c77b2c..bef59c74c 100644 --- a/nix/archetypes/default.nix +++ b/nix/archetypes/default.nix @@ -31,15 +31,8 @@ module = { pkgs, lib, ... }: let - # build-tools 36.1.0 and platform 35: the pair the damocles agent builds - # with. No emulator and no system images; they are large, and a build - # is run against a device or an emulator elsewhere. - sdk = pkgs.androidenv.composeAndroidPackages { - buildToolsVersions = [ "36.1.0" ]; - platformVersions = [ "35" ]; - includeEmulator = false; - includeSystemImages = false; - }; + # Arguments in ./android-sdk-args.nix. + sdk = pkgs.androidenv.composeAndroidPackages (import ./android-sdk-args.nix); sdkRoot = "${sdk.androidsdk}/libexec/android-sdk"; # The unfree names the SDK needs: each package it is built from and # the archives those fetch. Read off the SDK itself, because the diff --git a/nix/vm-test-typecheck.nix b/nix/vm-test-typecheck.nix index 1903968ef..6c116dbef 100644 --- a/nix/vm-test-typecheck.nix +++ b/nix/vm-test-typecheck.nix @@ -127,6 +127,7 @@ let tests = { inherit (vmTests) + agent-android-dev hive-squid hive-egress-antispoof swarm-archive diff --git a/nix/vm-tests.nix b/nix/vm-tests.nix index 75d2cde69..98be9f84a 100644 --- a/nix/vm-tests.nix +++ b/nix/vm-tests.nix @@ -9,6 +9,7 @@ }: { agent-ephemeral-rootfs = import ./vm-tests/agent-ephemeral-rootfs.nix { inherit pkgs hivePriv; }; + agent-android-dev = import ./vm-tests/agent-android-dev.nix { inherit pkgs; }; hive-squid = import ./vm-tests/hive-squid.nix { inherit pkgs hivePriv; }; hive-egress-antispoof = import ./vm-tests/hive-egress-antispoof.nix { inherit pkgs hivePriv; }; swarm-archive = import ./vm-tests/swarm-archive.nix { inherit pkgs; }; diff --git a/nix/vm-tests/agent-android-dev.nix b/nix/vm-tests/agent-android-dev.nix new file mode 100644 index 000000000..50a3c693b --- /dev/null +++ b/nix/vm-tests/agent-android-dev.nix @@ -0,0 +1,145 @@ +# `vmTests..agent-android-dev`: the `android-dev` archetype +# (../archetypes/default.nix) builds a real Android app. The module-eval suite +# only reads the archetype's settings; this boots a VM with that module and runs +# `gradle assembleDebug` on a small sample project (./android-dev-sample), which +# is the one thing that shows the pieces work together: the SDK under +# `ANDROID_HOME`, the JDK, Gradle, and `nix-ld`, without which the `aapt2` that +# Android Gradle Plugin downloads cannot start on NixOS. +# +# The VM has no network, so the build is offline. `deps` is a fixed-output +# derivation that builds the sample once with network access, in the sandbox, and +# keeps what Gradle downloaded as a Maven repository. The VM builds against only +# that repository (`offline.init.gradle`). The downloaded `aapt2` is the real +# one, started by the VM's loader. +# +# The fetch cannot start that `aapt2` itself (the build sandbox has no loader; the +# agent container has `nix-ld`), so it runs twice: the first run downloads `aapt2` +# and fails when it starts it, the second points Gradle at the SDK's own `aapt2` +# through `android.aapt2FromMavenOverride` and gets the rest of the dependencies. +# +# When `deps` changes (a new Gradle plugin version in the sample, or a new +# nixpkgs Gradle), its hash changes; the build prints the new one. +{ pkgs }: +let + sample = ./android-dev-sample; + + # The SDK the fetch builds against: the archetype's own arguments, so the + # platform and build tools cannot drift apart. + unfreePkgs = import pkgs.path { + inherit (pkgs.stdenv.hostPlatform) system; + config = { + allowUnfree = true; + android_sdk.accept_license = true; + }; + }; + sdkArgs = import ../archetypes/android-sdk-args.nix; + sdk = unfreePkgs.androidenv.composeAndroidPackages sdkArgs; + sdkRoot = "${sdk.androidsdk}/libexec/android-sdk"; + + deps = + pkgs.runCommand "android-dev-sample-deps-sdk-aapt2" + { + nativeBuildInputs = [ + pkgs.gradle + pkgs.jdk + ]; + ANDROID_HOME = sdkRoot; + JAVA_HOME = "${pkgs.jdk.home}"; + SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = "sha256-IAf9/EeF4dAFuwiQhojXbVJAHkSQNmcmVDAx71MhgoI="; + } + '' + export HOME=$TMPDIR GRADLE_USER_HOME=$TMPDIR/gradle-home + cp -r ${sample} project + chmod -R u+w project + cd project + build() { + gradle --no-daemon --console=plain -I online.init.gradle "$@" assembleDebug + } + + # First run: downloads aapt2, then fails to start it. + build || true + cache=$GRADLE_USER_HOME/caches/modules-2/files-2.1 + jar=$(find "$cache/com.android.tools.build/aapt2" -name 'aapt2-*-linux.jar' | head -n 1) + test -n "$jar" || { echo "the first run did not download aapt2" >&2; exit 1; } + + # Second run: the rest of the build. + build -Pandroid.aapt2FromMavenOverride=${sdkRoot}/build-tools/${builtins.head sdkArgs.buildToolsVersions}/aapt2 + + # Gradle's cache is `group/artifact/version//`; a Maven + # repository has no sha1 directory. + mkdir -p $out + cd "$cache" + find . -type f | while read -r f; do + IFS=/ read -r _ group artifact version _ name <<< "$f" + dest=$out/''${group//.//}/$artifact/$version + mkdir -p "$dest" + cp "$f" "$dest/$name" + done + ''; + + scriptWith = + { sample, deps }: + '' + machine.wait_for_unit("multi-user.target") + + # A login shell, so the build reads the archetype's variables and PATH + # the way an agent's shell does. + machine.succeed("cp -r ${sample} /root/sample && chmod -R u+w /root/sample") + machine.succeed("bash -lc 'echo ANDROID_HOME=$ANDROID_HOME JAVA_HOME=$JAVA_HOME; command -v gradle adb'") + machine.succeed( + "cd /root/sample && ANDROID_DEPS_REPO=${deps} bash -lc " + "'gradle --offline --no-daemon --console=plain -I offline.init.gradle assembleDebug' 2>&1" + ) + + # The APK holds what aapt2 produced: a compiled manifest and resource table. + apk = "/root/sample/build/outputs/apk/debug/sample-debug.apk" + listing = machine.succeed(f"unzip -l {apk}") + for entry in ("AndroidManifest.xml", "resources.arsc", "classes.dex"): + assert entry in listing, f"{entry} missing from the APK:\n{listing}" + ''; +in +pkgs.testers.runNixOSTest { + name = "agent-android-dev"; + + # The build takes a few minutes on the runner; the limit catches a hang. + globalTimeout = 1800; + + # The VM's package set, with the SDK's unfree licence accepted. Left to its own + # `nixpkgs.config`, the node evaluates the archetype's allowance, which reads + # the SDK out of the package set the allowance builds, and recurses. + node.pkgs = unfreePkgs; + node.pkgsReadOnly = false; + + nodes.machine = { + imports = [ + (import ../archetypes/default.nix).android-dev.module + # The agent option the archetype sets a default for, declared here so the + # VM needs no hive module. + ( + { lib, ... }: + { + options.services.hyperhive.agent.icon = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + }; + } + ) + ]; + virtualisation.memorySize = 4096; + virtualisation.cores = 2; + virtualisation.diskSize = 4096; + environment.systemPackages = [ pkgs.unzip ]; + }; + + testScript = scriptWith { inherit sample deps; }; + + # What `nix/vm-test-typecheck.nix` checks: the same text with stand-in paths, + # so the check does not build the dependency fetch. + passthru.typecheckScript = scriptWith { + sample = "/nix/store/stub"; + deps = "/nix/store/stub"; + }; +} diff --git a/nix/vm-tests/android-dev-sample/build.gradle b/nix/vm-tests/android-dev-sample/build.gradle new file mode 100644 index 000000000..6f73f5db4 --- /dev/null +++ b/nix/vm-tests/android-dev-sample/build.gradle @@ -0,0 +1,17 @@ +plugins { + id 'com.android.application' version '8.13.0' +} + +android { + namespace = 'test.hyperhive.sample' + compileSdk = 35 + buildToolsVersion = '36.1.0' + + defaultConfig { + applicationId = 'test.hyperhive.sample' + minSdk = 24 + targetSdk = 35 + versionCode = 1 + versionName = '1.0' + } +} diff --git a/nix/vm-tests/android-dev-sample/offline.init.gradle b/nix/vm-tests/android-dev-sample/offline.init.gradle new file mode 100644 index 000000000..233418c86 --- /dev/null +++ b/nix/vm-tests/android-dev-sample/offline.init.gradle @@ -0,0 +1,11 @@ +// Build with the fetched dependencies as the only repository, so a build that +// needs anything else fails instead of reaching for the network. +beforeSettings { settings -> + def repo = System.getenv('ANDROID_DEPS_REPO') + settings.pluginManagement.repositories { + maven { url = uri(repo) } + } + settings.dependencyResolutionManagement.repositories { + maven { url = uri(repo) } + } +} diff --git a/nix/vm-tests/android-dev-sample/online.init.gradle b/nix/vm-tests/android-dev-sample/online.init.gradle new file mode 100644 index 000000000..985746b6c --- /dev/null +++ b/nix/vm-tests/android-dev-sample/online.init.gradle @@ -0,0 +1,12 @@ +// Build with the real repositories: the fixed-output fetch of the dependencies. +beforeSettings { settings -> + settings.pluginManagement.repositories { + google() + mavenCentral() + gradlePluginPortal() + } + settings.dependencyResolutionManagement.repositories { + google() + mavenCentral() + } +} diff --git a/nix/vm-tests/android-dev-sample/settings.gradle b/nix/vm-tests/android-dev-sample/settings.gradle new file mode 100644 index 000000000..f2da8b376 --- /dev/null +++ b/nix/vm-tests/android-dev-sample/settings.gradle @@ -0,0 +1,3 @@ +// No repositories here: the init scripts in this directory add them, the +// real ones to fetch the dependencies and a file repository to build offline. +rootProject.name = 'sample' diff --git a/nix/vm-tests/android-dev-sample/src/main/AndroidManifest.xml b/nix/vm-tests/android-dev-sample/src/main/AndroidManifest.xml new file mode 100644 index 000000000..5a4e0e87c --- /dev/null +++ b/nix/vm-tests/android-dev-sample/src/main/AndroidManifest.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/nix/vm-tests/android-dev-sample/src/main/java/test/hyperhive/sample/MainActivity.java b/nix/vm-tests/android-dev-sample/src/main/java/test/hyperhive/sample/MainActivity.java new file mode 100644 index 000000000..3e838be3b --- /dev/null +++ b/nix/vm-tests/android-dev-sample/src/main/java/test/hyperhive/sample/MainActivity.java @@ -0,0 +1,15 @@ +package test.hyperhive.sample; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; + +public class MainActivity extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + TextView view = new TextView(this); + view.setText(R.string.app_name); + setContentView(view); + } +} diff --git a/nix/vm-tests/android-dev-sample/src/main/res/values/strings.xml b/nix/vm-tests/android-dev-sample/src/main/res/values/strings.xml new file mode 100644 index 000000000..7639bcc67 --- /dev/null +++ b/nix/vm-tests/android-dev-sample/src/main/res/values/strings.xml @@ -0,0 +1,4 @@ + + + hyperhive sample +