From 1a134b005f1d5321b56272423e49eadb8474a6a3 Mon Sep 17 00:00:00 2001 From: Kai Giokas Date: Sun, 19 Jul 2026 10:13:22 +0400 Subject: [PATCH] Add self package to flake.nix, nix fixes, fmt * Added a `default`/`clawscli` package that builds this project. Note, `vendorHash` has to be set when the dependencies change. * Uses the same flags as in the `.goreleaser.yml` * Uses `self` for automatic version options using the hash of the current git revision. Uses the `dirtyShortRev` if it is available so that builds and versions show that there are changes to the working copy. Nix already takes care of making the output different, so no bumping versions are necessary to do `nix build ...` * Formatted the flake.nix using `nixfmt`. * Used built-in `stdenv.targetPlatform.node` variables for fetching indexion, set `meta.platforms` to limit where it can run. * Added nix info to the README. * Does replacement for /bin/echo in tests * Uses a temporary home directory in tests for config tests * Added nix checks into a separate Github actions job * Ignore nix output `result`s --- .github/workflows/ci.yml | 16 +++++ .gitignore | 4 ++ README.md | 8 +++ flake.nix | 147 +++++++++++++++++++++++++++------------ 4 files changed, 129 insertions(+), 46 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41195ff4..00ebd5dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,3 +39,19 @@ jobs: - name: Test run: go test ./... + nix: + runs-on: ubuntu-latest + env: + CGO_ENABLED: 0 + steps: + - uses: actions/checkout@v7 + - uses: cachix/install-nix-action@6b2916c41eac5735ac995c58c9e8561a78c42319 # v31 + # Check that the flake is at least correct on all systems, does not run + # any builds + - name: Run nix flake check + run: | + nix flake check -vL --no-build --all-systems + # Checks get run during the nix build + - name: Nix build/test + run: | + nix build -vL .#clawscli --print-out-paths diff --git a/.gitignore b/.gitignore index d7cf440d..9b78807e 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,7 @@ claws.log node_modules/ .claude .gtrconfig + +# Nix +result +result-* diff --git a/README.md b/README.md index 3cd8f2b3..4009c669 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,14 @@ go install github.com/clawscli/claws/cmd/claws@latest docker run -it --rm -v ~/.aws:/home/claws/.aws:ro ghcr.io/clawscli/claws ``` +### Nix + +```bash +nix profile add github:clawscli/claws +# or run it without installing to a profile: +nix run github:clawscli/claws -- ... +``` + ## Quick Start ```bash diff --git a/flake.nix b/flake.nix index ebd6c8e5..5a6113e8 100644 --- a/flake.nix +++ b/flake.nix @@ -6,58 +6,113 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, flake-utils }: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; }; lib = pkgs.lib; - indexionPlatform = { - "aarch64-darwin" = "darwin-arm64"; - "x86_64-darwin" = "darwin-x64"; - "aarch64-linux" = "linux-arm64"; - "x86_64-linux" = "linux-x64"; - }.${system} or (throw "indexion: unsupported system ${system}"); - - indexionHash = { - "darwin-arm64" = "1w3m4px4f5hjr0776rxf3682rizz909zrvsh8i115ig67n1riqq5"; - "darwin-x64" = "0000000000000000000000000000000000000000000000000000"; - "linux-arm64" = "0000000000000000000000000000000000000000000000000000"; - "linux-x64" = "0y7dbkrxyr56kdz326hxpm6i3pljp2z4jpzv1mc5qqwrzqfgybbd"; - }.${indexionPlatform}; - - # No upstream release binary for this platform yet (e.g. linux-arm64). - # Dummy hash = unsupported; drop indexion from the shell instead of failing. - indexionSupported = indexionHash != "0000000000000000000000000000000000000000000000000000"; - - indexion = pkgs.stdenvNoCC.mkDerivation { - pname = "indexion"; - version = "0.16.0"; - src = pkgs.fetchzip { - url = "https://github.com/trkbt10/indexion/releases/download/v0.16.0/indexion-${indexionPlatform}.tar.gz"; - sha256 = indexionHash; - stripRoot = true; - }; - installPhase = '' - mkdir -p $out/bin $out/share/indexion - cp indexion $out/bin/ - cp -r kgfs $out/share/indexion/ - ''; + indexion = + let + indexionHashes = { + "aarch64-darwin" = "1w3m4px4f5hjr0776rxf3682rizz909zrvsh8i115ig67n1riqq5"; + "x86_64-linux" = "0y7dbkrxyr56kdz326hxpm6i3pljp2z4jpzv1mc5qqwrzqfgybbd"; + }; + nodePlatform = pkgs.stdenv.targetPlatform.node; + indexionFile = "indexion-${nodePlatform.platform}-${nodePlatform.arch}.tar.gz"; + in + pkgs.stdenvNoCC.mkDerivation (fa: { + pname = "indexion"; + version = "0.16.0"; + src = pkgs.fetchzip { + url = "https://github.com/trkbt10/${fa.pname}/releases/download/v${fa.version}/${indexionFile}"; + sha256 = indexionHashes.${system} or (throw "indexion: unsupported system"); + stripRoot = true; + }; + installPhase = '' + mkdir -p $out/bin $out/share/indexion + cp indexion $out/bin/ + cp -r kgfs $out/share/indexion/ + ''; + meta = { + platforms = builtins.attrNames indexionHashes; + }; + }); + in + { + packages = { + clawscli = pkgs.buildGoModule (fa: { + pname = "claws"; + version = "git-${self.dirtyShortRev or self.shortRev}"; + # Needs to be changed whenever dependencies are updated + vendorHash = "sha256-Ef/2Xs15E5noUYJk2J9k48g0kfTPB6v+D9uUHdOyya0="; + src = + let + inherit (pkgs.lib.fileset) toSource unions; + in + toSource { + root = ./.; + fileset = unions [ + ./go.mod + ./go.sum + ./cmd + ./custom + ./docs + ./internal + ./scripts + ./testdata + ]; + }; + # Note: only building `cmd/claws` does not run the checks properly + excludedPackages = [ + "scripts/gen-imports" + ]; + ldflags = [ + "-s" + "-w" + "-X main.version=${fa.version}" + ]; + # Replace these absolute path to echo with the nix path for testing + postPatch = '' + substituteInPlace internal/action/action_test.go \ + --replace-warn '/bin/echo' '${pkgs.coreutils}/bin/echo' + ''; + # Fix home to be a writable tmp directory for config tests + preCheck = '' + export HOME=$(mktemp -d) + ''; + }); + gen-imports = self.packages.${system}.clawscli.overrideAttrs ( + f: p: { + pname = "gen-imports"; + subPackages = p.excludedPackages or [ ]; + excludedPackages = [ ]; + } + ); + default = self.packages.${system}.clawscli; }; - in { devShells.default = pkgs.mkShell { - packages = with pkgs; [ - go_1_25 - go-task - gopls - golangci-lint - vhs - ttyd - nodejs - bash - ] ++ lib.optionals indexionSupported [ - indexion - ]; + packages = + with pkgs; + [ + go_1_25 + go-task + gopls + golangci-lint + vhs + ttyd + nodejs + bash + ] + ++ lib.optionals (builtins.elem pkgs.stdenv.targetPlatform.system indexion.meta.platforms) [ + indexion + ]; env.GOROOT = "${pkgs.go_1_25}/share/go";