Flakes: what are the simplest, beginner friendly ones you have seen in the wild?

I have adopted flakes and so they are a topic I teach when introducing colleagues to nix.

I often begin writing simple flakes, but then I realized the Internet, Nix ecosystem, and you who respond to this post are an incredible resource.

What are the simplest nix flakes you have seen that are publicly available?

5 Likes

But the examples are not that simple if you are new.

Maybe others have better resources.

2 Likes

Thanks for the question!

Currently going through the process of learning to work with flakes, meanwhile having relatively limited experience with the legacy nix packaging approach.

Just recently started the following to help myself:

and to have something handy in the future in case I need to refresh my memory. I especially wanted something that is complete, meaning that nothing needs to be added to it in order to try it.

I somehow overlooked the templates mentioned by @nixinator, and will consult them in the future.

I’ve found Practical Nix Flakes quite useful.

As a newcomer it can be a bit difficult to quickly get something working based on examples, because different examples may use different approaches. For instance, the interplay between legacy nix and flakes can be coded differently. The default.nix can be imported in flake.nix or the default.nix can import the flake. When using a rust overlay, different overlays are available. If cross-compilation is needed, there seems to be different ways to go about it, etc. So it can be quite challenging to pick things up from multiple examples as different examples may use different approaches.

Hence having a rich collection of ideally fully working examples can be very useful. So far I’ve glanced at the repo of different projects I come across that are relevant to what I am working on, and that people point too. But all these projects are not so simple, so I had to pick them apart to try what I needed.

3 Likes

As auxilliary to everyone else’s answers, some of mine are pretty simple while still being useful, and might serve as a lightweight example for some things here and there.

I would also recommend checking out https://christine.website/blog/nix-flakes-1-2022-02-21, which is a well written introduction to the concept of flakes as a whole.

2 Likes

I started using flakes only after I was comfortable writing other Nix packages. However, I found the NUR template a good way to get started with “here’s my nix stuff”. GitHub - nix-community/nur-packages-template: A template for NUR repositories: [maintainer=@fgaz] – Maybe it’s not “simple”; but, I could use it without knowing what flakes are / without interacting with the flakes, and I could just remove the stuff I didn’t care about. I think it’s monkey-see-money-do’able.

GitHub - nix-community/nix-direnv: A fast, persistent use_nix/use_flake implementation for direnv [maintainer=@Mic92 / @bbenne10] I find nix-direnv pretty neat. Since it provides a template, getting started with it (assuming direnv is installed) is simply:

nix flake new -t github:nix-community/nix-direnv
2 Likes

Not an example, but I have a video on flake usage:

3 Likes

Development environments can be very simple flakes. For example,

{
  description = "Development environment";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";

  outputs =
    { self
    , flake-utils
    , nixpkgs
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        devShell = pkgs.mkShell {
          packages = with pkgs; [
            # See https://github.com/NixOS/nixpkgs/issues/59209.
            bashInteractive
          ];
          buildInputs = with pkgs; [
            some
            nice
            packages
            here
          ];
        };
      }
    );
}

I am not even sure if one needs flake-utils here.

3 Likes

I’m pretty happy with the one for agenix:

I’ve been happy with accepting some boilerplate around multiple system types in exchange for the flake being less complex and nested.

2 Likes