Accessing flake outputs in the flake itself

I am using flakes to build my NixOS system. I have an overlay or a custom package derivation buried somewhere in one of the modules that I use to build my system. The flake.nix looks something like this:

{ inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs, ... }: {
    nixosConfigurations.my-computer = nixpkgs.lib.nixosSystem {
      modules = [ ./file1.nix ./file2.nix ./gui.nix ];
    };
  }
}

and inside gui.nix I have

{ pkgs, ... }:
let my-overlay = self: super: # bla bla bla
in {
  nixpkgs.overlays = [ my-overlay ];
  # more gui stuff
}

Now I would like to move my-overlay from gui.nix to outputs.overlays in flake.nix but still “use” it in gui.nix. So the line nixpkgs.overlays = [ my-overlay ]; should essentially stay in the file so that I can just remove the gui.nix from my modules list if I want to and effectively not be using the overlay any longer.

So how can I access outputs.overlays inside a file that is listed in the modules list of my nixpkgs.lib.nixosSystem call?

(The question could be equally stated for outputs.packages.)

background

This is not critical in any way (my config builds and works just fine). But I am trying to modularize my flake a little so that parts of it could be used from outside. For example I moved some custom package to outputs.packages in order to nix build it and trying out stuff. Now I am done and I would like to just use outputs.packages in gui.nix instead of copying the code around again.

2 Likes

You can access the flake output through self. E.g. self.nixosConfigurations.my-computer will contain your nixos configuration. To have it be available in gui.nix you will have to add self or self.defaultOverlay (or something else) to the extraArgs of the nixosSystem call. You can then use { pkgs, self, ... }: in your nixos configuration files.

Thank you, extraArgs for nixosSystem did the trick. Do you know where this function and argument is documented?

For reference, I am now using

{ inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs, ... }: {
    nixosConfigurations.my-computer = nixpkgs.lib.nixosSystem {
      modules = [ ./file1.nix ./file2.nix ./gui.nix ];
      extraArgs = { flake = self; };
    };
  }
}

and

{ flake, pkgs, ... }: {
  nixpkgs.overlays = [ flake.overlays.my-overlay ];
  # more gui stuff
}

I don’t think there is any documentation for it, sadly. It’s defined here but it mostly just calls this function.

I was happy with this solution but since

it errors like so:

$ nixos-rebuild build --override-input nixpkgs github:nixos/nixpkgs/b5681a7a4096b81101626a0853c28bede0495490
warning: not writing modified lock file of flake 'git+file:///home/luc/src/sys':
• Updated input 'nixpkgs':
    'github:nixos/nixpkgs/bc5d68306b40b8522ffb69ba6cff91898c2fbbff' (2021-12-06)
  → 'github:nixos/nixpkgs/b5681a7a4096b81101626a0853c28bede0495490' (2021-11-01)
building the system configuration...
warning: not writing modified lock file of flake 'git+file:///home/luc/src/sys?ref=HEAD&rev=95ddb9eab26f703dae289384c999e0728e1a4c4d':
• Updated input 'nixpkgs':
    'github:nixos/nixpkgs/bc5d68306b40b8522ffb69ba6cff91898c2fbbff' (2021-12-06)
  → 'github:nixos/nixpkgs/b5681a7a4096b81101626a0853c28bede0495490' (2021-11-01)
error: attribute 'flake' missing

       at /nix/store/6micd9bwlb2c9is60jfwplslsnp7lh99-source/lib/modules.nix:360:28:

          359|         builtins.addErrorContext (context name)
          360|           (args.${name} or config._module.args.${name})
             |                            ^
          361|       ) (lib.functionArgs f);
(use '--show-trace' to show detailed location information)

(With --show-trace the output is very long).

@chvp do you have an idea? I also created a mini flake to produce the error:

And checked the current Github Issue (which they already closed because they think it is fixed):

For future reference: As extraArgs is being deprecated I am now using the NixOS option config._module.args like this:

{ inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs, ... }: {
    nixosConfigurations.my-computer = nixpkgs.lib.nixosSystem {
      modules = [
        ./file1.nix ./file2.nix ./gui.nix
	    { config._module.args = { flake = self; }; }
      ];
    };
  }
}

Sadly the NixOS option config._module.args (or even config._module orconfig) is not documented in the NixOS options.

The code for it seems to be in lib/module.nix.

3 Likes

Using the deprecated extraArgs was the reason why I was unable to build a flake configuration with config.specialisation set. Now I replaced it with an inline module like you showed and it works, thank you!