Import flake.nix file into another flake.nix file

I would like to import one Flake file into another Flake file, change an attribute, and then build it. Is it possible? Let’s say the Flake file to be imported contains a version number, based on which I pull source code from GitHub. Since I cannot change this version number with the ‘nix build’ command, I am trying this method. If you can suggest a better approach, I would appreciate it. However, my main question is whether it is possible to import one Flake file into another and change the attributes.

There’s a bunch of weird stuff going on in this repo, so maybe think twice before copying things you find here–I was just kind of messing around. But yes you can import a flake into another flake, I do so here: https://github.com/MatrixManAtYrService/sieve-illwill/blob/main/flake.nix#L30.

Keep in mind that a flake is a function, so you have to both import it and call it.

      zippy-flake = import ./subflake/zippy/flake.nix;
      zippy-outputs = zippy-flake.outputs {
        inherit self;
        inherit nixpkgs;
        inherit flake-utils;
      };
      zippy = zippy-outputs.packages.${system}.default;

As for modifying an attribute on one of the outputs, I’m not sure off the top of my head: Nix attributes are immutable, but there ought to be a way to create a new attribute which has the desired mutation (overlays come to mind, but I’m not confident about that).

Thank you for your helping, @MattRixman . I am also looking into the concepts of override and overlay. However, I believe what I want is possible. It seems that we cannot change the attributes of flake using the Cli. But I have seen examples where the attributes of a package from nixpkgs can be overridden in many places. Actually, my goal is to create a more flexible structure when creating packages. Instead of creating a new flake and copying the entire process for each new version of a derivative, it seems more logical to simply change the version number and create a package. Maybe I’m understanding the topic wrong, so I’m not sure. Because I have been doing OOP for many years, I always look at it from that perspective, maybe I need to change myself.

I also have OOP habits which I have to question sometimes :slight_smile:

I don’t have a strong sense of your requirements (e.g. what is meant by “derivative”) but based on this:

Instead of creating a new flake and copying the entire process for each new version of a derivative

I wonder if the way forward is to express the difference between each derivative as a different value of one of the flake inputs (which I believe should correspond with the parameters on the .outputs function). Something like this:

      inner-flake = import ./inner/flake.nix;
      derivative-a = inner-flake.outputs {
        inherit self;
        inherit nixpkgs;
        inherit flake-utils;
        derive = "a";
      };
      derivative-b = inner-flake.outputs {
        inherit self;
        inherit nixpkgs;
        inherit flake-utils;
        derive = "b";
      };

Where instead of “a” and “b” you’ve got whatever data determines the difference between each derivative.

I could imagine a structure like this being useful if you want the outer flake to handle things like “build separate apps, one which uses postgres and the other which mysql” and then the inner flake is just the app itself, no test automation. The parameter would then be the particular library (likely a reference to nixpkgs). :man_shrugging:

Yes, I need something like this. After your first message I changed my way, and I got some result. Thanks again.