Creating options to import files

I have a default.nix file in home manager and am trying to import files based off of the options and enable them by default, but none of them are currently importing.

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

  options = {
    firefox.enable = lib.mkEnableOption "Firefox";
    vscodium.enable = lib.mkEnableOption "VSCodium";
    vesktop.enable = lib.mkEnableOption "Discord";
    kitty.enable = lib.mkEnableOption "Kitty";
    obsidian.enable = lib.mkEnableOption "Obsidian";
  };

  config.lib = {
    mkIf.config = {
      firefox.enable = import ./firefox.nix;
      vscodium.enable = import ./vscodium.nix;
      vesktop.enable = import ./vesktop.nix;
      kitty.enable = import ./kitty.nix;
      obsidian.enable = import ./obsidian.nix;
    };
    mkDefault.config = {
      firefox.enable = true;
      vscodium.enable = true;
      vesktop.enable = true;
      kitty.enable = true;
      obsidian.enable = true;
    };
  };

 /* imports = [
    pkgs.nix
  ];*/
}

My full configuration is at https://github.com/Hdaboom/nixos-dotfiles for any additional context needed.

You can’t do conditional imports with a nix module system.

Instead, wrap the file in something like:

# firefox.nix
{ config, ... }: {
  options.firefox.enable = ... ;

  config = lib.mkIf config.firefox.enable {
    # config here
  };
}

Better yet, you can just not write a custom option, because home-manager already has a programs.firefox.enable, so you can simplify that:

# firefox.nix
{ config, lib, ... }: lib.mkIf config.programs.firefox.enable {
  # config here
}

And then if some other module imports that one, it will auto-activate your config if it enables firefox, and keep it disabled if firefox is not enabled:

# home.nix
{
  imports = [
    ./firefox.nix
  ];

  programs.firefox.enable = true;
}
2 Likes

My firefox.nix file is now throwing the error “Syntax Error unexpected identifier, expecting “.” or “=”.

and showing the line

config = {
  lib.mkIf config.firefox.enable {

and highlighting the config.firefox.enable

out of the file

# firefox.nix
{ config, pkgs, lib, inputs, ... }: {

  options.firefox.enable = lib.mkEnableOption "Firefox";

  config = {
    lib.mkIf config.firefox.enable {
      programs.firefox = {
        #firefox options
      };
  };
}

Yeah, you added a syntax error, nix code don’t work like that. Copy this:

# firefox.nix
{ config, pkgs, lib, inputs, ... }: {

  options.firefox.enable = lib.mkEnableOption "Firefox";

  config = lib.mkIf config.firefox.enable {
    programs.firefox = {
      #firefox options
    };
  };
}
1 Like

Yeah that worked, thanks. I’m still new to the nix language so I didn’t know

The nix language basics is quite short and really helpful. Especially for explaining things that might not be supper intuitive coming from more traditional programming languages

1 Like