Can't use let for inputs

Yeah I’ve seen Why can't I use let variables in flake.nix inputs? but I figured it would be rude to bump such an old thread.

I wanted to do this:

  inputs =
  let
    version = "unstable";
  in
  {
    nixpkgs = {
      url = if version == "unstable" then
        "github:NixOS/nixpkgs/nixpkgs-unstable"
      else
        "github:NixOS/nixpkgs/nixpkgs-${version}-darwin";
    };
    nix-darwin = {
      url = if version == "unstable" then
        "github:lnl7/nix-darwin"
      else
        "github:lnl7/nix-darwin/nix-darwin-${version}";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    home-manager = {
      url = if version == "unstable" then
        "github:nix-community/home-manager"
      else
        "github:nix-community/home-manager/release-${version}";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

but that gives me:

error: expected a set but got a thunk at /nix/store/k3q3jn3fc95jb15v0rv5prm9axjc3dlf-source/flake.nix:4:3

A minor annoyance, but…it is kind of annoying. Would also have been nice if such major projects used a consistent convention for the version (though it wouldn’t change much in this case, it’s just one more paper cut).

Yep, been there. A flake is more like a manifest than a normal dynamic expression as you’d put in any other .nix file. So it does not supports that kind things you are trying to do.

However, I have a little project of mine: flake-file that generates the flake.nix file for you. It requires you to use flake-parts and you write your inputs as any other nix configuration. Checkout the getting started templates, I’m using it and you can use my configs as example as well.

EDIT: flake-file uses itself -that’s why the project logo is an ouroboros generating itself, haha- so it is probably a much simpler example than my configs.

3 Likes

What do you need help with? It’s simply not possible to do, by design, as you found out.