From 2adefa7d95b36e02d64e73daec2efaafa0239b4d Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Tue, 29 Sep 2026 12:50:00 -0400 Subject: [PATCH] feat(flake): add teefmt and format *.nix --- README.md | 8 ++-- default.nix | 112 +++++++++++++++++++++++++++----------------- flake.lock | 21 +++++++++ flake.nix | 11 ++++- flake/default.nix | 1 + flake/devShells.nix | 33 ++++++++----- flake/packages.nix | 2 +- flake/treefmt.nix | 13 +++++ 8 files changed, 139 insertions(+), 62 deletions(-) create mode 100644 flake/treefmt.nix diff --git a/README.md b/README.md index b9ed91a..eb9708c 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,20 @@ images for a final review pass. needs cargo. native/render/install.sh # then enable `view` in /extensions ``` -formatting (Gleam and Python) +formatting ```sh +nix fmt # treefmt: Nix, Gleam, Go, Python, and shell files pre-commit install # once per checkout; available in `nix develop` -pre-commit run --all-files # format the whole repository +pre-commit run --all-files # run the Gleam and Python hooks gleam format src test # fix Gleam formatting ruff format priv/python test # fix Python formatting ``` The commit hook formats staged Gleam and Python files (and asks you to re-stage any changes). It requires `gleam` on PATH; pre-commit installs the pinned Ruff -hook in its own environment. +hook in its own environment. Treefmt uses the Gleam and Ruff versions from +nixpkgs; the pre-commit Ruff hook remains pinned separately. tests diff --git a/default.nix b/default.nix index e07a84a..46e8d55 100644 --- a/default.nix +++ b/default.nix @@ -1,28 +1,50 @@ -{ lib, stdenv, rustPlatform, buildGoModule, fetchurl, gleam, beamPackages, python3, bash, coreutils -, makeWrapper, zig, usage-core }: -let - erlang = beamPackages.erlang; +{ + lib, + stdenv, + rustPlatform, + buildGoModule, + fetchurl, + gleam, + beamPackages, + python3, + bash, + coreutils, + makeWrapper, + zig, + usage-core, +}: let + inherit (beamPackages) erlang; # esqlite loads the pc plugin during its Rebar3 build, which cannot fetch # plugins from Hex inside the Nix sandbox. - rebar3 = beamPackages.rebar3WithPlugins { plugins = [ beamPackages.pc ]; }; + rebar3 = beamPackages.rebar3WithPlugins {plugins = [beamPackages.pc];}; manifest = builtins.fromTOML (builtins.readFile ./manifest.toml); source = lib.fileset.toSource { root = ./.; fileset = lib.fileset.unions [ - ./gleam.toml ./manifest.toml ./src + ./gleam.toml + ./manifest.toml + ./src (lib.fileset.fileFilter (file: !file.hasExt "pyc") ./priv) - ./cli/go.mod ./cli/go.sum ./cli/cmd ./cli/internal + ./cli/go.mod + ./cli/go.sum + ./cli/cmd + ./cli/internal ]; }; - hexPackages = map (p: { - inherit (p) name version; - archive = fetchurl { - url = "https://repo.hex.pm/tarballs/${p.name}-${p.version}.tar"; - sha256 = lib.toLower p.outer_checksum; - }; - }) manifest.packages; - packageIndex = builtins.toFile "packages.toml" ("[packages]\n" + lib.concatMapStrings - (p: "${p.name} = \"${p.version}\"\n") hexPackages + "\n[git]\n"); + hexPackages = + map (p: { + inherit (p) name version; + archive = fetchurl { + url = "https://repo.hex.pm/tarballs/${p.name}-${p.version}.tar"; + sha256 = lib.toLower p.outer_checksum; + }; + }) + manifest.packages; + packageIndex = builtins.toFile "packages.toml" ("[packages]\n" + + lib.concatMapStrings + (p: "${p.name} = \"${p.version}\"\n") + hexPackages + + "\n[git]\n"); renderSource = lib.fileset.toSource { root = ./native/render; fileset = lib.fileset.unions [ @@ -60,7 +82,7 @@ let runHook postUnpack ''; sourceRoot = "provide-usage-main"; - nativeBuildInputs = [ zig ]; + nativeBuildInputs = [zig]; # The build.zig ranlib step hardcodes zig-out, so the default prefix it is # (no --prefix): artifacts are copied out in installPhase instead. buildPhase = '' @@ -85,16 +107,17 @@ let pname = "albedo-daemon"; version = "1.0.0"; src = source; - nativeBuildInputs = [ gleam erlang rebar3 makeWrapper ]; + nativeBuildInputs = [gleam erlang rebar3 makeWrapper]; buildPhase = '' runHook preBuild export HOME="$TMPDIR/home" mkdir -p "$HOME" build/packages ${lib.concatMapStringsSep "\n" (p: '' - mkdir -p build/packages/${p.name} "$TMPDIR/hex-${p.name}" - tar -xf ${p.archive} -C "$TMPDIR/hex-${p.name}" - tar -xzf "$TMPDIR/hex-${p.name}/contents.tar.gz" -C build/packages/${p.name} - '') hexPackages} + mkdir -p build/packages/${p.name} "$TMPDIR/hex-${p.name}" + tar -xf ${p.archive} -C "$TMPDIR/hex-${p.name}" + tar -xzf "$TMPDIR/hex-${p.name}/contents.tar.gz" -C build/packages/${p.name} + '') + hexPackages} cp ${packageIndex} build/packages/packages.toml chmod -R u+w build/packages gleam export erlang-shipment @@ -109,28 +132,29 @@ let ln -s ${usageCore}/bin/usage $out/lib/albedo/albedo/priv/bin/usage makeWrapper $out/lib/albedo/entrypoint.sh $out/bin/albedo-daemon \ --add-flags run \ - --prefix PATH : ${lib.makeBinPath [ erlang python3 bash coreutils render ]} + --prefix PATH : ${lib.makeBinPath [erlang python3 bash coreutils render]} runHook postInstall ''; }; -in buildGoModule { - pname = "albedo"; - version = "1.0.0"; - src = source; - modRoot = "cli"; - subPackages = [ "cmd/albedo" ]; - vendorHash = "sha256-enSzYyW7Ku9JjLeRuzfBZsUgdAYb4P7vFGZkvPDZGSo="; - nativeBuildInputs = [ makeWrapper python3 ]; - ALBEDO_NO_BROWSER = "1"; - postInstall = '' - wrapProgram $out/bin/albedo \ - --set-default ALBEDO_DAEMON ${daemon}/bin/albedo-daemon - ''; - passthru = { inherit daemon render usageCore; }; - meta = { - description = "coding agent daemon with a Charm terminal client"; - license = lib.licenses.wtfpl; - mainProgram = "albedo"; - platforms = lib.platforms.unix; - }; -} +in + buildGoModule { + pname = "albedo"; + version = "1.0.0"; + src = source; + modRoot = "cli"; + subPackages = ["cmd/albedo"]; + vendorHash = "sha256-enSzYyW7Ku9JjLeRuzfBZsUgdAYb4P7vFGZkvPDZGSo="; + nativeBuildInputs = [makeWrapper python3]; + ALBEDO_NO_BROWSER = "1"; + postInstall = '' + wrapProgram $out/bin/albedo \ + --set-default ALBEDO_DAEMON ${daemon}/bin/albedo-daemon + ''; + passthru = {inherit daemon render usageCore;}; + meta = { + description = "coding agent daemon with a Charm terminal client"; + license = lib.licenses.wtfpl; + mainProgram = "albedo"; + platforms = lib.platforms.unix; + }; + } diff --git a/flake.lock b/flake.lock index 5869f21..f9f0bf0 100644 --- a/flake.lock +++ b/flake.lock @@ -53,9 +53,30 @@ "inputs": { "flake-parts": "flake-parts", "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix", "usage-core": "usage-core" } }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1786901030, + "narHash": "sha256-WSFCsDSE5ffgD2MqzkM2CYjeFiKhRF/dJUN8uedb6YE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "27b3b12a8e6375f28ebe122f07d230ca5459bbfa", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + }, "usage-core": { "flake": false, "locked": { diff --git a/flake.nix b/flake.nix index fa941d8..68ab181 100644 --- a/flake.nix +++ b/flake.nix @@ -4,6 +4,12 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-parts.url = "github:hercules-ci/flake-parts"; + + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + # Pinned Zig usage CLI consumed by the daemon as a non-flake archive. usage-core = { url = "https://api.next.tangled.org/xrpc/org.tangled.temp.git.getArchive?repo=did%3Aplc%3A2lf7buutfnfcucljnmaypf7u&ref=1a4bff9&format=tar.gz&prefix=provide-usage-main"; @@ -19,6 +25,9 @@ "x86_64-linux" ]; - imports = [./flake]; + imports = [ + ./flake + inputs.treefmt-nix.flakeModule + ]; }; } diff --git a/flake/default.nix b/flake/default.nix index 5d1d330..16ebfaa 100644 --- a/flake/default.nix +++ b/flake/default.nix @@ -2,5 +2,6 @@ imports = [ ./devShells.nix ./packages.nix + ./treefmt.nix ]; } diff --git a/flake/devShells.nix b/flake/devShells.nix index 5eba419..f4c0cab 100644 --- a/flake/devShells.nix +++ b/flake/devShells.nix @@ -1,18 +1,25 @@ _: { - perSystem = {pkgs, ...}: { + perSystem = { + config, + lib, + pkgs, + ... + }: { devShells.default = pkgs.mkShell { - packages = with pkgs; [ - go - gopls - gleam - beamPackages.erlang - (beamPackages.rebar3WithPlugins {plugins = [beamPackages.pc];}) - python3 - pre-commit - ruff - cargo - rustc - ]; + packages = + (with pkgs; [ + go + gopls + gleam + beamPackages.erlang + (beamPackages.rebar3WithPlugins {plugins = [beamPackages.pc];}) + python3 + pre-commit + ruff + cargo + rustc + ]) + ++ lib.attrValues config.treefmt.build.programs; }; }; } diff --git a/flake/packages.nix b/flake/packages.nix index f0fb912..cdd8d6d 100644 --- a/flake/packages.nix +++ b/flake/packages.nix @@ -1,7 +1,7 @@ {inputs, ...}: { perSystem = {pkgs, ...}: { packages = rec { - albedo = pkgs.callPackage ../default.nix {usage-core = inputs.usage-core;}; + albedo = pkgs.callPackage ../default.nix {inherit (inputs) usage-core;}; inherit (albedo) daemon render usageCore; default = albedo; }; diff --git a/flake/treefmt.nix b/flake/treefmt.nix new file mode 100644 index 0000000..0dd9003 --- /dev/null +++ b/flake/treefmt.nix @@ -0,0 +1,13 @@ +_: { + perSystem = _: { + treefmt.config.programs = { + alejandra.enable = true; + deadnix.enable = true; + gleam.enable = true; + gofmt.enable = true; + ruff-format.enable = true; + shfmt.enable = true; + statix.enable = true; + }; + }; +} -- knotmirror 0.0.1