`fh`, the CLI for FlakeHub

Hey everyone,

After last week’s launch of FlakeHub, one of the most frequent requests was for a CLI. Today, we’re publishing the first release of fh, the FlakeHub CLI.

Today’s release is a scrappy one, but it solves a few tasks:

  1. Searching for existing flakes, including listing all the flakes and organizations.
  2. Adding flakes to your flake.nix
  3. Listing all the revisions of a published flake.

Here’s our announcement blog post: `fh`: the CLI for FlakeHub

Here’s our project on FlakeHub: FlakeHub

Adding flakes

To add a flake to your project, use fh add nixos/nixpkgs. You can even start with an empty file, and fh will bootstrap one for you:

$ fh add nixos/nixpkgs
$ cat flake.nix
{
  description = "My new flake.";

  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.490449.tar.gz";

  outputs = { nixpkgs, ... } @ inputs: {};
}

I really like that it updates the outputs function to accept your new input:

$ fh add nix-community/nix-melt
$ cat flake.nix
{
  description = "My new flake.";

  inputs.nix-melt.url = "https://flakehub.com/f/nix-community/nix-melt/0.1.8.tar.gz";
  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.490449.tar.gz";

  outputs = { nixpkgs, nix-melt, ... } @ inputs: {};
}

If the project you’re interested in isn’t published to FlakeHub yet, you can add that too:

$ fh add github:nix-community/naersk
$ cat flake.nix
{
  description = "My new flake.";

  inputs.naersk.url = "github:nix-community/naersk";
  inputs.nix-melt.url = "https://flakehub.com/f/nix-community/nix-melt/0.1.8.tar.gz";
  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.490449.tar.gz";

  outputs = { nixpkgs, nix-melt, naersk, ... } @ inputs: {};
}

Listing releases

A lot of people used a service I used to maintain, channels.nix.gsc.io, to see all the releases of NixOS channels over time. FlakeHub has archived every release of nixos-23.05, and approximately the last nine months of nixos-unstable.

Bisecting nixpkgs

You can list the releases of the NixOS/nixpkgs flake, and use this to help bisect:

$ fh list releases nixos/nixpkgs
+------------------------------------------------------------+
| Version                                                    |
+------------------------------------------------------------+
| 0.1.428801+rev-2788904d26dda6cfa1921c5abb7a2466ffe3cb8c    |
| 0.1.429057+rev-42337aad353c5efff4382d7bf99deda491459845    |
| 0.1.429304+rev-27ccd29078f974ddbdd7edc8e38c8c8ae003c877    |
...
| 0.2305.490435+rev-c540061ac8d72d6e6d99345bd2d590c82b2f58c1 |
| 0.2305.490449+rev-2ab91c8d65c00fd22a441c69bbf1bc9b420d5ea1 |
+------------------------------------------------------------+

Versions 0.1.x refer to nixos-unstable, and 0.2305 refer to nixos-23.05.

We can pipe this output to grep to only get 23.05 releases, and cut to get only the revisions for git bisect:

$ fh list releases nixos/nixpkgs | grep 2305 | cut -d- -f2
453bb832f715dd6e51004c72dbd59b1556d554e1
c7ff1b9b95620ce8728c0d7bd501c458e6da9e04
aaef163eac75c2ac7f882c1fae4e0c08aa18d186
572d26930456132e7f2035340e3d88b36a5e9b6e

They’re ordered by semver, so the oldest releases are at the top, and the newest releases are at the end.

Anyway, this is a first step for fh, but in the future it’ll support pushing from your local machine, or from other CI / forges like GitLab. FlakeHub is the largest collection of flakes, backed by the power of search. People have been asking for something like fh add for ages, and I’m really glad we could make that work nicely.

I hope you like it!

11 Likes

Brilliant! Looking forward to the release of flakes.

3 Likes

Given the “could this be upstreamed” discussion with FlakeHub itself, I wonder what this would look like upstreamed. It seems a bit too vendor-specific to work unless FlakeHub itself were taken over by the Nix org.

As I understand, git can detect executables on PATH with names of the form git-commandname and execute them when you type git commandname. I wonder if a future nix could use the same extensibility model, allowing e.g. an external flake directory to inject its cli into nix flake. Has that been discussed before? I imagine it’s presently infeasible because nix-build and nix build both exist and aren’t equivalent right now, but maybe in the future.

1 Like

Thank you for the source code on this one ; have you considered extracting the code manipulation / transformation part in a library that could be leveraged by all the ecosystem to reproduce similar features (but also more) ?

2 Likes

For sure that fh add could be upstreamed! That’d be great to see!

No: I think if you look at how the sausage is made, you’ll see why :). We have more work to be done here, and at some point I think it’d be straightforward.

Sounds interesting. cargo supports something similar. I think one challenge this presents is blurring the lines about what is nix, and what isn’t. That’s definitely above my pay grade!

2 Likes

Thank you @figsoda for adding fh to Nixpkgs! Coming soon to a binary cache near you =). fh: init at 0.1.1 by figsoda · Pull Request #252807 · NixOS/nixpkgs · GitHub

3 Likes