How to pass through nixosModules in flakes

This is about composition of NixOS modules provided by seperate flakes. I want to create a new flake which provides NixOS modules upstream to its consumer.

Say I have a nix flake A, which provides a package A.defaultPackage
and a NixOS module in that flake nixosModules.a. How do I pass through
that NixOS module in another flake B which is consumed in a NixOS
configuration?

When I start a repl in repo B I cannot see that inputs.A “exports” a
a NixOS module (as it is visible for packages); The package is there,
which is fine (1).

Host

  • configuration based on Flake B:
    Flake B
    - provides: NixOS module
    - depends-on:
    Flake-A:
    - provides: package
    - provides: NixOS module

So in summary:

  • I have 3 flakes in seperate Git repositories

  • Host configuration needs flake B and packages which it should
    propagate, and also NixOS modules

  • Flake B should provide its own NixOS module (works) and it should
    propagate the input package from Flake A (works). It should also
    provide the NixOS module from Flake A to its consumers (i.e. host, doesnt work).

  • Flake A provides a package (works) and provides a NixOS module
    (works if directly consumed in a host configuration, but not as
    passthrough in flake B).

Another formulation of my problem could be:

  • Say I have multiple NixOS modules in seperate flakes
  • How do I create a new flake which aggregates these NixOS modules?

Example nix repl session (1):

$ nix repl 
Welcome to Nix 2.6.0pre20211217_6e6e998. Type :? for help.
nix-repl> :lf .#
Added 11 variables.
nix-repl> inputs.A
{ defaultPackage = { ... }; inputs = { ... }; lastModified = 1645565462; lastModifiedDate = "20220222213102"; narHash = "sha256-VIhwPpgiX7s7Vz3lPnVULXZFViZmh1csDeGzfAayoE0="; outPath = "/nix/store/36davbc584ar1784p7q9a2jh8mknfll2-source"; outputs = { ... }; packages = { ... }; rev = "2219497c9b85b0fb07c6c6b112b9e226509ba02d"; revCount = 2; shortRev = "2219497"; sourceInfo = { ... }; submodules = false; }
nix-repl> inputs.A.outputs 
{ defaultPackage = { ... }; packages = { ... }; }

Looking Forward :slight_smile:

2 Likes

I’ve not fully understood the relations you have between flakes but this is how I utilized nixosModules from another flake:

# wrapper, this is our first import level:
{ nixos-hardware }:
# actual module, this is what's passed to nixos just like a normal import:
{ lib, ... }:

{
  imports = [
    nixos-hardware.nixosModules.lenovo-thinkpad-x1-extreme
    # "${nixos-hardware}/lenovo/thinkpad/x1-extreme/default.nix"
  ];
  # etc ...
}

then above

 nixosSystem {
    system = "x86_64-linux";
    modules = [
        # the difference between these two modules is one is used as is
        # whereas one is wrapped and pre imported passing in my nixos-hardware input
        (import ./modules/some-hardware.nix { inherit nixos-hardware; })
        ./modules/tailscale.nix
     ]
# ...
2 Likes

How about something like:

{
  description = "Flake A";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

  outputs = { self, nixpkgs, ... }:
    {
      nixosModules = {
        moduleA = import ./module.nix;
      };
    };
}

and then:

{
  description = "Flake B";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flakeA = {
      inputs.nixpkgs.follows = "nixpkgs";
      url = "... flake A URI ...";
    };
  };

  outputs = { self, nixpkgs, flakeA, ... }:
    {
      nixosModules = {
        # add moduleA from flakeA as an output
        moduleA = flakeA.nixosModules.moduleA;
        moduleB = import ./module.nix;
      };
    };
}
3 Likes

Thank you for your reply (also @j-k ). I stepped back from what I created so far to concentrate on the basics. I found that what I was missing was a too old flake.lock. This was the reason that I was missing nixosModules in B (you see it my repl output above).

I created for learning purposes two repositories which demonstrate what I learned:

Note: I’m missing partial application of functions taking an attribute set as argument :slight_smile:

This helped me a lot!

3 Likes