Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
197 lines (188 loc) · 10.9 KB
/
Copy pathflake.nix
File metadata and controls
197 lines (188 loc) · 10.9 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
{
description = "Engram — pinned dev toolchain (Rust + just + jq + sqlx-cli + cargo-watch + cargo-nextest).";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
# Reads rust-toolchain.toml so the flake stays in lockstep with
# the file rustup picks up. Bumping Rust = edit rust-toolchain.toml,
# the flake follows.
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
in {
devShells.default = pkgs.mkShell {
# Per-project tooling. System services (Postgres, Docker daemon)
# and privileged binaries (Firecracker) live outside the shell —
# see README "Validating the production path".
packages = with pkgs; [
rustToolchain
just
tilt # `just dev` orchestrator (ADR 0024).
# One engine on every host: macOS via
# nix develop or brew, the Linux dev-vm
# via nix develop. Replaces the bespoke
# integration-up.sh path.
jq
postgresql # psql + pg_dump; not the server
sqlx-cli
cargo-watch
cargo-nextest # parallel test runner; `just check`
# uses it for ~3-5× speedup over
# `cargo test --workspace`.
cargo-hakari # workspace-hack feature-unifier.
# See `workspace-hack/` and
# `.config/hakari.toml`. `cargo
# hakari verify` runs in CI to
# keep the union current.
pkg-config
openssl
protobuf # protoc, for tonic-build when grpc lands
llvmPackages.libclang # bindgen for userfaultfd-sys (Linux only,
# but harmless on macOS)
nodejs_26 # web SPA dev server (`just web` -> vite).
# Keep this major in step with the web CI
# lane (.github/workflows/ci.yml) and
# docker/web.Dockerfile — see the note
# beside `setup-node` in that workflow.
pnpm # workspace package manager for web/
# ADR 0055 P2: mksquashfs packs an uploaded skill dir into a
# content-addressed RO squashfs at registration (the coordinator's
# `skill_pack`); also what the deploy/bundles/* recipes already use.
# Needed by `nix develop` (the coordinator + `just check`'s pack test)
# and bundled into the coordinator runtime image.
squashfsTools
] ++ lib.optionals stdenv.isLinux [
# Parallel linker; wired in via the `shellHook` below
# (CARGO_TARGET_*_UNKNOWN_LINUX_GNU_RUSTFLAGS). Cuts
# incremental link time on this multi-binary workspace
# by 3–10× vs. GNU ld — engram ships ~10 binaries
# (coord/host-agent/agentd/uffd-handler/cli/...) so the
# savings compound on every edit-rebuild. macOS is not
# wired: Apple's ld is already fast and lld's Mach-O
# support is fragile.
mold
] ++ lib.optionals stdenv.isDarwin [
libiconv # required by some macOS-aarch64 crates
# `just bake-demo` cross-compiles the in-guest musl binaries
# (agentd, harness-claude); the cross stdenvs ship `<target>-cc`
# linkers wired below in the shellHook (.cargo/config.toml hard-codes
# the brew binary names, so we override via CARGO_TARGET_*_LINKER).
pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc
pkgs.pkgsCross.musl64.stdenv.cc
];
# OPENSSL_DIR / PKG_CONFIG_PATH so `cargo build` finds the
# Nix-provided openssl instead of looking system-wide.
# LIBCLANG_PATH points bindgen at the Nix-provided libclang;
# without it, `userfaultfd-sys`'s build script can't find
# libLLVM under the Nix loader.
env = {
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
};
# The cc-wrapper's fortify hardening adds -D_FORTIFY_SOURCE to every
# C compile. Debug builds compile C at -O0, so glibc then emits
# "_FORTIFY_SOURCE requires compiling with optimization". jemalloc's
# configure probes compile with -Werror, so every probe fails and
# configure stops ("cannot determine return type of strerror_r").
# This shell only makes development builds; release images are
# built outside it.
hardeningDisable = [ "fortify" ];
shellHook = ''
# Insulate cargo from the host's rustup installation.
#
# cargo searches for external subcommands (cargo-clippy,
# cargo-fmt, …) in $CARGO_HOME/bin *before* PATH. If the
# host has rustup, ~/.cargo/bin holds host-built plugins
# that get preferred over the nix toolchain's, even
# inside `nix develop`. On systems where the host glibc
# is older than nix's (e.g. Ubuntu 22.04 host glibc 2.35
# vs. nix glibc 2.42), those host plugins fail to dlopen
# nix-built proc-macro .so files with `version GLIBC_2.X
# not found`.
#
# Fix: point CARGO_HOME at a project-local directory so
# bin/ is empty → cargo falls through to PATH and picks
# nix's plugins. Symlink registry/ + git/ to the host's
# ~/.cargo so we share the crates.io index and avoid a
# cold re-download on first entry.
export CARGO_HOME="$PWD/.nix/cargo"
mkdir -p "$CARGO_HOME"
if [ -d "$HOME/.cargo" ]; then
for d in registry git; do
if [ ! -e "$CARGO_HOME/$d" ] && [ -d "$HOME/.cargo/$d" ]; then
ln -s "$HOME/.cargo/$d" "$CARGO_HOME/$d"
fi
done
fi
echo "engram dev shell — rustc $(rustc --version | awk '{print $2}'), just $(just --version | awk '{print $2}'), fc=system, CARGO_HOME=$CARGO_HOME"
# Linker selection for native Linux targets: tell rustc
# to pass `-fuse-ld=mold` to the C-compiler driver. gcc
# ≥12 (Ubuntu 22.04+) and clang ≥12 both honor this, and
# mold itself comes from the nix shell above. macOS skips
# this entirely — Apple's `ld` is fast and lld's Mach-O
# support is fragile. CI runners install mold via the
# `rui314/setup-mold` action; this hook only wires up the
# `nix develop` shell.
if [ "$(uname -s)" = "Linux" ]; then
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-fuse-ld=mold"
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-fuse-ld=mold"
fi
# macOS cross-compile to the musl guest targets (`just bake-demo`).
# `.cargo/config.toml` names the brew linkers (aarch64-linux-musl-gcc);
# point cargo + the `cc` crate (ring's C/asm) at the nix cross
# toolchains instead, so no Homebrew is needed. Env overrides
# `.cargo/config.toml`'s `linker=`; the `relocation-model=static`
# rustflags there still apply.
if [ "$(uname -s)" = "Darwin" ]; then
ARM64_CC="${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc}/bin/aarch64-unknown-linux-musl-cc"
X86_CC="${pkgs.pkgsCross.musl64.stdenv.cc}/bin/x86_64-unknown-linux-musl-cc"
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$ARM64_CC"
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER="$X86_CC"
export CC_aarch64_unknown_linux_musl="$ARM64_CC"
export CC_x86_64_unknown_linux_musl="$X86_CC"
export AR_aarch64_unknown_linux_musl="${pkgs.pkgsCross.aarch64-multiplatform-musl.stdenv.cc}/bin/aarch64-unknown-linux-musl-ar"
export AR_x86_64_unknown_linux_musl="${pkgs.pkgsCross.musl64.stdenv.cc}/bin/x86_64-unknown-linux-musl-ar"
# The cross stdenvs also export GENERIC `CC`, `CXX`, `AR`,
# `RANLIB` and `LD` naming musl tools, and cc-rs falls back
# to those for host (apple-darwin) C: ring's asm for a
# proc-macro, onig_sys's static archive for the TUI. Pin the
# host target and the generics to the nix clang wrapper; the
# per-target variables above keep the musl targets on the
# cross tools.
for host in aarch64_apple_darwin x86_64_apple_darwin; do
export "CC_$host=$(command -v cc)"
export "CXX_$host=$(command -v c++)"
export "AR_$host=$(command -v ar)"
export "RANLIB_$host=$(command -v ranlib)"
done
export CC="$(command -v cc)" CXX="$(command -v c++)" AR="$(command -v ar)"
export RANLIB="$(command -v ranlib)" LD="$(command -v ld)"
# The musl cross sysroots ship libc but NOT the Linux kernel
# UAPI headers, so a kernel-header bindgen crate (userfaultfd-sys,
# and the C `cc` step) can't find <linux/userfaultfd.h> when you
# cross-check the Linux-gated crates from macOS. Point clang
# (bindgen) and the `cc` crate at the cross targets' kernel
# headers. This is what makes `cargo {check,clippy} --target
# aarch64-unknown-linux-musl` work in `nix develop` for
# engram-{host-agent,uffd-handler,sandbox-firecracker}.
export BINDGEN_EXTRA_CLANG_ARGS="''${BINDGEN_EXTRA_CLANG_ARGS:+$BINDGEN_EXTRA_CLANG_ARGS }-I${pkgs.pkgsCross.aarch64-multiplatform-musl.linuxHeaders}/include"
export CFLAGS_aarch64_unknown_linux_musl="-I${pkgs.pkgsCross.aarch64-multiplatform-musl.linuxHeaders}/include"
export CFLAGS_x86_64_unknown_linux_musl="-I${pkgs.pkgsCross.musl64.linuxHeaders}/include"
fi
'';
};
formatter = pkgs.nixpkgs-fmt;
});
}