Home-manager: Color scheme switcher

Created on 28 Aug 2018  路  12Comments  路  Source: nix-community/home-manager

It would be nice if there was an option to store different color schemes (+ background directories) and have the ability to switch between them. No idea how to implement it though.

I have something like this in mind:

  • Being able to choose the theme when switching: home-manager switch --theme="nature"
  • Having a configuration option where you specify the theme
  • ...

The themes would be stored in a file called themes.nix and would be referenced from the home.nix

Not sure if that was clear, let's hope it was!

Most helpful comment

FWIW I have some half-baked base16-inspired module in my NUR.

There is a bit of documentation if you search for "base16" in https://rycee.gitlab.io/nur-expressions/hm-options.html and the configuration I use is public.

All 12 comments

I think this is a great idea. Right now I just store my theme config in a
separate file, and append onto the main config file. Here's an example:

https://github.com/bsima/config/blob/master/home.nix#L46-L47

So, the general mechanism is simply "append theme.nix to the main config", which
seems simple enough to generalize across multiple modules.

On 08.28..03.51, N茅fix Estrada wrote:

It would be nice if there was an option to store different color schemes (+ background directories) and have the ability to switch between them. No idea how to implement it though.

I have something like this in mind:

  • Being able to choose the theme when switching: home-manager switch --theme="nature"
  • Having a configuration option where you specify the theme
  • ...

The themes would be stored in a file called themes.nix and would be referenced from the home.nix

Not sure if that was clear, let's hope it was!

--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/rycee/home-manager/issues/361

Yeah, I've been thinking about adding some form of theming support for a long time but haven't yet thought of a solution I would be happy with. The main difficulty is the wide variety of how programs and widget toolkits do their theming.

It most likely would end up with the programs and toolkits modules having their own theming options (e.g., like the gtk and gnome-terminal modules do today). Then it would be possible for people to create themes under some themes option name space. This should be quite possible today, just create your own mytheme.nix file as a Home Manager module, publish it somewhere, and convince people to put

imports = [ https://.../mytheme.nix ];

in their Home Manager configuration. Clearly there are security aspects to consider here since a module can do pretty much anything but in principle it works.

I was thinking more in something like having different color palettes and being able to switch between them easily (not sure if I've understood you). Right now, I handle my theming in the following way:

{ pkgs, ... }:

let

  ...

  # Colors
  color0 = "#04020b";
  color1 = "#2e3568";
  color2 = "#47346a";
  color3 = "#90327a";
  color4 = "#384f87";
  color5 = "#4e6897";
  color6 = "#c228b6";
  color7 = "#a4bdcf";
  color8 = "#728490";
  color9 = "#2e3568";
  color10 = "#47346a";
  color11 = "#90327a";
  color12 = "#384f87";
  color13 = "#4e6897";
  color14 = "#c228b6";
  color15 = "#a4bdcf";
  background = "#04020b";
  foreground = "#a4bdcf";
  cursorColor = "#a4bdcf";
in
...

But I'm sure there's a better way to do that. The thing is that I can't switch between color schemes easily

Sorry for the long delay.

In your case I think you could just make a simple module for it. For example, create a file colors.nix containing

{ lib, ... }:

with lib;

{
  options.colors =
    let
      mkColorOption = name: {
        inherit name;
        value = mkOption {
          type = types.strMatching "#[a-fA-F0-9]{6}";
          description = "Color ${name}.";
        };
      };
    in
      listToAttrs (map mkColorOption [
        "background" "foreground" "cursor" "color0" "color1" "color2"
        "color3" "color4" "color5" "color6" "color7" "color8" "color9"
        "color10" "color11" "color12" "color13" "color14" "color15"
      ]);
}

Then you can create mycolors0.nix, mycolors1.nix, etc. that may contain something like

{
  imports = [ ./colors.nix ];

  colors = {
    color0 = "#04020b";
    color1 = "#2e3568";
    color2 = "#47346a";
    color3 = "#90327a";
    color4 = "#384f87";
    color5 = "#4e6897";
    color6 = "#c228b6";
    color7 = "#a4bdcf";
    color8 = "#728490";
    color9 = "#2e3568";
    color10 = "#47346a";
    color11 = "#90327a";
    color12 = "#384f87";
    color13 = "#4e6897";
    color14 = "#c228b6";
    color15 = "#a4bdcf";
    background = "#04020b";
    foreground = "#a4bdcf";
    cursor = "#a4bdcf";
  };
}

Finally you can use this in a HM configuration. For example,

{
  imports = [ ./mycolors0.nix ];
  home.file."foo".text = "foreground is ${config.colors.foreground}\n";
}

where you can relatively easily switch colors by changing the imports line.

If you insist on being able to change the color theme directly from the home-manager command then that is also possible. If you instead have

{
  imports = [ <color-theme> ];
  home.file."foo".text = "foreground is ${config.colors.foreground}\n";
}

then you can, for example, say

$ home-manager -I color-theme=$HOME/dotfiles/mycolors0.nix switch

I am working on some "theme translators" - currently I have QT theme figured out, as well as themes for i3, dunst, polybar (although only my custom config), VSCode, kate, konsole, kdevelop and some other stuff that I've forgotten about already. All of the themes are generated from single color list. I am not yet ready to show it to the world, though, as it's very bad code style and not polished enough. (Also, it's currently made with breeze-dark in mind and you can just change fg colours; I am not sure if bg changes will work correctly)

I implemented something similar to this. Here's my modules:
https://gist.github.com/alexameen/15d07005112d1808bb9271f122f40989

This will give you Xresources colors and add a ton of shell variables filled with hex codes that could be used in a variety of progams.
This is easily extended to add support for more programs in the config itself.

I included an example color-scheme, the modules, and a snipped of my home.nix with the relevant bits.

I wrote the options so that you could snag someones Xresources lists which are commonly shared online and easily convert it to a theme by replacing all : with = and wrapping all values in "<value>"; without hassling with renaming things too much.

I am using the module from @balsoft with success: https://github.com/balsoft/nixos-config/blob/master/modules/themes.nix

@feijoas cool! Can I look at the way you are using it?

You may all be interested in https://github.com/chriskempson/base16 . They have a lot of templates to apply a colorscheme to various apps. This might be partially nix-able.

FWIW I have some half-baked base16-inspired module in my NUR.

There is a bit of documentation if you search for "base16" in https://rycee.gitlab.io/nur-expressions/hm-options.html and the configuration I use is public.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wedens picture wedens  路  6Comments

memberbetty picture memberbetty  路  5Comments

stites picture stites  路  3Comments

PierreR picture PierreR  路  5Comments

mightybyte picture mightybyte  路  5Comments