Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

Enhance NixOS module and CI workflows for ai-memory service

Testb8c562409145
diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml
index 14111c73..987aa0bc 100644
--- a/.github/workflows/nix.yml
+++ b/.github/workflows/nix.yml
@@ -2,9 +2,10 @@ name: nix
# Builds the flake on Linux and macOS so the Nix packaging cannot rot unnoticed.
#
-# Matrix: x86_64-linux (package + NixOS module eval) and aarch64-darwin
-# (package + artifact smoke only). NixOS checks always evaluate x86_64-linux
-# and stay on the Linux leg — Darwin attrs would add noise without signal.
+# Matrix: x86_64-linux (package + NixOS module eval + NixOS systemd container
+# smoke) and aarch64-darwin (package + artifact smoke only). NixOS checks and
+# the OCI/systemd container smoke always target x86_64-linux and stay on the
+# Linux leg — Darwin attrs would add noise without signal.
#
# ai-memory does not otherwise test on Nix, so without this job `flake.nix`
# is source nobody executes: it can break through a dependency bump, a
@@ -26,6 +27,7 @@ on:
- 'Cargo.toml'
- 'rust-toolchain.toml'
- 'scripts/check-nix-packaging.sh'
+ - 'scripts/test-nixos-systemd-container.sh'
- '.github/workflows/nix.yml'
pull_request:
paths:
@@ -35,6 +37,7 @@ on:
- 'Cargo.toml'
- 'rust-toolchain.toml'
- 'scripts/check-nix-packaging.sh'
+ - 'scripts/test-nixos-systemd-container.sh'
- '.github/workflows/nix.yml'
schedule:
# Mondays 05:17 UTC. Off the hour so it does not pile onto the
@@ -76,3 +79,17 @@ jobs:
run: nix build --print-build-logs '.#checks.x86_64-linux.nixos-module-eval'
- if: matrix.system == 'x86_64-linux'
run: nix build --print-build-logs '.#checks.x86_64-linux.nixos-sandbox-parity'
+ # One runtime path: packages.x86_64-linux.nixos-ai-memory-docker builds
+ # system.build.toplevel once (via the docker-image tarball) and the
+ # script imports that rootfs, starts systemd, and checks install +
+ # /healthz + dataDir restart/volume persistence.
+ #
+ # Non-goals for this step (do not expand here):
+ # - A→B package upgrade / SQLite-wiki migration matrix
+ # - Exhaustive settings-options.nix key matrix
+ # - Soft fake-systemd without a real unit start
+ # - Claiming the app Docker image covers the NixOS module
+ # - Darwin / multi-arch NixOS OCI (Linux x86_64 only by design)
+ # - Re-asserting sandbox keys (covered by nixos-sandbox-parity)
+ - if: matrix.system == 'x86_64-linux'
+ run: scripts/test-nixos-systemd-container.sh
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cf388023..379952d6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,14 +18,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
binds). The web UI defaults off (`enableWeb = false`, matching the CLI
flag's own default rather than the packaged unit's hardcoded
`--enable-web`). `flake.nix` adds eval checks for sandbox keys, settings →
- `--config`, and agenix/sops wiring. The existing `flake.nix`
+ `--config`, agenix/sops wiring, refusal contracts (age+sops mutex, secrets
+ in `settings.auth`, forbidden `settings.bind`), `ExecStart`
+ (`--data-dir` / `serve` / `--transport http`), default `StateDirectory` vs
+ custom `dataDir` tmpfiles/`ReadWritePaths`, and cheap
+ network-online / `openFirewall` / `MemoryMax` / `TasksMax` round-trips.
+ `packages.x86_64-linux.nixos-ai-memory-docker` builds one NixOS
+ `system.build.toplevel` (via the docker-image rootfs tarball) and
+ `scripts/test-nixos-systemd-container.sh` runs a privileged systemd
+ container smoke (`systemctl` + `/healthz` + dataDir restart/volume
+ persistence) on the Linux `nix.yml` leg. The existing `flake.nix`
package/devShell/CLI build is unchanged. (#NNN)
### Changed
- `.github/workflows/nix.yml` now builds the flake on `x86_64-linux` and
`aarch64-darwin` with `scripts/check-nix-packaging.sh` artifact smoke tests
- (binary, hooks, config template, `nix run`); NixOS module eval stays on the
- Linux leg only. (#NNN)
+ (binary, hooks, config template, `nix run`); NixOS module eval and the
+ NixOS systemd container smoke stay on the Linux leg only. (#NNN)
### Fixed
- `install-hooks --apply --as-user <user> --auth-token <key>` no longer fails
diff --git a/docs/install.md b/docs/install.md
index 9f95fc23..ec254ef1 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -9,6 +9,8 @@ covers everything else:
- [Configuring the CLI URL and auth](#configuring-the-cli-url-and-auth)
- [Arch Linux native packages (AUR)](#arch-linux-native-packages-aur)
(systemd system service or user service)
+- [Nix / NixOS](#nix--nixos)
+ (flake package, NixOS module, maintainer test ladder)
- [macOS menu bar app](#macos-menu-bar-app)
(self-contained `.app` + LaunchAgent)
- [Configuring other agent CLIs](#configuring-other-agent-clis)
@@ -717,6 +719,60 @@ AI_MEMORY_NATIVE_TEST_IMAGE=quay.io/toolbx/arch-toolbox:latest scripts/test-nati
---
+## Nix / NixOS
+
+User-facing NixOS module setup lives in the [README NixOS
+section](../README.md#nixos) (`nixosModules.default`,
+`services.ai-memory.enable`). This section is the maintainer test ladder
+for the flake and module — what CI runs, and what each tier proves.
+
+### Maintainer test ladder
+
+1. **Package smoke** — `nix build .#packages.<system>.default` then
+ `scripts/check-nix-packaging.sh ./result` (binary `--version`, hooks
+ tree, config template, `nix run`). Runs on Linux and Darwin in
+ `.github/workflows/nix.yml`.
+2. **Eval contracts** — `nix build .#checks.x86_64-linux.nixos-module-eval`
+ and `nixos-sandbox-parity`. Cheap Linux-only asserts for enable/bind/
+ `--config`/secrets wiring, refusal messages (age+sops mutex, secrets in
+ `settings.auth`, `settings.bind`), `ExecStart` (`--data-dir`, `serve`,
+ `--transport http`), default `StateDirectory` vs custom `dataDir`
+ tmpfiles/`ReadWritePaths`, and sandbox key parity with
+ `nix/systemd-sandbox.nix`.
+3. **Toplevel → OCI → container smoke** — one closure path. Building
+ `packages.x86_64-linux.nixos-ai-memory-docker` builds
+ `system.build.toplevel` once (via the nixpkgs docker-image tarball);
+ there is no separate bare-toplevel CI job. Then:
+
+```bash
+scripts/test-nixos-systemd-container.sh
+```
+
+ That script imports the rootfs, runs a privileged systemd container,
+ checks `systemctl is-active ai-memory` and `curl /healthz`, writes a
+ marker under `/var/lib/ai-memory`, restarts the unit, and (by default)
+ remounts a named volume once. Wired into the Linux leg of `nix.yml`
+ only (same path filters as the rest of the Nix job).
+
+**Non-goals** (do not treat these as covered by the ladder above):
+
+- A→B flake/package upgrade or SQLite-wiki migration matrices
+- Exhaustive `settings-options.nix` key coverage
+- Soft/fake systemd without a real unit start
+- Claiming the published app Docker image covers the NixOS module path
+- Darwin / multi-arch NixOS OCI (Linux x86_64 only by design)
+
+Useful knobs for the container smoke:
+
+```bash
+AI_MEMORY_NIXOS_TEST_KEEP=1 scripts/test-nixos-systemd-container.sh
+AI_MEMORY_NIXOS_TEST_VOLUME=0 scripts/test-nixos-systemd-container.sh
+AI_MEMORY_NIXOS_TEST_IMAGE=ai-memory-nixos-test scripts/test-nixos-systemd-container.sh
+AI_MEMORY_DOCKER=podman scripts/test-nixos-systemd-container.sh
+```
+
+---
+
## macOS menu bar app
On a Mac, the self-contained menu bar app is the GUI install: it bundles the
diff --git a/flake.nix b/flake.nix
index 11f74773..ad8f47da 100644
--- a/flake.nix
+++ b/flake.nix
@@ -132,18 +132,113 @@
bind = "0.0.0.0";
};
};
- failingAssertions =
- lib.filter (a: !a.assertion) nonLoopbackNoSecrets.config.assertions;
+ bothAgeAndSops = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ ageSecret = "ai-memory-env";
+ sopsSecret = "ai-memory/env";
+ };
+ };
+ secretsInSettings = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ settings.auth.bearer_token = "sekrit";
+ };
+ };
+ bindInSettings = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ settings.bind = "0.0.0.0";
+ };
+ };
+ withCustomDataDir = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ dataDir = "/data/custom-ai-memory";
+ };
+ };
+ withFirewall = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ openFirewall = true;
+ };
+ };
+ withLimits = mkNixos {
+ services.ai-memory = {
+ enable = true;
+ memoryMax = "2G";
+ tasksMax = 512;
+ };
+ };
+
+ failingAssertions = sys: lib.filter (a: !a.assertion) sys.config.assertions;
+ nonLoopbackFailing = failingAssertions nonLoopbackNoSecrets;
+ ageSopsFailing = failingAssertions bothAgeAndSops;
+ secretsFailing = failingAssertions secretsInSettings;
+ bindFailing = failingAssertions bindInSettings;
enabledSc = enabled.config.systemd.services.ai-memory.serviceConfig;
+ enabledUnit = enabled.config.systemd.services.ai-memory;
execStart = enabledSc.ExecStart;
settingsExec =
withSettings.config.systemd.services.ai-memory.serviceConfig.ExecStart;
+ ageUnit = withAge.config.systemd.services.ai-memory;
+ customSc = withCustomDataDir.config.systemd.services.ai-memory.serviceConfig;
+ customTmpfiles = withCustomDataDir.config.systemd.tmpfiles.rules;
+ limitsSc = withLimits.config.systemd.services.ai-memory.serviceConfig;
+
+ # One NixOS system for the container smoke path. Building its
+ # docker-image tarball builds system.build.toplevel once; there is
+ # no separate bare-toplevel check that would rebuild the same closure.
+ containerNixos = nixpkgs.lib.nixosSystem {
+ system = "x86_64-linux";
+ modules = [
+ self.nixosModules.default
+ (
+ { modulesPath, ... }:
+ {
+ imports = [ "${modulesPath}/virtualisation/docker-image.nix" ];
+ system.stateVersion = "25.05";
+ networking.hostName = "ai-memory";
+ # Slim the closure: docs are useless inside the smoke image.
+ documentation.enable = false;
+ documentation.doc.enable = false;
+ documentation.info.enable = false;
+ documentation.man.enable = false;
+ documentation.nixos.enable = false;
+ services.ai-memory.enable = true;
+ }
+ )
+ ];
+ };
+
+ nixosAiMemoryDocker =
+ linuxPkgs.runCommand "nixos-ai-memory-docker"
+ {
+ meta.description = "NixOS rootfs tarball with services.ai-memory for systemd-in-container smoke tests";
+ passthru = {
+ toplevel = containerNixos.config.system.build.toplevel;
+ tarball = containerNixos.config.system.build.tarball;
+ imageName = "ai-memory-nixos-test";
+ };
+ }
+ ''
+ mkdir -p "$out"
+ # Building this derivation builds toplevel via the tarball dep —
+ # single closure path for CI (no duplicate bare-toplevel job).
+ ln -s ${containerNixos.config.system.build.tarball}/tarball/*.tar.xz \
+ "$out/rootfs.tar.xz"
+ ln -s ${containerNixos.config.system.build.toplevel} "$out/toplevel"
+ printf '%s\n' "ai-memory-nixos-test" > "$out/image-name"
+ '';
nixosChecks = {
nixos-module-eval =
assert lib.hasInfix "--enable-web" execStart;
assert lib.hasInfix "--bind 127.0.0.1:49374" execStart;
+ assert lib.hasInfix "--data-dir" execStart;
+ assert lib.hasInfix " serve " (" " + execStart + " ");
+ assert lib.hasInfix "--transport http" execStart;
assert !(disabled.config.systemd.services ? ai-memory);
assert enabledSc.MemoryDenyWriteExecute == true;
assert enabledSc.RestrictNamespaces == true;
@@ -153,6 +248,8 @@
assert enabledSc.PrivateTmp == true;
assert enabledSc.ProtectHome == true;
assert enabledSc.ProtectSystem == "strict";
+ assert enabledSc.StateDirectory == "ai-memory";
+ assert lib.elem "/var/lib/ai-memory" enabledSc.ReadWritePaths;
assert lib.hasInfix "--config" settingsExec;
assert withAge.config.systemd.services.ai-memory.serviceConfig.EnvironmentFile
== "/run/agenix/ai-memory-env";
@@ -160,9 +257,24 @@
== "/run/secrets/ai-memory/env";
assert loopbackEnvFile.config.systemd.services.ai-memory.serviceConfig.EnvironmentFile
== "-/run/ai-memory/env";
- assert failingAssertions != [ ];
+ assert nonLoopbackFailing != [ ];
assert lib.any (a: lib.hasInfix "non-loopback bind requires" a.message)
- failingAssertions;
+ nonLoopbackFailing;
+ assert lib.any (a: lib.hasInfix "mutually exclusive" a.message) ageSopsFailing;
+ assert lib.any (a: lib.hasInfix "settings.auth must not contain secrets" a.message)
+ secretsFailing;
+ assert lib.any (a: lib.hasInfix "settings.bind is not allowed" a.message)
+ bindFailing;
+ assert (customSc.StateDirectory or null) == null;
+ assert lib.elem "/data/custom-ai-memory" customSc.ReadWritePaths;
+ assert lib.any (r: lib.hasPrefix "d /data/custom-ai-memory 0750 " r) customTmpfiles;
+ assert lib.elem "network.target" enabledUnit.after;
+ assert !(lib.elem "network-online.target" (enabledUnit.wants or [ ]));
+ assert lib.elem "network-online.target" ageUnit.after;
+ assert lib.elem "network-online.target" ageUnit.wants;
+ assert lib.elem 49374 withFirewall.config.networking.firewall.allowedTCPPorts;
+ assert limitsSc.MemoryMax == "2G";
+ assert limitsSc.TasksMax == 512;
linuxPkgs.runCommand "ai-memory-nixos-module-eval" { } "touch $out";
nixos-sandbox-parity =
@@ -196,57 +308,65 @@
};
in
{
- packages.default = rustPlatform.buildRustPackage {
- pname = "ai-memory";
- version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
+ packages =
+ {
+ default = rustPlatform.buildRustPackage {
+ pname = "ai-memory";
+ version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
- src = ./.;
- cargoLock.lockFile = ./Cargo.lock;
+ src = ./.;
+ cargoLock.lockFile = ./Cargo.lock;
- # No nativeBuildInputs needed — the build is fully self-contained
- # (SQLite bundled, libgit2 vendored, rustls with webpki-roots).
+ # No nativeBuildInputs needed — the build is fully self-contained
+ # (SQLite bundled, libgit2 vendored, rustls with webpki-roots).
- # Skip the Tailwind CLI download in the sandbox. The build script
- # falls back to the vendored static/tailwind.css committed to the
- # repo (see crates/ai-memory-web/build.rs).
- TAILWIND_SKIP = "1";
+ # Skip the Tailwind CLI download in the sandbox. The build script
+ # falls back to the vendored static/tailwind.css committed to the
+ # repo (see crates/ai-memory-web/build.rs).
+ TAILWIND_SKIP = "1";
- buildType = "release";
+ buildType = "release";
- # The packaging test suite (tests/packaging.rs) exercises the
- # Docker-wrapper shell script `bin/ai-memory` and needs
- # docker/podman on PATH — not available in a Nix sandbox. The
- # rest of the workspace test suite (unit tests + integration)
- # does not need them and can be run via `nix develop -c cargo
- # test --workspace` on a machine with Docker.
- doCheck = false;
+ # The packaging test suite (tests/packaging.rs) exercises the
+ # Docker-wrapper shell script `bin/ai-memory` and needs
+ # docker/podman on PATH — not available in a Nix sandbox. The
+ # rest of the workspace test suite (unit tests + integration)
+ # does not need them and can be run via `nix develop -c cargo
+ # test --workspace` on a machine with Docker.
+ doCheck = false;
- # Install the bundled hook scripts alongside the binary,
- # mirroring what the AUR PKGBUILD does. Native binary users
- # (`ai-memory serve`, `install-hooks`) look up hooks under
- # the binary's share directory at runtime.
- #
- # `bin/ai-memory` (the Docker-wrapper shell script) is NOT
- # installed — Nix users build the native binary directly and
- # have no need for a Docker wrapper.
- postInstall = ''
- mkdir -p $out/share/ai-memory
- cp -a hooks $out/share/ai-memory/
+ # Install the bundled hook scripts alongside the binary,
+ # mirroring what the AUR PKGBUILD does. Native binary users
+ # (`ai-memory serve`, `install-hooks`) look up hooks under
+ # the binary's share directory at runtime.
+ #
+ # `bin/ai-memory` (the Docker-wrapper shell script) is NOT
+ # installed — Nix users build the native binary directly and
+ # have no need for a Docker wrapper.
+ postInstall = ''
+ mkdir -p $out/share/ai-memory
+ cp -a hooks $out/share/ai-memory/
- # Install the default config template so `ai-memory init`
- # has a known-good starting point without a network fetch.
- mkdir -p $out/etc/ai-memory
- cp crates/ai-memory-cli/templates/config.default.toml \
- $out/etc/ai-memory/config.default.toml
- '';
+ # Install the default config template so `ai-memory init`
+ # has a known-good starting point without a network fetch.
+ mkdir -p $out/etc/ai-memory
+ cp crates/ai-memory-cli/templates/config.default.toml \
+ $out/etc/ai-memory/config.default.toml
+ '';
- meta = {
- description = "Long-term memory for AI coding agents";
- homepage = "https://github.com/akitaonrails/ai-memory";
- license = pkgs.lib.licenses.mit;
- mainProgram = "ai-memory";
+ meta = {
+ description = "Long-term memory for AI coding agents";
+ homepage = "https://github.com/akitaonrails/ai-memory";
+ license = pkgs.lib.licenses.mit;
+ mainProgram = "ai-memory";
+ };
+ };
+ }
+ // lib.optionalAttrs (system == "x86_64-linux") {
+ # Rootfs tarball derived from the same NixOS system as
+ # passthru.toplevel — single closure for the container smoke.
+ nixos-ai-memory-docker = nixosAiMemoryDocker;
};
- };
devShells.default = pkgs.mkShell {
name = "ai-memory-dev";
diff --git a/nix/settings-options.nix b/nix/settings-options.nix
index 69f9c2bc..48dd9060 100644
--- a/nix/settings-options.nix
+++ b/nix/settings-options.nix
@@ -308,6 +308,10 @@ in
auth = lib.mkOption {
type = types.nullOr (types.submodule {
+ # Freeform so a mistaken secret key reaches the module assertion
+ # ("settings.auth must not contain secrets") instead of a generic
+ # "option does not exist" type error.
+ freeformType = types.attrsOf types.str;
options = {
secure_cookie = lib.mkOption { type = types.nullOr types.bool; default = null; };
root_username = lib.mkOption { type = types.nullOr types.str; default = null; };
diff --git a/scripts/test-nixos-systemd-container.sh b/scripts/test-nixos-systemd-container.sh
new file mode 100755
index 00000000..cba37160
--- /dev/null
+++ b/scripts/test-nixos-systemd-container.sh
@@ -0,0 +1,215 @@
+#!/usr/bin/env bash
+# Privileged NixOS systemd-in-container smoke for services.ai-memory.
+#
+# Builds (or reuses) packages.x86_64-linux.nixos-ai-memory-docker — a rootfs
+# tarball derived from one NixOS system.build.toplevel — imports it into
+# Docker/Podman, starts systemd as PID 1, and checks:
+# - systemctl is-active ai-memory
+# - curl /healthz from the host
+# - a marker under /var/lib/ai-memory survives systemctl restart
+# - optional: the same marker survives a recreate with a named volume
+#
+# Non-goals (do not extend this script for them):
+# - A→B flake/package upgrade or SQLite-wiki migration matrices
+# - Exhaustive settings-options.nix key coverage
+# - Soft/fake systemd without a real unit start
+# - Claiming the app Docker image covers the NixOS module path
+# - Re-asserting sandbox key parity (see nixos-sandbox-parity)
+
+set -euo pipefail
+
+IMAGE_NAME="${AI_MEMORY_NIXOS_TEST_IMAGE:-ai-memory-nixos-test}"
+CONTAINER_NAME="${AI_MEMORY_NIXOS_TEST_CONTAINER:-ai-memory-nixos-systemd-test}"
+VOLUME_NAME="${AI_MEMORY_NIXOS_TEST_VOLUME_NAME:-ai-memory-nixos-test-data}"
+KEEP="${AI_MEMORY_NIXOS_TEST_KEEP:-0}"
+# Default on: one volume remount persistence pass. Set 0 to skip.
+TEST_VOLUME="${AI_MEMORY_NIXOS_TEST_VOLUME:-1}"
+PORT="${AI_MEMORY_NIXOS_TEST_PORT:-49374}"
+HEALTH_URL="http://127.0.0.1:${PORT}/healthz"
+DATA_DIR="/var/lib/ai-memory"
+MARKER_PATH="${DATA_DIR}/.ai-memory-nixos-ci-marker"
+MARKER_VALUE="nixos-container-smoke"
+
+ENGINE=""
+ROOTFS=""
+BUILT_IMAGE=0
+
+log() {
+ printf '\n==> %s\n' "$*"
+}
+
+fail() {
+ printf 'error: %s\n' "$*" >&2
+ exit 1
+}
+
+repo_root() {
+ local script_dir
+ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+ cd "${script_dir}/.." && pwd
+}
+
+detect_engine() {
+ if [ -n "${AI_MEMORY_DOCKER:-}" ]; then
+ ENGINE="${AI_MEMORY_DOCKER}"
+ return 0
+ fi
+ if command -v docker >/dev/null 2>&1; then
+ ENGINE=docker
+ return 0
+ fi
+ if command -v podman >/dev/null 2>&1; then
+ ENGINE=podman
+ return 0
+ fi
+ fail "need docker or podman on PATH (or set AI_MEMORY_DOCKER)"
+}
+
+cleanup() {
+ if [ "${KEEP}" = "1" ]; then
+ log "Keeping container ${CONTAINER_NAME} (AI_MEMORY_NIXOS_TEST_KEEP=1)"
+ return 0
+ fi
+ if [ -n "${ENGINE}" ]; then
+ "${ENGINE}" rm -f "${CONTAINER_NAME}" >/dev/null 2>&1 || true
+ if [ "${TEST_VOLUME}" = "1" ]; then
+ "${ENGINE}" volume rm -f "${VOLUME_NAME}" >/dev/null 2>&1 || true
+ fi
+ if [ "${BUILT_IMAGE}" = "1" ] && [ "${KEEP}" != "1" ]; then
+ "${ENGINE}" rmi -f "${IMAGE_NAME}" >/dev/null 2>&1 || true
+ fi
+ fi
+}
+
+wait_for_http() {
+ local url="$1"
+ local i
+ for i in $(seq 1 120); do
+ if curl -fsS "${url}" >/dev/null 2>&1; then
+ return 0
+ fi
+ sleep 0.5
+ done
+ "${ENGINE}" exec "${CONTAINER_NAME}" journalctl -u ai-memory --no-pager -n 120 >&2 || true
+ fail "timed out waiting for ${url}"
+}
+
+wait_for_active() {
+ local i
+ for i in $(seq 1 120); do
+ if "${ENGINE}" exec "${CONTAINER_NAME}" systemctl is-active --quiet ai-memory; then
+ return 0
+ fi
+ sleep 0.5
+ done
+ "${ENGINE}" exec "${CONTAINER_NAME}" systemctl status ai-memory --no-pager >&2 || true
+ "${ENGINE}" exec "${CONTAINER_NAME}" journalctl -u ai-memory --no-pager -n 120 >&2 || true
+ fail "timed out waiting for systemctl is-active ai-memory"
+}
+
+run_container() {
+ local extra_args=("$@")
+ # Privileged + host cgroup namespace: systemd as PID 1 on cgroup v2 hosts
+ # (including GitHub Actions ubuntu-latest) needs this to activate units.
+ "${ENGINE}" run -d --name "${CONTAINER_NAME}" \
+ --privileged \
+ --cgroupns=host \
+ -v /sys/fs/cgroup:/sys/fs/cgroup:rw \
+ -p "127.0.0.1:${PORT}:49374" \
+ "${extra_args[@]}" \
+ "${IMAGE_NAME}" /init
+}
+
+write_marker() {
+ "${ENGINE}" exec -u root "${CONTAINER_NAME}" sh -c \
+ "printf '%s\n' '${MARKER_VALUE}' > '${MARKER_PATH}' && chown ai-memory:ai-memory '${MARKER_PATH}'"
+}
+
+assert_marker() {
+ local got
+ got="$("${ENGINE}" exec "${CONTAINER_NAME}" cat "${MARKER_PATH}")"
+ if [ "${got}" != "${MARKER_VALUE}" ]; then
+ fail "marker mismatch at ${MARKER_PATH}: expected '${MARKER_VALUE}', got '${got}'"
+ fi
+}
+
+smoke_once() {
+ log "Waiting for ai-memory unit"
+ wait_for_active
+ "${ENGINE}" exec "${CONTAINER_NAME}" systemctl is-active ai-memory
+
+ log "Waiting for ${HEALTH_URL}"
+ wait_for_http "${HEALTH_URL}"
+ curl -fsS "${HEALTH_URL}" >/dev/null
+
+ log "Writing marker and restarting ai-memory"
+ write_marker
+ "${ENGINE}" exec "${CONTAINER_NAME}" systemctl restart ai-memory
+ wait_for_active
+ wait_for_http "${HEALTH_URL}"
+ assert_marker
+ log "Marker survived systemctl restart"
+}
+
+main() {
+ detect_engine
+ trap cleanup EXIT
+
+ local root
+ root="$(repo_root)"
+ cd "${root}"
+
+ case "$(uname -s)-$(uname -m)" in
+ Linux-x86_64 | Linux-amd64) ;;
+ *)
+ fail "NixOS container smoke is Linux x86_64 only (got $(uname -s)-$(uname -m))"
+ ;;
+ esac
+
+ log "Building packages.x86_64-linux.nixos-ai-memory-docker"
+ nix build --print-build-logs '.#packages.x86_64-linux.nixos-ai-memory-docker'
+ ROOTFS="$(pwd)/result/rootfs.tar.xz"
+ if [ ! -e "${ROOTFS}" ]; then
+ fail "missing rootfs tarball at ${ROOTFS}"
+ fi
+ if [ -f "$(pwd)/result/image-name" ]; then
+ IMAGE_NAME="$(tr -d '[:space:]' <"$(pwd)/result/image-name")"
+ fi
+
+ cleanup
+ # Re-arm trap after cleanup cleared a previous container.
+ trap cleanup EXIT
+
+ log "Importing rootfs into ${ENGINE} as ${IMAGE_NAME}"
+ "${ENGINE}" import "${ROOTFS}" "${IMAGE_NAME}"
+ BUILT_IMAGE=1
+
+ log "Starting privileged NixOS container ${CONTAINER_NAME}"
+ run_container
+
+ smoke_once
+
+ if [ "${TEST_VOLUME}" = "1" ]; then
+ log "Recreating with named volume ${VOLUME_NAME} at ${DATA_DIR}"
+ "${ENGINE}" rm -f "${CONTAINER_NAME}" >/dev/null
+ "${ENGINE}" volume rm -f "${VOLUME_NAME}" >/dev/null 2>&1 || true
+ "${ENGINE}" volume create "${VOLUME_NAME}" >/dev/null
+
+ run_container -v "${VOLUME_NAME}:${DATA_DIR}"
+ wait_for_active
+ wait_for_http "${HEALTH_URL}"
+ write_marker
+
+ log "Remounting volume and checking marker survival"
+ "${ENGINE}" rm -f "${CONTAINER_NAME}" >/dev/null
+ run_container -v "${VOLUME_NAME}:${DATA_DIR}"
+ wait_for_active
+ wait_for_http "${HEALTH_URL}"
+ assert_marker
+ log "Marker survived volume remount"
+ fi
+
+ log "NixOS systemd container smoke passed"
+}
+
+main "$@"