Nix syntax for passing arguments to imported Modules

Hi all! I am experimenting with NixOS, and I am hitting a roadblock as I can’t find any documentation on how to pass multiple arguments to an imported nix file. I tried the usage of args, but also lib.mkOption, but both seem to fail.

Note that I am running this from home-manager, e.g. home-manager switch -n. I have the feeling that impacts the way I am off on a conceptual level, or misunderstand how nix import works. I think I have to use config, but don’t understand how that works.

I am getting a range of errors related to missing Attributes or just build failures.

The relevant code reduced to the essentials is the following:


{ config, pkgs, lib, ... }:


  username = "myuser";
  homeDirectory = "/somepath";

  myarg = 2;

  myfunction = import ./myfunction.nix {
    lib = lib;
    # passing additional args here does not work for example
    # foo = myarg;
  # more nix
  # ...

in {
  home.username      = username;
  home.homeDirectory = homeDirectory;

  # Here I would like to use myfunction.something
  # I don't think I understand the syntax.

  # more nix setting home.file and such
  # .... 

  home.packages = with pkgs; [

  home.sessionVariables = {
    # EDITOR = "emacs";

  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;

{ lib, ... }:
  # does not work
  # foo =;
  a = or 0;
  result = foo + 3;

in {
  something = result;

I can’t find docs on how to do this properly. Could somebody point me to the right documentation or give a minimal example?

Thanks a lot!

1 Like

I found the solution.

{ lib,  foo, ... }:

Still, I see no one else in their config passing variables in this manner. Is it not canonical to do it this way?

1 Like

Sadly, I am unsure myself what the canonical way of passing parameters to imported modules is. For me, I do it like this:

In my flake.nix:

(import ./modules/desktop/kde.nix { inherit config lib options pkgs; })

in my ./modules/desktop/kde.nix:

{ config
, options
, lib
, pkgs
, ...
  inherit (builtins) pathExists readFile;
  inherit (lib.modules) mkIf;

  cfg = config.modules.desktop.kde;
  options.modules.desktop.kde =
      inherit (lib.options) mkEnableOption mkOption;
      inherit (lib.types) nullOr path;
      enable = mkEnableOption "Enable the KDE desktop environment";

  config = mkIf cfg.enable {
    # ... various options like:
    #environment.systemPackages = with pkgs; [
    #  ...
    # ];
1 Like