Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
385 lines (356 loc) · 15.4 KB
/
Copy pathflake.nix
File metadata and controls
385 lines (356 loc) · 15.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
{
description = "Installable maco package and development shell for the Multi-Agent Coding Orchestrator";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, rust-overlay, ... }:
let
inherit (nixpkgs) lib;
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forEachSystem = lib.genAttrs systems;
supplyChainPins =
builtins.fromTOML (builtins.readFile ./scripts/supply_chain_pins.toml);
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
# The package fileset has no `.git`, so identity comes from the flake.
macoSourceIdentity =
if builtins.isString (self.rev or null)
&& builtins.match "[0-9a-f]{40}" self.rev != null then {
revision = self.rev;
state = "clean";
} else if builtins.isString (self.dirtyRev or null)
&& builtins.match "[0-9a-f]{40}" (lib.removeSuffix "-dirty" self.dirtyRev) != null then {
# Nix appends `-dirty` to the commit. The recorded revision stays
# the 40-digit commit; the suffix is not part of the identity.
revision = lib.removeSuffix "-dirty" self.dirtyRev;
state = "dirty";
} else {
revision = "";
state = "unknown";
};
pkgsFor = system:
import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
rustToolchainFor = pkgs:
pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# Headless CAM crate (`coding-agent-manager`). Omit the desktop
# shell, Node tree, and desktop-only lockfile.
camHeadlessLibSrc = ./account-manager/core;
macoSrc = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./.cargo
./assets
./benches
./build.rs
./Cargo.lock
./Cargo.toml
./flake.nix
./flake.lock
./rust-toolchain.toml
./src
camHeadlessLibSrc
];
};
macoPackageFor = system:
let
pkgs = pkgsFor system;
rustToolchain = rustToolchainFor pkgs;
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# Hash immutable source inputs, never Cargo's modified build tree.
sourceManifest = pkgs.runCommand "maco-source-manifest.json" { } ''
${pkgs.python3}/bin/python3 - "${macoSrc}" "$out" <<'PY'
import hashlib
import json
import os
import pathlib
import sys
root = pathlib.Path(sys.argv[1])
files = []
for path in sorted(root.rglob("*"), key=lambda path: path.relative_to(root).as_posix()):
relative = path.relative_to(root).as_posix()
if path.is_symlink():
files.append({
"path": relative,
"type": "symlink",
"target_sha256": hashlib.sha256(os.fsencode(os.readlink(path))).hexdigest(),
})
elif path.is_file():
with path.open("rb") as source:
digest = hashlib.file_digest(source, "sha256").hexdigest()
metadata = path.stat()
files.append({
"path": relative,
"type": "file",
"sha256": digest,
"size": metadata.st_size,
"executable": bool(metadata.st_mode & 0o111),
})
elif not path.is_dir():
raise ValueError("unsupported source entry")
manifest = {
"schema_version": 1,
"source_revision": "${macoSourceIdentity.revision}",
"source_state": "${macoSourceIdentity.state}",
"files": files,
}
pathlib.Path(sys.argv[2]).write_text(
json.dumps(manifest, sort_keys=True, separators=(",", ":")) + "\n",
encoding="utf-8",
)
PY
'';
in
rustPlatform.buildRustPackage {
pname = "maco";
version = cargoToml.package.version;
src = macoSrc;
cargoLock.lockFile = ./Cargo.lock;
env = {
MACO_SOURCE_REVISION = macoSourceIdentity.revision;
MACO_SOURCE_STATE = macoSourceIdentity.state;
};
# Read the generated input at build time, without import-from-derivation.
preBuild = lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
export MACO_SOURCE_MANIFEST_SHA256="$(sha256sum "${sourceManifest}" | cut -d ' ' -f 1)"
'';
# Select the public CLI and, on Linux, the CAM authority and setup tools.
cargoBuildFlags = [ "--locked" "-p" "multi-agent-coding-orchestrator" "--bin" "maco" ]
++ lib.optionals pkgs.stdenv.hostPlatform.isLinux [
"-p" "coding-agent-manager" "--bin" "account-authority" "--bin" "cam-grok-setup"
];
nativeBuildInputs = [
pkgs.cmake
pkgs.pkg-config
];
buildInputs =
lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.dbus
]
++ lib.optionals pkgs.stdenv.hostPlatform.isDarwin [
pkgs.libiconv
];
# Packaging builds the selected release binaries. The repository Cargo
# gates remain the correctness suite; they need Git, fixtures, and
# sometimes a delegated user manager that this derivation does not
# provide.
doCheck = false;
doInstallCheck = true;
installCheckPhase = ''
runHook preInstallCheck
test -f "$out/bin/maco"
test ! -L "$out/bin/maco"
test -x "$out/bin/maco"
version_output="$("$out/bin/maco" --version)"
echo "$version_output"
printf '%s\n' "$version_output" | grep -Fx "maco ${cargoToml.package.version}"
printf '%s\n' "$version_output" | grep -Fx "package_version=${cargoToml.package.version}"
printf '%s\n' "$version_output" | grep -Fx "source_revision=${macoSourceIdentity.revision}"
printf '%s\n' "$version_output" | grep -Fx "source_state=${macoSourceIdentity.state}"
test ! -e "$out/bin/multi-agent-coding-orchestrator"
'' + lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
test -f "$out/bin/account-authority"
test ! -L "$out/bin/account-authority"
test -x "$out/bin/account-authority"
helper_help="$("$out/bin/account-authority" --help 2>&1)"
printf '%s\n' "$helper_help" | grep -Fx "Usage: account-authority --socket-path PATH"
test -f "$out/bin/cam-grok-setup"
test ! -L "$out/bin/cam-grok-setup"
test -x "$out/bin/cam-grok-setup"
setup_help="$("$out/bin/cam-grok-setup" --help)"
printf '%s\n' "$setup_help" | grep -Fx "Usage: cam-grok-setup [--data-dir ABSOLUTE_PATH] prepare ACCOUNT_ID"
printf '%s\n' "$setup_help" | grep -Fx "No passwords, tokens, auth paths, or default-home import are accepted."
test -f "$out/source-manifest.json"
test ! -L "$out/source-manifest.json"
cmp "${sourceManifest}" "$out/source-manifest.json"
cmp "$out/source-manifest.json" "$out/share/maco/source-manifest.json"
${pkgs.python3}/bin/python3 - "$out" "${macoSrc}/src/bin/maco.rs" "${macoSrc}/account-manager/core/src/bin/grok_setup.rs" <<'PY'
import hashlib
import json
import pathlib
import stat
import sys
root = pathlib.Path(sys.argv[1])
def digest(path):
with path.open("rb") as source:
return hashlib.file_digest(source, "sha256").hexdigest()
for name in ("source-manifest.json", "share/maco/source-manifest.json", "share/maco/main-helper.json", "share/maco/grok-setup-helper.json"):
metadata = (root / name).lstat()
assert stat.S_ISREG(metadata.st_mode)
assert 0 < metadata.st_size <= 1024 * 1024
assert metadata.st_mode & 0o6022 == 0
binary = root / "bin/maco"
assert stat.S_ISREG(binary.lstat().st_mode)
with binary.open("rb") as source:
assert source.read(4) == b"\x7fELF"
declaration = json.loads((root / "share/maco/main-helper.json").read_text())
assert declaration == {
"schema": "maco-source-bound-main-helper-v1",
"binary_sha256": digest(binary),
"source_manifest_sha256": digest(root / "share/maco/source-manifest.json"),
"main_entry_sha256": digest(pathlib.Path(sys.argv[2])),
}
setup = root / "bin/cam-grok-setup"
metadata = setup.lstat()
assert stat.S_ISREG(metadata.st_mode) and metadata.st_mode & 0o111
assert metadata.st_mode & 0o6022 == 0
with setup.open("rb") as source:
assert source.read(4) == b"\x7fELF"
assert json.loads((root / "share/maco/grok-setup-helper.json").read_text()) == {
"schema": "maco-source-bound-grok-setup-helper-v1",
"binary_sha256": digest(setup),
"source_manifest_sha256": digest(root / "share/maco/source-manifest.json"),
"setup_entry_sha256": digest(pathlib.Path(sys.argv[3])),
}
PY
'' + lib.optionalString (!pkgs.stdenv.hostPlatform.isLinux) ''
test ! -e "$out/bin/account-authority"
test ! -e "$out/bin/cam-grok-setup"
test ! -e "$out/share/maco/grok-setup-helper.json"
test ! -e "$out/source-manifest.json"
'' + ''
runHook postInstallCheck
'';
postInstall = ''
rm -f "$out/bin/multi-agent-coding-orchestrator"
'' + lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
install -m 0444 "${sourceManifest}" "$out/source-manifest.json"
install -Dm 0444 "${sourceManifest}" "$out/share/maco/source-manifest.json"
'';
# Strip/patchelf must finish before binding the installed native image.
postFixup = lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
${pkgs.python3}/bin/python3 - "$out" "${macoSrc}/src/bin/maco.rs" "${macoSrc}/account-manager/core/src/bin/grok_setup.rs" <<'PY'
import hashlib
import json
import pathlib
import sys
root = pathlib.Path(sys.argv[1])
def digest(path):
with path.open("rb") as source:
return hashlib.file_digest(source, "sha256").hexdigest()
declaration = {
"schema": "maco-source-bound-main-helper-v1",
"binary_sha256": digest(root / "bin/maco"),
"source_manifest_sha256": digest(root / "share/maco/source-manifest.json"),
"main_entry_sha256": digest(pathlib.Path(sys.argv[2])),
}
path = root / "share/maco/main-helper.json"
path.write_text(json.dumps(declaration, sort_keys=True, separators=(",", ":")) + "\n")
path.chmod(0o444)
setup_declaration = {
"schema": "maco-source-bound-grok-setup-helper-v1",
"binary_sha256": digest(root / "bin/cam-grok-setup"),
"source_manifest_sha256": digest(root / "share/maco/source-manifest.json"),
"setup_entry_sha256": digest(pathlib.Path(sys.argv[3])),
}
path = root / "share/maco/grok-setup-helper.json"
path.write_text(json.dumps(setup_declaration, sort_keys=True, separators=(",", ":")) + "\n")
path.chmod(0o444)
PY
'';
meta = {
description = cargoToml.package.description;
homepage = cargoToml.package.repository;
license = lib.licenses.agpl3Plus;
mainProgram = "maco";
platforms = systems;
};
};
in
{
packages = forEachSystem (system:
let pkgs = pkgsFor system;
in rec {
maco = macoPackageFor system;
default = maco;
} // lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
runtime-test-helpers = pkgs.runCommand "maco-runtime-test-helpers" { } ''
mkdir -p "$out/bin"
ln -s ${pkgs.coreutils}/bin/env "$out/bin/env"
ln -s ${pkgs.bash}/bin/sh "$out/bin/sh"
ln -s ${pkgs.coreutils}/bin/sleep "$out/bin/sleep"
ln -s ${pkgs.coreutils}/bin/stat "$out/bin/stat"
ln -s ${lib.getBin pkgs.util-linux}/bin/findmnt "$out/bin/findmnt"
'';
});
apps = forEachSystem (system: rec {
maco = {
type = "app";
program = "${self.packages.${system}.maco}/bin/maco";
};
default = maco;
});
overlays.default = final: prev: {
maco = self.packages.${prev.stdenv.hostPlatform.system}.maco;
};
devShells = forEachSystem (system:
let
pkgs = pkgsFor system;
rustToolchain = rustToolchainFor pkgs;
# Hashes are for the pin versions in scripts/supply_chain_pins.toml.
# Refresh both hashes when those versions change.
cargo-audit = pkgs.rustPlatform.buildRustPackage {
pname = "cargo-audit";
version = supplyChainPins.cargo_audit;
src = pkgs.fetchCrate {
pname = "cargo-audit";
version = supplyChainPins.cargo_audit;
hash = "sha256-hrkkDRJvXe2fltWjEW2A0/uKVFWq+9O+wRphsJjT1tE=";
};
cargoHash = "sha256-pdFoawDRzJ8gPYAAQHwrCVYeaa1ShSqYA8nwpCAnS1s=";
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
pkgs.openssl
pkgs.zlib
];
buildFeatures = [ "fix" ];
doCheck = false;
};
cargo-deny = pkgs.rustPlatform.buildRustPackage {
pname = "cargo-deny";
version = supplyChainPins.cargo_deny;
src = pkgs.fetchFromGitHub {
owner = "EmbarkStudios";
repo = "cargo-deny";
tag = supplyChainPins.cargo_deny;
hash = "sha256-sYxRQvJVbVmzajGJdAHnuvJDELv0cyDCCU8cRU0U0oQ=";
};
cargoHash = "sha256-Zb6vQCnhhhL9Ducn9eh5P8Gfopl0lQPTXWW8Q0Y5xBQ=";
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.zstd ];
env.ZSTD_SYS_USE_PKG_CONFIG = true;
doCheck = false;
};
in
{
default = pkgs.mkShell {
packages =
[
rustToolchain
pkgs.python3
pkgs.git
cargo-audit
cargo-deny
]
++ lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.pkg-config
pkgs.dbus
];
RUST_BACKTRACE = "1";
};
});
};
}