forjar 1.6.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
Documentation
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
<p align="center">
  <img src="docs/hero.svg" alt="forjar — Rust-native Infrastructure as Code" width="900" />
</p>

<p align="center">
  <a href="#quick-start">Quick Start</a> &middot;
  <a href="docs/book/src/README.md">Book</a> &middot;
  <a href="docs/specifications/forjar-spec.md">Specification</a> &middot;
  <a href="https://github.com/paiml/forjar-cookbook">Cookbook</a> &middot;
  <a href="#benchmarks">Benchmarks</a>
</p>

[![CI](https://github.com/paiml/forjar/actions/workflows/ci.yml/badge.svg)](https://github.com/paiml/forjar/actions/workflows/ci.yml)
[![crates.io](https://img.shields.io/crates/v/forjar.svg)](https://crates.io/crates/forjar)
[![Documentation](https://docs.rs/forjar/badge.svg)](https://docs.rs/forjar)
[![MSRV](https://img.shields.io/badge/MSRV-1.89.0-blue)](https://blog.rust-lang.org/)

---

## Table of Contents

- [Features]#features
- [Why Forjar]#why-forjar
- [Quick Start]#quick-start
- [Configuration]#configuration
- [Content-Addressed Store]#content-addressed-store
- [How It Works]#how-it-works
- [Benchmarks]#benchmarks
- [Falsifiable Claims]#falsifiable-claims
- [Testing]#testing
- [Installation]#installation
- [Contributing]#contributing
- [License]#license

---

**Latest: v1.6.1 — on [crates.io](https://crates.io/crates/forjar).** Install via `cargo install forjar`, a prebuilt binary, or from source.

Forjar is a single-binary IaC tool written in Rust. It manages bare-metal machines over SSH using YAML configs, BLAKE3 content-addressed state, and deterministic DAG execution. No cloud APIs, no runtime dependencies, no remote state backends.

```
forjar.yaml  →  parse  →  resolve DAG  →  plan  →  codegen  →  execute  →  BLAKE3 lock
```

## Features

- Declarative YAML-based infrastructure provisioning
- Content-addressed artifact store (BLAKE3 hashing) with a 4-level purity model (Pure / Pinned / Constrained / Impure)
- SSH-based remote execution with automatic retry
- Drift detection and convergence verification
- Provable convergence contracts — `idempotent-apply`, `plan-apply-equivalence`, and `destroy-undo-roundtrip` (see `forjar contracts` / `forjar prove`)
- L0–L5 test coverage levels — `forjar test` reports per-resource coverage through L3 convergence, L4 mutation, and L5 preservation
- Distribution-artifact generation — `forjar dist` emits a shell installer, Homebrew formula, Nix flake, cargo-binstall metadata, deb, and rpm with real checksum resolution, plus `--verify` (static installer checks) and `--verify-containers` (gnu/musl container runs)
- age-encrypted state at rest — `forjar state-encrypt` / `state-decrypt` / `state-rekey`
- WASM resource plugins (opt-in `--features wasm-runtime`) via `forjar plugin`
- Pure Rust with zero C dependencies

## Why Forjar

| | Terraform | Ansible | **Forjar** |
|---|---|---|---|
| Runtime | Go + providers | Python + SSH | **Single Rust binary** |
| State | S3 / Consul / JSON | None | **Git (BLAKE3 YAML)** |
| Drift detection | API calls | None | **Local hash compare** |
| Bare metal | Weak | Strong | **First-class** |
| Dependencies | ~200 Go modules | ~50 Python pkgs | **~30 crates** |
| Apply speed | Seconds–minutes | Minutes | **Milliseconds–seconds** |

## Quick Start

```bash
# Install from crates.io
cargo install forjar

# ...or grab a prebuilt binary (picks the right target, verifies sha256)
curl -fsSL https://raw.githubusercontent.com/paiml/forjar/main/install.sh | sh

# ...or build from source: git clone … && cargo install --path .

# Initialize a project
forjar init my-infra && cd my-infra

# Edit forjar.yaml (see Configuration below)

# Preview changes
forjar plan -f forjar.yaml

# Apply
forjar apply -f forjar.yaml

# Check for unauthorized changes
forjar drift --state-dir state

# View current state
forjar status --state-dir state
```

## Configuration

A `forjar.yaml` declares machines, resources, and policy:

```yaml
version: "1.0"
name: home-lab
description: "Sovereign AI stack provisioning"

params:
  data_dir: /mnt/data

machines:
  gpu-box:
    hostname: lambda
    addr: 192.168.50.100
    user: noah
    ssh_key: ~/.ssh/id_ed25519
    arch: x86_64
    roles: [gpu-compute]

resources:
  base-packages:
    type: package
    machine: gpu-box
    provider: apt
    packages: [curl, htop, git, tmux, ripgrep]

  data-dir:
    type: file
    machine: gpu-box
    state: directory
    path: "{{params.data_dir}}"
    owner: noah
    mode: "0755"
    depends_on: [base-packages]

  app-config:
    type: file
    machine: gpu-box
    path: /etc/app/config.yaml
    content: |
      data_dir: {{params.data_dir}}
      log_level: info
    owner: noah
    mode: "0644"
    depends_on: [data-dir]

policy:
  failure: stop_on_first
  tripwire: true
  lock_file: true
```

### Resource Types

| Type | States | Key Fields |
|------|--------|------------|
| `package` | present, absent | `provider` (apt/cargo/uv), `packages` |
| `file` | file, directory, symlink, absent | `path`, `content`, `owner`, `group`, `mode` |
| `service` | running, stopped, enabled, disabled | `name`, `enabled`, `restart_on` |
| `mount` | mounted, unmounted, absent | `source`, `path`, `fstype`, `options` |
| `user` | present, absent | `name`, `groups`, `shell`, `home`, `ssh_keys` |
| `docker` | running, stopped, absent | `image`, `ports`, `environment`, `volumes` |
| `cron` | present, absent | `name`, `schedule`, `command`, `user` |
| `network` | present, absent | `port`, `protocol`, `action`, `from_addr` |
| `pepita` | present, absent | `name`, `cgroups`, `overlayfs`, `netns`, `seccomp` |
| `model` | present, absent | `name`, `source`, `format`, `quantization`, `checksum`, `cache_dir` |
| `gpu` | present, absent | `driver_version`, `cuda_version`, `devices`, `persistence_mode`, `compute_mode` |

### Templates

Use `{{params.key}}` to reference global parameters in any string field. Templates are resolved before codegen.

### Recipes

Reusable, parameterized resource patterns (like Homebrew formulae):

```yaml
# recipes/dev-tools.yaml
name: dev-tools
version: "1.0"
inputs:
  user:
    type: string
    required: true
  shell:
    type: enum
    values: [bash, zsh, fish]
    default: zsh
resources:
  packages:
    type: package
    provider: apt
    packages: [build-essential, cmake, pkg-config]
  dotfiles:
    type: file
    state: directory
    path: "/home/{{inputs.user}}/.config"
    owner: "{{inputs.user}}"
    mode: "0755"
```

See the **[Forjar Cookbook](https://github.com/paiml/forjar-cookbook)** for 67 production-ready recipes covering packages, files, services, Docker, GPU, network, pepita sandboxing, multi-machine stacks, and content-addressed store reproducibility. The cookbook includes a [Reproducibility Series](https://github.com/paiml/forjar-cookbook/blob/master/docs/book/src/recipes/reproducibility.md) (recipes 63-67) demonstrating version pinning, sandboxed builds, SSH caching, CI gates, and profile rollback.

## Content-Addressed Store

Forjar includes a Nix-inspired content-addressed store for reproducible builds. Every build output lives at a deterministic path derived from its inputs:

```
/var/lib/forjar/store/<blake3-hash>/
├── meta.yaml          # Input manifest, provenance
└── content/           # Build output
```

### Store Commands

```bash
forjar pin                            # Pin all inputs to current versions
forjar pin --check                    # CI gate — fail if lock file is stale
forjar cache list                     # List local store entries
forjar cache push user@host:path      # Push to SSH binary cache
forjar cache verify                   # Re-hash all entries
forjar store gc --dry-run             # Preview garbage collection
forjar store diff <hash>              # Diff against upstream origin
forjar store-import apt nginx=1.24.0  # Import from any provider
forjar archive pack <hash>            # Pack into .far archive
forjar convert --reproducible         # Auto-convert recipe to store model
```

Supported import providers: `apt`, `cargo`, `uv`, `nix`, `docker`, `tofu`, `terraform`, `apr`.

### 4-Level Purity Model

| Level | Name | Requirement |
|-------|------|-------------|
| 0 | Pure | Version + store + sandbox |
| 1 | Pinned | Version + store (no sandbox) |
| 2 | Constrained | Provider-scoped, floating version |
| 3 | Impure | Unconstrained |

See the [architecture docs](docs/book/src/05-architecture.md) for details on the store model, sandbox lifecycle, substitution protocol, and derivation executor.

## How It Works

1. **Parse** — Read `forjar.yaml`, validate schema and references
2. **Resolve** — Expand templates, build dependency DAG (Kahn's toposort, alphabetical tie-break)
3. **Plan** — Diff desired state against BLAKE3 lock file (hash comparison, no API calls)
4. **Codegen** — Generate shell scripts per resource type
5. **Execute** — Run scripts locally or via SSH (stdin pipe, not argument passing). Files > 1MB use copia delta sync (only changed blocks transferred)
6. **State** — Atomic lock file write (temp + rename), append to JSONL event log

### Failure Policy (Jidoka)

On first failure, execution stops immediately. Partial state is preserved in the lock file. No cascading damage. Re-run to continue from where it stopped.

### Transport

- **Local**: `bash` via stdin pipe (for `127.0.0.1` / `localhost`)
- **SSH**: `ssh -o BatchMode=yes` with stdin pipe (no argument length limits)

## Benchmarks

```bash
cargo bench
```

<!-- BENCH-TABLE-START -->

**Core Operations**

| Operation | Input | Mean | 95% CI |
|---|---|---|---|
| BLAKE3 hash | 64 B string | 27 ns | +/- 0.5 ns |
| BLAKE3 hash | 1 KB string | 92 ns | +/- 1.2 ns |
| BLAKE3 hash | 1 MB file | 172 us | +/- 0.4 us |
| YAML parse | 500 B config | 20.7 us | +/- 0.2 us |
| Topo sort | 100 nodes | 34.6 us | +/- 0.4 us |
| Copia signature | 1 MB file | 294 us | +/- 0.3 us |
| Copia signature | 4 MB file | 1.19 ms | +/- 0.01 ms |
| Copia delta | 4 MB, 2% change | 1.18 ms | +/- 0.01 ms |
| Copia patch gen | 1 MB, 10% change | 60 us | +/- 0.3 us |

**Store Operations** (`cargo bench --bench store_bench`)

| Operation | Input | Target |
|---|---|---|
| Store path hash | 3 inputs | < 1 us |
| Purity classify | 4 levels | < 1 us |
| Closure hash | 3/10/50 nodes | < 10 us |
| Repro score | 1/5/20 resources | < 100 us |
| FAR encode | 1KB/1MB/10MB | < 100 ms |
| FAR decode | 64KB manifest | < 10 ms |
| Lockfile staleness | 10/100/1K pins | < 1 ms |
| Sandbox validate | 4 presets | < 1 us |
| Derivation closure | 5-input DAG | < 10 us |
| Script purify | small/med/large | < 10 ms |

<!-- BENCH-TABLE-END -->

Criterion.rs, 100 samples, 3s warm-up. Run `make bench-update` to refresh table.

## Falsifiable Claims

<details>
<summary>10 testable claims with linked tests (click to expand)</summary>

### C1: Deterministic hashing
BLAKE3 of identical inputs always produces identical outputs.
Tests: `test_fj014_hash_file_deterministic`, `test_fj014_hash_string`

### C2: Deterministic DAG order
Same dependency graph always produces the same execution order.
Tests: `test_fj003_topo_sort_deterministic`, `test_fj003_alphabetical_tiebreak`

### C3: Idempotent apply
Second apply on unchanged config produces zero changes.
Tests: `test_fj012_idempotent_apply`, `test_fj004_plan_all_unchanged`

### C4: Cycle detection
Circular dependencies are rejected at parse time.
Tests: `test_fj003_cycle_detection`

### C5: Content-addressed state
Lock hashes are derived from desired state, not timestamps.
Tests: `test_fj004_hash_deterministic`, `test_fj004_plan_all_unchanged`

### C6: Atomic state persistence
Lock writes use temp file + rename. No corruption on crash.
Tests: `test_fj013_atomic_write`, `test_fj013_save_and_load`

### C7: Recipe input validation
Invalid typed inputs are rejected before expansion.
Tests: `test_fj019_validate_inputs_type_mismatch`, `test_fj019_validate_inputs_enum_invalid`

### C8: Heredoc injection safety
Single-quoted heredoc prevents shell expansion in file content.
Tests: `test_fj007_heredoc_safe`

### C9: Lean dependency set
30 direct runtime dependencies — 27 always-on plus 3 optional, feature-gated
(`age` for encryption, `wasmi` for WASM plugins, `dhat` for heap profiling) —
and 3 build dependencies. Single static binary output.
Verify (counts all runtime deps, including the optional ones):
`cargo metadata --no-deps --format-version 1 | jq '[.packages[0].dependencies[] | select(.kind == null)] | length'` → `30`
(build deps: `select(.kind == "build")` → `3`).

### C10: Jidoka failure isolation
First failure stops execution. Previously converged state is preserved. A failing `pre_apply`
gate hook fails apply (v1.6.0) and is not retried — it never silently passes.
Tests: `test_fj012_apply_local_file`

</details>

## Testing

```bash
cargo test                    # 12000+ unit tests
cargo test -- --nocapture     # with output
cargo test planner            # specific module
cargo bench                   # Criterion benchmarks
cargo clippy -- -D warnings   # lint
```

## Installation

**Prebuilt binaries** (Linux x86_64/aarch64, gnu + static musl — see
[Releases](https://github.com/paiml/forjar/releases)):

```bash
# One-liner installer (picks the right target, verifies sha256)
curl -fsSL https://raw.githubusercontent.com/paiml/forjar/main/install.sh | sh

# Or manually: download, verify, extract
VERSION=1.6.1 TARGET=x86_64-unknown-linux-gnu
curl -fsSLO "https://github.com/paiml/forjar/releases/download/v${VERSION}/forjar-${VERSION}-${TARGET}.tar.gz"
curl -fsSLO "https://github.com/paiml/forjar/releases/download/v${VERSION}/forjar-${VERSION}-${TARGET}.tar.gz.sha256"
sha256sum -c "forjar-${VERSION}-${TARGET}.tar.gz.sha256"
tar xzf "forjar-${VERSION}-${TARGET}.tar.gz"
sudo install "forjar-${VERSION}-${TARGET}/forjar" /usr/local/bin/

# Via cargo-binstall (uses the same release artifacts)
cargo binstall forjar
```

**From crates.io / source:**

```bash
# From crates.io
cargo install forjar

# From source
git clone https://github.com/paiml/forjar.git
cd forjar
cargo install --path .
```

## Contributing

Contributions are welcome. Please follow these guidelines:

1. Fork the repo and create your changes on `main`
2. Run the full quality gate before submitting: `make all`
3. Ensure `cargo test` passes with zero failures
4. Run `cargo clippy -- -D warnings` with no warnings
5. Add tests for new functionality (95% coverage minimum)
6. Submit a pull request with a clear description of the change

For bug reports, please include: forjar version (`forjar --version`), OS, and a minimal `forjar.yaml` that reproduces the issue.

## License

MIT OR Apache-2.0