danth / stylix

System-wide colorscheming and typography for NixOS
https://stylix.danth.me/
MIT License
1.05k stars 124 forks source link

treewide: avoid IFD #159

Open lovesegfault opened 11 months ago

lovesegfault commented 11 months ago

The main drawback of using stylix is how much it relies on IFD for setting themes, which bogs down evaluation times and causes basic commands like nix flake show to stop working.

In my config, I found that the following targets all rely on IFD:

Looking at the modules, though, they mostly don't need IFD, and could be reworked to avoid it. One example is bat, where I was able to remove the need for IFD by improving the underlying HomeManager module.

I think most of the others on the list can also avoid IFD by just linking the theme file, and then referencing the linked path instead of trying to embed the theme in the configuration files.

The exception to this is xresources, where include directives rely on a C preprocessor, which is disabled by a lot of display managers.

lovesegfault commented 11 months ago

I closed some of the PRs because they didn't actually solve the IFD, but rather just moved it around.

I think the right solution there might be to turn most of those fetchFromGitHubs into flake inputs

lovesegfault commented 11 months ago

I guess all the templates would need to be added as flake inputs, and then we could have a moduleArgs inputs that would let individual modules reference inputs.base16-alacritty, for example.

danth commented 11 months ago

Also, some of the templates are quite short, so we could consider just embedding them as a string.

lovesegfault commented 11 months ago

Alright, with the two open PRs listed above and https://github.com/danth/stylix/pull/165 we get rid of all IFD except for xresources, which I can't see a clear way to do without IFD, but folks can also just disable it :)

lovesegfault commented 11 months ago

Another plus of the approach taken in #165 is that it's not possible to easily override the templates used, and they get auto-updated with the rest of the flake inputs :)

lovesegfault commented 11 months ago

I'm now running all these changes on my systems, everything working AOK and evaluation is much faster 🚀

lovesegfault commented 11 months ago

I may have a solution for xresources.

All home-manager does is create the .Xresources file: https://github.com/nix-community/home-manager/blob/master/modules/xresources.nix#L90-L100

And then configure the xsession to merge it to xrdb: https://github.com/nix-community/home-manager/blob/master/modules/xresources.nix#L102

And that is just a command that points to the file: https://github.com/nix-community/home-manager/blob/master/modules/xresources.nix#L26

We could do something similar with:

{
  home.file.".Xstylix" = {
    source = theme;
    onChange = ''
      if [[ -v DISPLAY ]]; then
        ${pkgs.xorg.xrdb}/bin/xrdb -merge ${config.home.homeDirectory}/.Xstylix
      fi
    '';
  };

  xsession.initExtra = "${pkgs.xorg.xrdb}/bin/xrdb -merge ${config.home.homeDirectory}/.Xstylix";
}

And that does no IFD.

I can't really test this because I don't know what the .Xresources theme even impacts.

cc. @danth

danth commented 11 months ago

It should have an effect on rxvt and xterm.

I think we can avoid linking the file too:

let
  # theme = ;
  command = "${pkgs.xorg.xrdb}/bin/xrdb -merge ${theme}";

in {
  xsession.initExtra = command;

  home.activation.Xresources = lib.hm.dag.mkEntryAfter [ "writeBoundary" ] ''
    if [[ -v DISPLAY ]]; then
      $DRY_RUN_CMD ${command}
    fi
  '';
}

On the other hand the template is fairly simple, so we could translate it to xresources.properties and remove the dependency:

{
  xresources.properties =
    with config.lib.stylix.colors.withHashtag;
    {
      "*foreground" = base05;
      "*cursorColor" = base05;
      ...
    };
}

Then it goes through the Home Manager module as usual.

lovesegfault commented 11 months ago

Ah, good idea, let me give it a shot :)

lovesegfault commented 11 months ago

Seems to work! Done in https://github.com/danth/stylix/pull/168

trueNAHO commented 6 months ago

Looking at the modules, though, they mostly don't need IFD, and could be reworked to avoid it. One example is bat, where I was able to remove the need for IFD by improving the underlying HomeManager module.

I think most of the others on the list can also avoid IFD by just linking the theme file, and then referencing the linked path instead of trying to embed the theme in the configuration files.

(Source: https://github.com/danth/stylix/issues/159#issue-1931474198)

Would replacing the current Mustache templates with upstream Home Manager and NixOS options not further improve evaluation time? Additionally, unlike Mustache templates, Nix options can be seamlessly overwritten by end users in Stylix, Home Manager, or NixOS.

I guess all the templates would need to be added as flake inputs, and then we could have a moduleArgs inputs that would let individual modules reference inputs.base16-alacritty, for example.

(Source: https://github.com/danth/stylix/issues/159#issuecomment-1751810834)

For reference, this is quite similar to what https://github.com/danth/stylix/pull/222 does.

danth commented 6 months ago

Yes, using Nix options is a better option.