Import inside flake.nix inputs?

Is it possible to somehow import a file in inputs in a flake.nix?

{
  inputs = {
         nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  } // (import other-inputs.nix);
}

and other-inputs.nix

{
    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
    };
}

So far that somehow does not work, nix wants a attrs set not a thunk

Not with nix.

nix-super as a set of patches supports that, and I heard that lix also has lifted the language limitations in the flake.nix.

2 Likes

i also had this to enable full eval in flake.nix

Nothing but literal data is allowed in flake.nix outside of outputs, unfortunately. Personally, I’m actually getting around this by having my flake.nix generated in a pre-commit hook, essentially caching information computed elsewhere. That was quite a bit of work to set up robustly, though.

1 Like

:slight_smile: Why you wanna do that in a pre-commit hook, that sound like I water my plants with a pool…

So I don’t have to think about making sure it’s up to date before committing. Every commit is just naturally synced.

Consider sharing your motivation for this; you may get better advice.

1 Like

Yeah, that’s a good point. There are use cases where there are alternatives, e.g. if you’re doing this because you want some form of inheritance you can define inputs like:

{
  inputs = {
    shared-inputs.url = "something";

    nixpkgs.follows = "shared-inputs/nixpkgs";
    treefmt-nix.follows = "shared-inputs/treefmt-nix";
  };
}

I use this pattern quite a bit, since I think flakes’ most useful purpose is to share development tooling setups between multiple repositories, and for that sharing specific input versions is very desirable.

You can even combine that with subflakes if you’re doing this just to make your inputs block look less busy.

You can never totally DRY out inputs, though. This is by design, since nix wants to avoid expensive evaluation for deeply nested trees of flakes when you use e.g. nix flake show. It is also one of the big criticisms of flakes - I personally still believe they should never have been turing-complete nix files to begin with.

starting to get off-topic, but the inputs themselves are not which causes confusion

the point about inputs not being fully turing complete too and thus making users rely on even more unsafe workarounds is a solid argument too :

1 Like