Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
468 lines (441 loc) · 18.4 KB
/
Copy pathflake.nix
File metadata and controls
468 lines (441 loc) · 18.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# Nix flake for ai-memory.
#
# Provides:
# nix build # → result/bin/ai-memory (native release binary)
# nix run . -- --version # smoke-test without installing
# nix develop # dev shell with Rust 1.95 (pinned)
#
# The build is self-contained: SQLite is bundled via rusqlite's `bundled`
# feature, libgit2 is vendored via git2's `vendored-libgit2` feature, and
# TLS uses rustls (webpki-roots) — no OpenSSL, no system-library hunting.
# The only extra step is TAILWIND_SKIP=1 so the web crate's build script
# uses the vendored static/tailwind.css instead of downloading the
# Tailwind CLI (which a sandboxed Nix build cannot do).
#
# `doCheck = false` skips the packaging test suite. Those tests exercise
# `bin/ai-memory`, a Docker-wrapper shell script that needs `docker` or
# `podman` on PATH — they are host-environment tests, not build tests,
# and are not Nix's responsibility. Run them manually with
# `nix develop -c cargo test -p ai-memory-cli --test packaging` if your
# machine has Docker.
{
description = "Long-term memory for AI coding agents";
# Inputs are pinned to explicit revisions rather than floating branches.
#
# A flake's value is reproducibility, and `github:NixOS/nixpkgs/nixos-unstable`
# resolves to whatever that branch points at today, so two people — or the
# same person a week apart — can get different builds from identical source.
# Normally `flake.lock` handles this; pinning here achieves the same
# determinism and keeps the tree honest for contributors who do not have
# Nix installed and so cannot regenerate a lock.
#
# To update: bump these revisions deliberately, in their own commit, and
# let the `nix` CI job prove the result still builds.
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/0ae2bc1419c3f345984c2629e72e7a631820fa4d";
flake-utils.url = "github:numtide/flake-utils/11707dc2f618dd54ca8739b309ec4fc024de578b";
rust-overlay = {
url = "github:oxalica/rust-overlay/99607a06c2ea1290cd3258c11d1416dde9201f94";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
...
}:
let
linuxPkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [ (import rust-overlay) ];
};
inherit (linuxPkgs) lib;
# Eval-only NixOS module smoke tests. Kept at the top level under
# checks.x86_64-linux only — the eval always targets x86_64-linux, so
# duplicating these under every package system would add noise on Darwin.
ageSopsStub =
{ lib, ... }:
{
options.age.secrets = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options.path = lib.mkOption { type = lib.types.path; };
});
default = { };
};
options.sops.secrets = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options.path = lib.mkOption { type = lib.types.path; };
});
default = { };
};
config.age.secrets.ai-memory-env.path = "/run/agenix/ai-memory-env";
config.sops.secrets."ai-memory/env".path = "/run/secrets/ai-memory/env";
};
mkNixos = extra: nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.default
ageSopsStub
{ system.stateVersion = "25.05"; }
extra
];
};
# enableWeb defaults to false, so the enabled-smoke config sets
# it explicitly to true — this exercises the --enable-web
# wiring, it isn't asserting what the default is.
enabled = mkNixos { services.ai-memory = { enable = true; enableWeb = true; }; };
disabled = mkNixos { services.ai-memory.enable = false; };
apiOnly = mkNixos {
services.ai-memory = {
enable = true;
enableApi = true;
};
};
withSettings = mkNixos {
services.ai-memory = {
enable = true;
settings.allowed_hosts = [
"localhost"
"127.0.0.1"
"::1"
];
settings.log_level = "info";
};
};
withAge = mkNixos {
services.ai-memory = {
enable = true;
bind = "0.0.0.0";
ageSecret = "ai-memory-env";
};
};
withSops = mkNixos {
services.ai-memory = {
enable = true;
bind = "0.0.0.0";
sopsSecret = "ai-memory/env";
};
};
loopbackEnvFile = mkNixos {
services.ai-memory = {
enable = true;
environmentFile = "/run/ai-memory/env";
};
};
nonLoopbackNoSecrets = mkNixos {
services.ai-memory = {
enable = true;
bind = "0.0.0.0";
};
};
localhostNoSecrets = mkNixos {
services.ai-memory = {
enable = true;
bind = "localhost";
};
};
withIpv6Loopback = mkNixos {
services.ai-memory = {
enable = true;
bind = "::1";
};
};
bothAgeAndSops = mkNixos {
services.ai-memory = {
enable = true;
ageSecret = "ai-memory-env";
sopsSecret = "ai-memory/env";
};
};
ageAndEnvironmentFile = mkNixos {
services.ai-memory = {
enable = true;
ageSecret = "ai-memory-env";
environmentFile = "/run/ai-memory/env";
};
};
secretsInSettings = mkNixos {
services.ai-memory = {
enable = true;
settings.auth.bearer_token = "sekrit";
};
};
headersInSettings = mkNixos {
services.ai-memory = {
enable = true;
settings.llm_headers = [ "Authorization: Bearer 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;
localhostFailing = failingAssertions localhostNoSecrets;
ageSopsFailing = failingAssertions bothAgeAndSops;
ageEnvironmentFailing = failingAssertions ageAndEnvironmentFile;
secretsFailing = failingAssertions secretsInSettings;
headersFailing = failingAssertions headersInSettings;
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;
ipv6Exec = withIpv6Loopback.config.systemd.services.ai-memory.serviceConfig.ExecStart;
apiOnlyExec = apiOnly.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.settings."10-ai-memory";
limitsSc = withLimits.config.systemd.services.ai-memory.serviceConfig;
escapedExecArgs = args: lib.concatStringsSep " " (map builtins.toJSON args);
# 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, pkgs, ... }:
{
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;
# In-container /healthz probe (docker published ports cannot
# reach the module's default 127.0.0.1 bind).
environment.systemPackages = [ pkgs.curl ];
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 (escapedExecArgs [ "--enable-web" ]) execStart;
assert lib.hasInfix (escapedExecArgs [ "--enable-api" ]) apiOnlyExec;
assert !(lib.hasInfix (escapedExecArgs [ "--enable-web" ]) apiOnlyExec);
assert lib.hasInfix
(escapedExecArgs [ "--bind" "127.0.0.1:49374" ]) execStart;
assert lib.hasInfix
(escapedExecArgs [ "--data-dir" "/var/lib/ai-memory" ]) execStart;
assert lib.hasInfix
(escapedExecArgs [ "serve" "--transport" "http" ]) execStart;
assert !(disabled.config.systemd.services ? ai-memory);
assert enabledSc.MemoryDenyWriteExecute == true;
assert enabledSc.RestrictNamespaces == true;
assert enabledSc.UMask == "0077";
assert enabledSc.CapabilityBoundingSet == [ ];
assert enabledSc.NoNewPrivileges == true;
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 (escapedExecArgs [ "--config" ]) settingsExec;
assert withAge.config.systemd.services.ai-memory.serviceConfig.EnvironmentFile
== "/run/agenix/ai-memory-env";
assert withSops.config.systemd.services.ai-memory.serviceConfig.EnvironmentFile
== "/run/secrets/ai-memory/env";
assert loopbackEnvFile.config.systemd.services.ai-memory.serviceConfig.EnvironmentFile
== "-/run/ai-memory/env";
assert nonLoopbackFailing != [ ];
assert lib.any (a: lib.hasInfix "non-loopback bind requires" a.message)
nonLoopbackFailing;
assert lib.any (a: lib.hasInfix "non-loopback bind requires" a.message)
localhostFailing;
assert lib.hasInfix
(escapedExecArgs [ "--bind" "[::1]:49374" ]) ipv6Exec;
assert lib.any (a: lib.hasInfix "mutually exclusive" a.message) ageSopsFailing;
assert lib.any (a: lib.hasInfix "mutually exclusive" a.message)
ageEnvironmentFailing;
assert lib.any (a: lib.hasInfix "settings.auth must not contain secrets" a.message)
secretsFailing;
assert lib.any (a: lib.hasInfix "settings.llm_headers" a.message) headersFailing;
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 customTmpfiles."/data/custom ai-memory".d.mode == "0750";
assert customTmpfiles."/data/custom ai-memory".d.user == "ai-memory";
assert lib.hasInfix
(escapedExecArgs [ "--data-dir" "/data/custom ai-memory" ])
customSc.ExecStart;
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";
};
in
(flake-utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
] (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
inherit (pkgs) lib;
# Read the same toolchain file the project pins for every other CI
# path — rust-toolchain.toml says `channel = "1.95"`.
rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rustPlatform = pkgs.makeRustPlatform {
rustc = rust;
cargo = rust;
};
in
{
packages =
{
default = rustPlatform.buildRustPackage {
pname = "ai-memory";
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"zip-8.6.0" = "sha256-cnLvGK9nhGeYqJYA6kjlYTYChH0fXs2q27rWvHEZWpg=";
};
};
# No nativeBuildInputs needed — the build is fully self-contained
# (SQLite bundled, libgit2 vendored, rustls with webpki-roots).
#
# The binary is deliberately NOT wrapped (wrapProgram renames it
# to `.ai-memory-wrapped`, which hides a running server from the
# live-process guard that matches the process name `ai-memory`).
# Commands that shell out to `git` find it on PATH; the NixOS
# module adds git to the service's PATH.
# 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";
# 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 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://aimemory.sh";
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";
buildInputs = [
rust
pkgs.cargo-watch
];
# Same escape hatch for local `cargo build` / `cargo test` —
# prevents the web crate from trying to download Tailwind.
TAILWIND_SKIP = "1";
shellHook = ''
echo ""
echo "ai-memory dev shell — Rust $(rustc --version)"
echo ""
echo " cargo build --workspace # build"
echo " cargo test --workspace # unit + integration tests"
echo " cargo test -p ai-memory-cli --test packaging # needs docker"
echo ""
'';
};
}
))
// {
# Additive: a NixOS host can run `services.ai-memory.enable = true` to
# get this binary as a hardened systemd service (see
# nix/nixos-module.nix). Merged at the top level, not inside
# eachSystem, because NixOS modules are not system-scoped.
nixosModules.default =
{ pkgs, lib, ... }:
{
imports = [ ./nix/nixos-module.nix ];
config.services.ai-memory.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.default;
};
checks.x86_64-linux = nixosChecks;
};
}