Pass argument to import?

I am trying to modularize my home-manager home.nix file, which I have successfully split out, it is something like:

{ config, pkgs, ... }:

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

which works, but I am trying to pass variables that might be used in several locations,

theme.nix:

{
  hex = {
    foreground = "000000";
  };
}

which is imported in home.nix:

{ config, pkgs, ... }:

let
  theme = import ./theme.nix;
in {
  imports = [
    ./programs.nix
  ];
}

which in my programs.nix I want to use, something like:

{ config, pkgs, theme, ... }:

{
  programs = {
    kitty = {
      enable = true;
      settings = {
        foreground = "#${theme.foreground}";
      };
    };
  };
}

But I do not know how to pass “theme” to the file?

You can call the import function within imports. But ideally, the nix files in your config are primarily package expressions or modules, so I’d suggest structuring your config to be more conducive to making your theme.nix a proper module that can be added to imports directly. Right now you’re fighting the module system by trying to import the same content in multiple places.

In this case, I would suggest creating proper options for your colors. See https://nixos.org/manual/nixos/stable/#sec-writing-modules, particularly option declarations.

1 Like

I don’t see how this helps in my case? How does changing the theme.nix to have the options support like your link help me to reference those values in the programs.nix file?

I think I could import the theme.nix from programs.nix but what I want to do is house multiple themes in the file and be able to change them later

for example, in my home.nix I have something like:

{ config, pkgs, ... }:

let
  theme = (import ./theme.nix).dark;
in {
  imports = [
    ./programs.nix
  ];
}

So I could easily switch dark to light at one location and have it affect all the different modules that want to use theme.

I am brand new to nixos

Create options for your theme, or use the lib option hirarchy, and then read the options values from config.