Switching theme across programs with home manager

I would like to use Nix to manage themes (light/dark) across programs. I use home-manager, which can set the theme for some programs (e.g. rofi, via programs.rofi.theme).

For other programs, (e.g. terminal, emacs), I can set the theme via a config file, managed by home-manager via home.<file>.source.

  1. How can I group several options into one?
  2. Is it possible make an option which affects only part of a managed config file, while the rest of the file is managed via home.<file>.source?

For switching themes, I am thinking of passing an environment variable before running nixos-rebuild switch, similar to what is done here: NixOS Config Switching - Kevin Cox
Another related question: is it possible to speed up nixos-rebuild switch, so the theme switching wouldn’t take 2-5 seconds each time?

Welcome to the community! :slight_smile:

There is an ongoing issue Color scheme switcher · Issue #361 · nix-community/home-manager · GitHub on how other people are trying to use themes with home-manager.

But in my opinion switching themes with home-manager or NixOS is the wrong tool for the job.
I packaged wpgtk for NixOS a long time ago when I didn’t use home manager (actually at moment I’m not really using home-manager nor wpgtk :thinking: ).
In my eyes something like wpgtk is definitively a way better solution than rebuilding your system every time you want to make a change to the theme. I haven’t taken the time yet to investigate if and how it is possible to integrate wpgtk with home-manager/nixos in a reasonable way tho (I’m always busy with other things :sweat_smile: ).
I’m thinking that it it might be possible to have an option wpgtk.configfiles. All the files added to wpgtk.configfiles would be written to .config/wpg/templates instead of where they should go. Wpgtk is then responsible to write the configfiles with the proper theme settings to their final location and reload necessary services. This would add the possibility to switch and manipulate themes on the fly and also be way faster than nixos-rebuilt ever can be… The general wpgtk features for theme generation are in my opinion also pretty nice to have :grin:

If you’re familiar with the module system in home manager/nixpkgs, you can create a module for yourself with an option as to what type of theme you’d like to have.

A trivial example:

{ config, lib, pkgs, ... }:
{
  options.ui.darkTheme = {
    type = lib.types.bool;
    default = true;
    example = false;
    description = "If ui programs should use a dark or light theme";
  };
}

This option will be available in your other modules as part of the config argument passed to those module functions under config.ui.darkTheme assuming all the imports are done correctly. You can use this variable to conditionally set the theming values appropriate for the particular program.

Be consious of the namespace you use, you wouldn’t want to conflict with any other config namespaces used in home manager, I personally use my initials to avoid conflicts.

In your top level delcaration (probably home.nix) if you ever want to change from the default dark to a light theme, just add: config.ui.darkTheme = false; to that file and rebuild and all the light theme variants should be configured correctly.