How to pass variables to a module defined in a `flake.nix` file?

I have this flake.nix file

{
  description = "My GNOME Nix config";
  
  inputs = {
    nixpkgs = { 
      url = "github:NixOS/nixpkgs/release-23.05";
    };
    home-manager = {
      url = "github:nix-community/home-manager/release-23.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, home-manager, ... }:
  let
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config = { 
        allowUnfree = true;
        permittedInsecurePackages = [
          "electron-12.2.3"
        ];
      };
    };
    config = nixpkgs.config;
    username = "sayantan";
    hostname = "srcnix";
    stateVersion = "23.05";
  in
  {
    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
      inherit system;
      modules =
      [ 
        {
          imports = [
            ./my_general_configs.nix
          ];
        }
      ];
    };
  };
}

Everything is already defined here - pkgs with non-free option, home-manager etc.

My my_general_configs.nix file is as below:

{ config, pkgs, home-manager, ... }:

{
  home-manager = {
    useUserPackages = true;
    useGlobalPkgs = true;
    users.sayantan = { pkgs, ... }: {
      home = {
        stateVersion = "23.05";
        packages = with pkgs; [
          google-chrome
        ];
      };
    };
  };
}

This is a shortened version, but there are many other lines of code.

When I try to compile this with - nixos-rebuild build --impure --flake .#srcnix

I get this error:

error: The option `home-manager' does not exist. Definition values:
       - In `/nix/store/2xy5f03gjdkhvw88j0adr4q8ya3ka0k6-source/my_general_configs.nix':
           {
             useGlobalPkgs = true;
             useUserPackages = true;
             users = {
               sayantan = <function, args: {pkgs}>;
           ...

Sure I can fix it by changing my_general_configs.nix file as below

{ config, pkgs, ... }:

let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/release-23.05.tar.gz";
in {
  
  imports = [ <home-manager/nixos> ];

  nixpkgs.config.allowUnfree = true;
  
  home-manager = {
    useUserPackages = true;
    useGlobalPkgs = true;
    users.sayantan = { pkgs, ... }: {
      home = {
        stateVersion = "23.05";
        packages = with pkgs; [
          google-chrome
        ];
      };
    };
  };
}

But then what is the point of defining home-manager and all other variables in the flake.nix file? I am even having to declare the unfree option again which was already declared in the flake.nix file. Not to mention I always have to use --impure option because of the import statement.

I really need some assistance regarding how to pass home-manager and other variables from the flake.nix file to other modules. Thanks in advance.

1 Like

If you inherit inputs, home-manager will be available through inputs.home-manager.

outputs = { self, nixpkgs, ... }@inputs:
...
    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
      inherit system inputs;
...

Alternatively, adding {_module.args.home-manager = home-manager} to the modules list will make home-manager passed in as an argument to your modules.

1 Like

Some more clarification would be helpful. I tried the following ways:

  • Attempt 1:

    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
       inherit system inputs;
       modules =
       [ 
         ./my_general_configs.nix
       ];
     };
    

    Error:

    error: anonymous function at /nix/store/acyxfdyvyyp7gbp5mwqyzz9z2qnag3fx-source/nixos/lib/eval-config.nix:11:1 called with unexpected argument 'inputs'
    
        at /nix/store/acyxfdyvyyp7gbp5mwqyzz9z2qnag3fx-source/flake.nix:22:11:
    
            21|         nixosSystem = args:
            22|           import ./nixos/lib/eval-config.nix (
              |           ^
            23|             args // {
    
    
  • Attempt 2

    # flake.nix
    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
       inherit system;
       specialArgs = {
         inherit inputs;
       };
       modules =
       [ 
         ./my_general_configs.nix
       ];
    };
    
    # my_general_configs.nix
    
    { inputs, ... }:
    
    { 
      inputs.home-manager = {
        useUserPackages = true;
        useGlobalPkgs = true;
        users.sayantan = { pkgs, ... }: {
          home = {
            stateVersion = "23.05";
            packages = with pkgs; [
              google-chrome
            ];
          };
        };
      };
    }
    

    Error:

    error: The option `inputs' does not exist. Definition values:
        - In `/nix/store/v0j2sx9i7jkngxjsp48ip1v70ri5ix98-source/my_general_configs.nix':
            {
              home-manager = {
                useGlobalPkgs = true;
                useUserPackages = true;
                users = {
            ...
    
    
  • Attempt 3

    # flake.nix
    
    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
       inherit system;
       modules =
       [ 
         {_module.args.home-manager = home-manager;}
         ./my_general_configs.nix
       ];
     };
    
    # my_general_configs.nix
    
    { home-manager, ... }:
    
    { 
      home-manager = {
        useUserPackages = true;
        useGlobalPkgs = true;
        users.sayantan = { pkgs, ... }: {
          home = {
            stateVersion = "23.05";
            packages = with pkgs; [
              google-chrome
            ];
          };
        };
      };
    }
    

    Error:

    error: The option `home-manager' does not exist. Definition values:
        - In `/nix/store/kr7j591bh4js3wgn83rmd7lmrcw36hh6-source/my_general_configs.nix':
            {
              useGlobalPkgs = true;
              useUserPackages = true;
              users = {
                sayantan = <function, args: {pkgs}>;
            ...
    
    
1 Like

home-manager is implicitly defined (no explicit arg added or param added) in my top level config in all nix modules for me with my flake setup.

Try adding home-manager nixosModule in your modules list in flake.nix.

like

modules = [
  # ...
  # defining nix home-manager modules: https://nix-community.github.io/home-manager/options.xhtml
  home-manager.nixosModules.home-manager {
    home-manager.useGlobalPkgs = true;
    home-manager.useUserPackages = true;
    # edit here:
    home-manager.users.${username} = ({ pkgs, ... }: { home.stateVersion = "23.05"; })
    # or import
    # home-manager.users.${username} = import ./home-manager-modules { inherit vars pkgs lib config; };
  }
];