feat(nix): add flake to support nix builds#230
feat(nix): add flake to support nix builds#230emrtnn wants to merge 4 commits intomodem-dev:mainfrom
Conversation
Greptile SummaryThis PR adds Nix flake support so users can install and run Hunk natively via Nix, without requiring any Nix knowledge from core maintainers. The design uses a two-phase build — a fixed-output derivation to fetch and hash-verify
Confidence Score: 3/5Safe to merge for Linux users, but two defects affect Mac maintainers and non-x86/ARM builders before they can be fixed post-merge. The
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[flake.nix] --> B[import .nix/package.nix]
B --> C{bunDeps FOD}
C -->|Network enabled| D[bun install --frozen-lockfile --ignore-scripts]
D --> E[node_modules verified by sha256 hash]
E --> F[Main derivation - no network]
F --> G[cp node_modules from bunDeps]
G --> H[bun run build:bin]
H --> I[dist/hunk binary]
I --> J[$out/bin/hunk installed]
K[nix:update-hash script] -->|Writes dummy hash| C
C -->|Build fails - hash mismatch| L[Extract correct hash from error output]
L -->|Write to| M[.nix/nix-deps-hash.txt]
M --> C
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
.nix/package.nix:63
Bun's binary is only available for `x86_64-linux`, `aarch64-linux`, `x86_64-darwin`, and `aarch64-darwin`. Using `platforms.all` will let users attempt to build on i686-linux, armv7l-linux, riscv64-linux, s390x-linux, and others where `pkgs.bun` itself is unavailable — the build will fail with a confusing "attribute missing" error rather than a helpful "unsupported platform" message. Restricting `platforms` to Bun's actual supported set surfaces the issue clearly before the build starts.
```suggestion
platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
```
### Issue 2 of 2
scripts/update-nix-hash.sh:9
The `-P` (Perl-compatible regex) flag is not available in macOS's BSD `grep`. On any Mac the command silently produces no output, `NEW_HASH` stays empty, and the script exits with the "❌ Failed to extract hash" error. The `\K` keep-assertion can be replaced with `-E` (extended regex) plus a `sed` strip, which works on both GNU and BSD toolchains.
```suggestion
NEW_HASH=$(echo "$OUTPUT" | grep -oE 'got:[[:space:]]+sha256-[a-zA-Z0-9+/=]+' | sed 's/got:[[:space:]]*//')
```
Reviews (1): Last reviewed commit: "feat(nix): add flake to support nix buil..." | Re-trigger Greptile |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
|
Hey, I just opened basically the same PR, only using bun2nix instead of your impure network-enabled solution. I've had very good experience with bun2nix in the past. I think it would be a little annoying having to update the hash every time a package is updated -- especially by people who don't use nix. I can see that having the bun.lock.nix file which bun2nix requires could also be confusing for people not using nix. Also the added If this PR is the way forward, I'd suggest adding your bash script to |
| default = pkgs.mkShell { | ||
| buildInputs = with pkgs; [ | ||
| bun | ||
| nodejs |
There was a problem hiding this comment.
Also nodejs is not needed in the devShell I think -- even though it's listed as a requirement in CONTRIBUTING.md. Not sure why it is. Git should always be installed but could be added to the devShell as to be explicit.
There was a problem hiding this comment.
Hey, thanks for the comment! Yeah indeed I tried yesterday to build it using bun2nix but adding the postinstall script gave me a lot of issues because this repo uses bun as a dependency itself so it was causing a conflict between the nix installed bun and the repo installed bun by the nix one. Since I couldn't manage solve this I chose this way.
Didn't know you were also working on this if I had I wouldn't have made this PR, that's why I opened #215 before anything.
That said, if you agree with following with this PR I consider your postinstall suggestion a really good catch and will be happy to add it. The only issue I see is that it will make installing deps much slower but maybe that's not that big of a deal for core contributors
There was a problem hiding this comment.
I think it's very cool how we took two different routes and both landed at a valid solution to the problem! I too ran into the issue with the bun dependency in package.json. However, I just removed it there xd
| @@ -0,0 +1 @@ | |||
| sha256-Lr3M22JlMcX+HTZay2A1rN/zwGnznIjaib3E3y0Aob0= | |||
There was a problem hiding this comment.
I think the hash might change across architectures (darwin/linux and x86/arm64). I can test this if needed
There was a problem hiding this comment.
Removing it was also my first thought but I want to believe is there for an unknown reason to us and I usually try touching core files like package.json as little as possible.
Now that you speak about that I think you are right too. I can also test tomorrow at work where my machine has an ARM CPU. If our concerns are right then I don't think this is anymore a viable method. We would have to add a json file with one hash per architecture instead of a txt one and that is a maintenance nightmare.
We should use bun2nix if that happens but I just want to be sure that there's no issue with deleting bun as a dependency in package.json so this PR can be closed in favor of yours.
Any core maintainer can give opinion on removing it?
There was a problem hiding this comment.
just tried it on arm64 fedora and indeed the hash is different :/
Closes #215
As suggested in the issue, this PR adds a flake.nix file that Nix users can use to natively install and run the package.
The way I structured it is a top-level
flake.nixthat imports.nix/package.nix, the file containing the actual logic for building Hunk.This is designed so no core maintainer needs any Nix knowledge:
Inside
.nix/, there is anix-deps-hash.txtfile that's generated by the newbun run nix:update-hashscript. Whenever the project's dependencies change, the Nix build will temporarily fall out of sync until someone runs this script to fetch the new hash. I suggest automating this via CI in the future so it's entirely hands-off for you, but I didn't want to make this initial PR too broad.Since @MarkusZoppelt has already opened a PR (NixOS/nixpkgs#517626) to officially add Hunk to nixpkgs, this flake can be used by the community in the present while we wait for that to merge. Later, it will remain highly useful for local development (nix develop) and building purposes directly from source (the local building strategy is different from how nixpkgs will fetch the binary). I also used the exact same package name he chose, as discussed in his PR.