You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(nix_flake_fmt): handle flakes with a formatter package (#272)
* refactor(fix nix_flake_fmt): split `find_nix_fmt` into smaller functions
This should make things a bit more readable without changing the behavior.
* refactor(nix_flake_fmt): pass data as JSON rather than ad-hoc lines
This makes the communication between our lua code and the `nix eval`
subprocess a bit clearer.
* fix(nix_flake_fmt): handle flakes with a `formatter` package
nixpkgs [recently add a `nix fmt` entrypoint](NixOS/nixpkgs@374e6bc),
and I was excited to be able to use `nix_flake_fmt` in the project.
I was disappointed to find that it doesn't actually work in nixpkgs,
though :(
The problem boils down to how `nix eval .#formatter` behaves if your
flake defines a `formatter` package. See:
```console
$ nix eval nixpkgs#formatter
«derivation /nix/store/nadgq8kci2an9d3ddz1lk662gl53d8yz-formatter-0.4.0.drv»
```
This is *not* the `nix fmt` entrypoint, it's a [completely unrelated
package](NixOS/nixpkgs@b99357e).
Here's the `nix fmt` entrypoint:
```console
$ nix repl nixpkgs
nix-repl> formatter.x86_64-linux
«derivation /nix/store/rza8g2jxr1h7nd58qq3ryfz2zdqnggga-treefmt.drv»
```
This simple `nix repl` session is quite challenging to port to `nix eval`,
though. After quite a bit of futzing around, I found that I could accomplish
this with a combination of `nix flake metadata` and `builtins.getFlake`:
```
$ nix flake metadata nixpkgs --json | jq .resolvedUrl
"github:NixOS/nixpkgs/nixpkgs-unstable"
$ nix eval --expr "(builtins.getFlake "github:NixOS/nixpkgs/nixpkgs-unstable").formatter.x86_64-linux" --impure
«derivation /nix/store/rza8g2jxr1h7nd58qq3ryfz2zdqnggga-treefmt.drv»
```
* fix: ensure we always return a result, and clear the progress notification
There were 2 bugs here:
- In the `drv_path == nil` path, we'd abort execution without returning
a result by calling `done(...)`.
- In that codepath and another, we'd return a `nil` result without
clearing the progress notification.
There might be bugs here if we are running multiple formats in
parallel. I think that's OK to address in the future if it proves to
be a problem.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
0 commit comments