Repository navigation
Share one treefmt-nix configuration across every repository #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
38a4dcc
675a7af
285d730
03d8b41
5b8c1fa
d6cd419
b992ab2
32ddd4b
d0a71b2
77eed0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| name: Validate formatter | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| validate: | ||
| name: Validate configuration | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| steps: | ||
| - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa # v23 | ||
| - uses: DeterminateSystems/magic-nix-cache-action@84c0677f58dcedf3b91f8223ce36a9ea5b3c84b7 # v15 | ||
| with: | ||
| use-gha-cache: true | ||
| use-flakehub: false | ||
| - name: Evaluate configuration on all supported systems | ||
| run: nix flake check --all-systems --no-build --no-update-lock-file --show-trace | ||
| - name: Build generated treefmt configuration | ||
| run: nix build --no-link --no-update-lock-file .#config | ||
|
|
||
| formatting: | ||
| name: Check formatting | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| steps: | ||
| - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa # v23 | ||
| - uses: DeterminateSystems/magic-nix-cache-action@84c0677f58dcedf3b91f8223ce36a9ea5b3c84b7 # v15 | ||
| with: | ||
| use-gha-cache: true | ||
| use-flakehub: false | ||
| - name: Check formatting | ||
| run: nix flake check --no-update-lock-file --show-trace |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ | |
| node_modules/ | ||
| __pycache__/ | ||
| .DS_Store | ||
| result | ||
| result-* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,122 +1,89 @@ | ||
| # Caido formatter | ||
|
|
||
| Shared treefmt configuration for Caido projects, with tools managed by mise. | ||
| One shared [treefmt-nix](https://github.com/numtide/treefmt-nix) configuration for every repository. Each language always uses the same formatter, version and settings. There are no project presets or local formatter settings to maintain. | ||
|
|
||
| ## Usage | ||
| ## Add to any project | ||
|
|
||
| Clone this private repository alongside your projects. From this checkout: | ||
| Use this identical `flake.nix` in every repository: | ||
|
|
||
| ```sh | ||
| mise install | ||
|
|
||
| # Format a project using its existing conventions. | ||
| mise exec -- bin/format proxy-frontend --root ../proxy-frontend | ||
| mise exec -- bin/format proxy-backend --root ../proxy-backend | ||
|
|
||
| # Desktop uses an older Prettier version; the mise environment preserves it. | ||
| mise --env proxy-desktop exec -- bin/format proxy-desktop --root ../proxy-desktop | ||
|
|
||
| # CI: fail if formatting changes a file. | ||
| mise exec -- bin/format proxy-backend --root ../proxy-backend --ci | ||
| ```nix | ||
| { | ||
| inputs.caido-formatter.url = "git+ssh://git@github.com/caido/formatter"; | ||
|
|
||
| # Format selected files. Paths are relative to the target root. | ||
| mise exec -- bin/format proxy-frontend --root ../proxy-frontend packages/ui/src/App.vue | ||
| outputs = { self, caido-formatter, ... }: | ||
| caido-formatter.lib.forProject self; | ||
| } | ||
| ``` | ||
|
|
||
| `treefmt --ci` **can rewrite files** before returning a failure. Run it in a CI checkout or on a clean working tree. Use the formatter's own check command when a strictly read-only check is required. | ||
| Then run: | ||
|
|
||
| When tools are already supplied by a project's mise environment, install treefmt there and invoke `/path/to/formatter/bin/format <profile>` from the project root. The script uses executables on `PATH`; it does not install tools, change their versions, or load environment files itself. Use the version overrides in `mise.<profile>.toml` when running from this checkout. | ||
|
|
||
| ## Shared defaults | ||
|
|
||
| `treefmt.toml` contains entries for every formatter family found in the audit: | ||
|
|
||
| | Formatter | Files | Default selection | | ||
| | --------- | -------------------------------------------------------------------------- | ---------------------------------------------- | | ||
| | Oxfmt | TypeScript, JavaScript, Vue, HTML, CSS/SCSS, JSON, GraphQL, Markdown, YAML | Yes | | ||
| | rustfmt | Rust, using nightly-2026-04-06 and edition 2024 | Yes | | ||
| | Ruff | Python, stubs, notebooks | Yes | | ||
| | OpenTofu | Terraform/OpenTofu configuration and variable files | Yes | | ||
| | shfmt | Shell files with `.sh`, `.bash`, or `.bats` extensions | Yes | | ||
| | StyLua | Lua and Luau | Yes | | ||
| | Prettier | Web code and text formats | Alternative to Oxfmt | | ||
| | Deno | JavaScript, TypeScript, JSON, Markdown | Alternative to Oxfmt | | ||
| | nixfmt | Nix | Optional; installed in backend Nix shells | | ||
| | SQLFluff | PostgreSQL SQL | Optional; configuration found in cloud-backend | | ||
| | Black | Python and stubs | Legacy; found in archived projects | | ||
| ```sh | ||
| nix fmt | ||
| nix flake check | ||
| ``` | ||
|
|
||
| Use `bin/format default` for the shared defaults. This is an opt-in organization baseline, not a claim that every existing project already follows it. The Rust baseline assumes edition 2024. Use the project presets for audited differences. | ||
| The shared helper checks **your repository's source**. It exports only a `formatting` check and a `formatting-check` package. It checks the consuming repository rather than the library's source. An existing flake can merge these outputs with its other outputs; no formatter-specific settings are required. | ||
|
|
||
| The alternative entries are disabled in the default selection to prevent two tools from reformatting the same files. Select an optional engine explicitly, for example: | ||
| `nix flake check` runs all entries under `checks`; it has no check-name filter. If your flake adds other checks, run formatting alone with: | ||
|
|
||
| ```sh | ||
| mise exec -- bin/format default --root ../infrastructure --formatters tofu | ||
| bin/format nix --root ../nix-config | ||
| bin/format postgres --root ../cloud-backend | ||
| nix build --no-link .#formatting-check | ||
| ``` | ||
|
|
||
| Deno, nixfmt, SQLFluff, and legacy Black are not installed by the base mise configuration. Supply them through the target project's environment, or explicitly install a chosen version. The audit did not establish a version pin for all of these tools. Never use a PostgreSQL SQL preset on dbt or templated SQL without checking compatibility. | ||
|
|
||
| ## Project presets | ||
| This check formats a disposable source copy and leaves your working tree unchanged. Use `--no-update-lock-file` with these commands in CI to reject missing or stale lockfiles. | ||
|
|
||
| `profiles/` contains complete treefmt files; treefmt does not merge them with the root config. | ||
| ## The shared configuration | ||
|
|
||
| | Preset | Behavior retained | | ||
| | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------- | | ||
| | `proxy-backend` | Nightly Rust, edition 2024, macro exclusion; Prettier GraphQL at width 100 | | ||
| | `proxy-frontend` | Oxfmt at width 80 with grouped/sorted imports and generated-file exclusions | | ||
| | `proxy-e2e` | Oxfmt for TS/JS/MJS; excludes plugins, binaries, and Playwright output | | ||
| | `proxy-desktop` | Prettier defaults on the four source packages currently covered by lint tasks | | ||
| | `proxy-playground`, `proxy-extension` | Prettier formatting; existing ESLint checks remain separate | | ||
| | `cloud-backend`, `sync-backend`, `student-backend`, `sqlite-rs`, `cogfer`, `convert` | Existing Rust style settings; edition selected per project | | ||
| | `internal-services` | Oxfmt with shared import groups | | ||
| | `analytics` | Ruff on the four Python app trees, excluding dbt's transform app | | ||
| | `caido-private` | Ruff for Python 3.11, limited to scripts and tests | | ||
| | `infrastructure` | OpenTofu files under `tofu/` | | ||
| | `infrastructure-yaml` | Existing single-quoted YAML convention, separately opt-in | | ||
| | `mise-toolshed` | Oxfmt, shfmt and StyLua; excludes generated mise locks | | ||
| | `secrets-sync` | Deno formatting, using the project's Deno configuration | | ||
| | `electron-updater` | Existing Prettier width 180, no semicolons, ES5 trailing commas | | ||
| | `analytics-ingestion`, `cloud-cron` | Legacy Black presets for archived projects | | ||
| | `nix`, `postgres` | Optional infrastructure/database formatting | | ||
| `treefmt.nix` selects formatter packages, file patterns and generic exclusions. Formatter-specific settings live in their native files: | ||
|
|
||
| The presets centralize formatter style. They do not execute ESLint, Clippy, Ruff lint fixes, typechecking, dbt validation, or infrastructure validation. Keep those checks in the source repositories. Desktop's import ordering is an ESLint rule and is not replaced by running Prettier alone. | ||
| ```text | ||
| configs/ | ||
| oxfmt.json | ||
| ruff.toml | ||
| rustfmt.toml | ||
| stylua.toml | ||
| sqlfluff.ini | ||
| ``` | ||
|
|
||
| Rust files are passed directly to rustfmt with `skip_children=true`, so formatting one file cannot recursively modify excluded modules. Presets pin the nightly from proxy-backend; projects using floating nightly will need to review that stabilization. Cargo workspaces containing multiple editions need separate formatter entries or a Cargo-aware wrapper. | ||
| Shell flags stay in `treefmt.nix`; nixfmt and OpenTofu use their defaults. Every consuming repository shares these same files. | ||
|
|
||
| `configs/` stores settings that are clearer as companion files, notably Oxfmt import groups. `bin/format` resolves these relative to this checkout through `CAIDO_FORMATTER_ROOT`, even when the target project or shared checkout path contains spaces. Keep the entire checkout available. For direct treefmt use, export that variable first: | ||
| The language mapping is: | ||
|
|
||
| ```sh | ||
| export CAIDO_FORMATTER_ROOT=/absolute/path/to/formatter | ||
| treefmt --config-file "$CAIDO_FORMATTER_ROOT/profiles/proxy-backend.toml" | ||
| ``` | ||
| | Files | Formatter | | ||
| | --------------------------------------------------------------------- | ---------------------------- | | ||
| | JavaScript, TypeScript, Vue, HTML, CSS, JSON, GraphQL, Markdown, YAML | Oxfmt | | ||
| | Python | Ruff | | ||
| | Rust | rustfmt, pinned nightly | | ||
| | Terraform/OpenTofu | OpenTofu | | ||
| | Shell | shfmt | | ||
| | Lua/Luau | StyLua | | ||
| | Nix | nixfmt | | ||
| | SQL | SQLFluff, PostgreSQL dialect | | ||
|
|
||
| ## Mise integration in a consuming project | ||
| There is one set of language globs and generic dependency/generated-file exclusions. Existing per-repository formatter config files do not select alternate formatting settings. Prettier, Deno and Black are not alternative engines in this setup. | ||
|
|
||
| Pin this repository to a reviewed commit or release, then add tasks that point at the checkout. For example, when the shared checkout is a sibling: | ||
| All Rust files use edition 2024 and the shared 2024 style. Projects must update their `Cargo.toml` edition and migrate any incompatible code before adopting the shared formatter. Python formatting targets Python 3.11 compatibility everywhere. | ||
|
|
||
| ```toml | ||
| [tasks.format] | ||
| run = 'mise -C ../formatter exec -- bin/format proxy-frontend --root "$MISE_PROJECT_ROOT"' | ||
| Nix parses the JSON/TOML settings during evaluation and generates the tool configuration files and treefmt TOML in the Nix store. SQLFluff uses the shared INI file directly from the store. Nixpkgs and rust-overlay are pinned in `flake.lock`, so formatter binaries do not come from a project's PATH. Update the shared input's lockfile pin to roll out a new version of this configuration. | ||
|
|
||
| [tasks."format:check"] | ||
| run = 'mise -C ../formatter exec -- bin/format proxy-frontend --root "$MISE_PROJECT_ROOT" --ci' | ||
| ``` | ||
| ## Develop this library | ||
|
|
||
| This repository does not change or migrate consuming projects automatically. Review tool versions, nested configs, generated paths, and the initial formatting diff when adopting a preset. In particular, extensionless shell scripts need explicit project globs; the default shell entry only matches known extensions. | ||
| Configuration validation uses JSON/TOML parsing, treefmt-nix’s module option types and config generation. The SQLFluff INI file is passed through without Nix-side parsing. Formatter-specific settings passed through as data or arguments may still be rejected at runtime; there is no custom formatter test suite. | ||
|
|
||
| ## Maintenance | ||
| The [validation workflow](.github/workflows/validate.yml) runs on pull requests and pushes to `main`. It uses the Determinate Nix installer and Magic Nix Cache backed by GitHub Actions. One job evaluates all supported systems and builds the generated treefmt configuration on Linux. A separate job checks repository formatting with the committed lockfile, so formatting has its own CI result. | ||
|
|
||
| The [audit report](docs/audit.md) records the scope and findings. [The inventory](docs/inventory.json) pins source commits and lists matching config/task paths for all 169 repositories scanned on 2026-10-28. Forks and archived projects are identified separately; their conventions are not silently promoted to shared defaults. | ||
| ```sh | ||
| # Same formatter and formatting-only check used by every consumer. | ||
| nix fmt --no-update-lock-file | ||
| nix flake check --no-update-lock-file | ||
|
|
||
| [A GitHub Actions workflow](examples/github-actions.yml) is included as an example. To activate CI, copy it to `.github/workflows/validate.yml` using a GitHub connection with workflow write permission. The connection used to initialize this repository could upload contents but could not activate workflows. | ||
| # Evaluate module options and flake outputs on all supported systems without running checks. | ||
| nix flake check --all-systems --no-build --no-update-lock-file | ||
|
|
||
| ```sh | ||
| mise test | ||
| mise format | ||
| mise format:check | ||
| # Inspect the generated shared treefmt configuration. | ||
| cat "$(nix build --no-link --print-out-paths .#config)" | ||
| ``` | ||
|
|
||
| Tests exercise treefmt's real routing and shell invocation with stub formatter executables, including path handling, exclusions, competing engines, and CI failure behavior. Optional formatter engines must also be checked against their real pinned CLI before rollout. | ||
| Requires Nix or Lix with `nix-command` and `flakes` enabled. Supported systems are Apple Silicon macOS and ARM64/x86_64 Linux. Private Git access is needed to fetch the shared input. The same minimal consumer flake is available in `templates/default`. | ||
|
|
||
| Initial validation also ran the real Oxfmt 0.53.0, Prettier 3.7.4, nightly-2026-04-06 rustfmt, and OpenTofu 1.9.1 against scratch projects. All Rust presets accepted their configs; the infrastructure preset successfully formatted multiple files in one batch. | ||
| [ADR 0001](docs/adrs/0001-evaluate-treefmt-nix-in-formatter.md) records the proposed tooling experiment. | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| line-length = 88 | ||
| target-version = "py312" | ||
| target-version = "py311" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,7 @@ | ||
| unstable_features = true | ||
|
|
||
| edition = "2024" | ||
|
|
||
| # Currently rustfmt gives up on long strings | ||
| # See https://github.com/rust-lang/rustfmt/issues/3863 | ||
| single_line_if_else_max_width = 80 | ||
| style_edition = "2024" | ||
| group_imports = "StdExternalCrate" | ||
| imports_granularity = "Module" | ||
| error_on_line_overflow = false # We have a couple case where this fails | ||
| error_on_unformatted = false | ||
| single_line_if_else_max_width = 80 | ||
| skip_children = true |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the
formatteroutput.lib.forProjectalso exportsformatter. An existing flake that merges these outputs can overwrite its currentformatterand change whatnix fmtruns. List that output and note the potential collision.flake.nixLines 45–55 defines all three outputs.