Trouble Referencing Inputs in Sub-Module

I recently began to work on my NixOS configuration and just ran into a problem I can’t seem to find a solution for.

I’m using NixOS flakes and am passing down inputs as specialArgs down to my sub-modules. To my understanding, all child-modules should be able to import inputs and then use it.

Here is an excerpt from my configuration:

flake.nix

{
  ...
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-24.05";
  };
  outputs = inputs @ { self, nixpkgs, nixpkgs-stable, home-manager, ... }: 
  let 
    system = "x86_64-linux";
    pkgs-stable = nixpkgs-stable.legacyPackages.${system};
    commonArgs = { # <-- Creating a variable for storing my specialArgs
      inherit inputs pkgs-stable;
    };
  in {
    nixosConfigurations = {
      ideapad = nixpkgs.lib.nixosSystem {
        inherit system;
        specialArgs = commonArgs; # <-- Passing down specialArgs to all sub-modules

        modules = [
          ./hosts/ideapad/configuration.nix
          ...
        ];
      };
    };
  };
}

in my configuration.nix I import system/base/default.nix and from there nix.nix.
Basically like:

{ ... }:

{
  imports = [
    ./system/base/default.nix
  ];
}
{ ... }:

{
  imports = [
    ./nix.nix
  ];
}

Now in my nix.nix file I try to reference inputs.nixpkgs.<...>.

{ lib, inputs, ... }:

{
  # Enable Nix Flakes
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
  
  # Allow unfree packages from stable and unstable branch
  inputs.nixpkgs.config.allowUnfree = lib.mkForce true;
  inputs.nixpkgs-stable.config.allowUnfree = lib.mkForce true;
  
   ...
}

But when trying to rebuild my system configuration I get following error:

building the system configuration...
error:
       … while calling the 'seq' builtin

         at /nix/store/rljkm6h18fjvavm6l17jiil8glphc44f-source/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       … while calling the 'throw' builtin

         at /nix/store/rljkm6h18fjvavm6l17jiil8glphc44f-source/lib/modules.nix:298:18:

          297|                     ''
          298|             else throw baseMsg
             |                  ^
          299|         else null;
 
       # ERROR BEGINS HERE
       error: The option `inputs' does not exist. Definition values:  
       - In `/nix/store/vqwpv723z9278kpgni969k3rswapgqwf-source/modules/system/base/nix.nix':
           {
             nixpkgs = {
               config = {
                 allowUnfree = {
                   _type = "override";
           ...

To my understanding inputs should also be accessible to nix.nix but clearly it isn’t. Is this a misunderstanding from my side or am I just blatantly not seeing where my error lies?

I greatly appreciate any help, thank you!

There are a couple of points here:

  1. The way nix.nix is written is that it’s trying to set an option called “inputs.nixpkgs.config.allowUnfree”[*] rather than try to reference something coming in from inputs. This option does not exist. Change it to just nixpkgs.config, since it affects only the main pkgs instance that is used in the system, one that’s effectively created when you call nixpkgs.lib.nixosSystem.

    inputs is actually perfectly accessible in your code, but you are not reading from it (at least in the code snippets in the post).

  2. If you want to use unstable nixpkgs while using stable nixpkgs for the majority of the system, you have a couple of options

    a. Use an overlay.

    Or

    b. Create isolated instances of pkgs-unstable in individual modules:

     ```nix
     # somefile.nix
     { inputs, pkgs, ... }:
     let
         pkgs-unstable = import inputs.nixpkgs {
             inherit (pkgs) system;
         };
     in
     {
         environment.systemPackages = [ pkgs-unstable.hello ];
     }
     ```
    

[*] not literally that, but an option with this path.

1 Like

Thank you very much! Your tip about using overlays actually led me to a solution that I’m even happier with than what I initially imagined. Hope you have a great day :slightly_smiling_face:!