nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
6.39k stars 1.7k forks source link

bug: can not import module from home-managers.users.user #4585

Closed diegofariasm closed 9 months ago

diegofariasm commented 9 months ago

Are you following the right branch?

Is there an existing issue for this?

Issue description

I am trying to do a conditional import for a module:

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

with lib;
let
  cfg = config.modules.theme;

  stylixModule = lib.mkIf (cfg.active != null) {
    imports = [
      inputs.stylix.homeManagerModules.stylix
    ];
  };
in
{
  options.modules.theme = with types; {
    active = mkOption {
      type = nullOr str;
      default = null;
      apply = v:
        let theme = builtins.getEnv "THEME";
        in if theme != "" then theme else v;
      description = ''
        Name of the theme to enable. Can be overridden by the THEME environment
        variable. Themes can also be hot-swapped with 'hey theme $THEME'.
      '';
    };

    wallpaper = mkOpt (either path null) null;

    gtk = {
      iconTheme = {
        name = mkOpt str "Papirus";
        package = mkOpt package pkgs.papirus-icon-theme;
      };
      cursorTheme = {
        name = mkOpt str "Nordzy-white-cursors";
        package = mkOpt package pkgs.nordzy-cursor-theme;
      };
    };
  };

  config = lib.mkMerge [
    {
      gtk = {
        enable = true;
        iconTheme = {
          name = cfg.gtk.iconTheme.name;
          package = cfg.gtk.iconTheme.package;
        };
        cursorTheme = {
          name = cfg.gtk.cursorTheme.name;
          package = cfg.gtk.cursorTheme.package;
        };
      };

      stylix.image = config.modules.theme.wallpaper;
    }

    stylixModule
  ];
}

And the issue is i get this error:

error: The optionhome-manager.users.fushiii.imports' does not exist`

Is there anyway to achieve what i am trying to do or is not possible?

Maintainer CC

No response

System information

- system: `"x86_64-linux"`
 - host os: `Linux 6.1.56, NixOS, 23.11 (Tapir), 23.11.20231009.f99e5f0`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.17.0`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`
diegofariasm commented 9 months ago

I have tried look around for a option defined for the imports, but i couldn't really find it. To be honest, i think is mistagged this issue.

Anyway, wouldn't it be possible to make an option that resolves to the normal home-manager imports? this would, in instance, enabe you to do condiotional imports in a pretty concise manner.

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

let cfg = config.modules.editors.kakoune;
in
{
  options.modules.editors.kakoune = {
    enable = lib.mkOption {
      description = ''
        Wheter to install kakoune.
      '';
      type = lib.types.bool;
      default = false;
      example = true;
    };
  };
  config = lib.mkIf cfg.enable {

   imports = [
     someHomemanagerModule
   ];

    home.packages = with pkgs; [
      kakoune
    ];
  };

}
berbiche commented 9 months ago

Hi,

The assignment of imports must be moved at the same level as options and config.

It's not possible to have conditional (lazy) imports as the nixpkgs module system does not support it.

imports is not an option and handled in a unique way like config, options and other meta values.

diegofariasm commented 9 months ago

It's not possible to have conditional (lazy) imports as the nixpkgs module system does not support it. Oh, that's too bad.

The assignment of imports must be moved at the same level as options and config. I know. I just had an doubt and would be wondering if something like that would actually be possible to achieve.

I was just wondering if that would be possible because of this:

imports = [ … ] ++ lib.optional (builtins.pathExists ./secrets.nix) ./secrets.nix;

That i found in a discourse discussion: nixos-discourse

berbiche commented 9 months ago

Yes, that solution would work as it can be evaluated before the rest of the module system.

I believe all/most references to config while evaluating imports will lead to an infinite recursion error.

diegofariasm commented 9 months ago

Anyway, thanks for the help. It's just as you have said, mostly everything i have tried to do ended up in a infinite recursion. Thanks for the help and sorry for the trouble.