From 2757c649b981b363c5bbf33f4862be7edae2dd3c Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Thu, 10 Mar 2022 21:59:18 +0200
Subject: [PATCH] feat: dynamic theming attempt

---
 dotfiles/neovim/lua/my/options.lua            |  2 +-
 dotfiles/neovim/lua/my/plugins/lualine.lua    |  2 +-
 modules/applications/alacritty.nix            | 39 ++++++++++---------
 modules/applications/neovim.nix               | 25 ++++++++++--
 modules/applications/shells/aliases.nix       |  2 +
 .../applications/shells/sessionVariables.nix  |  3 ++
 modules/applications/tmux.nix                 | 14 +++++--
 modules/applications/xmonad/Main.hs           |  4 +-
 modules/default.nix                           |  2 +-
 modules/helpers.nix                           |  8 ++++
 modules/overlays/default.nix                  |  1 +
 modules/overlays/flakes.nix                   |  3 +-
 modules/overlays/myPackages.nix               |  5 +++
 modules/{theme => themes}/default.nix         |  0
 modules/{theme => themes}/fonts.nix           |  0
 modules/themes/githubVariant.nix              | 29 ++++++++++++++
 modules/themes/theme.nix                      |  2 +
 modules/themes/themes.nix                     |  9 +++++
 18 files changed, 120 insertions(+), 30 deletions(-)
 create mode 100644 modules/helpers.nix
 create mode 100644 modules/overlays/myPackages.nix
 rename modules/{theme => themes}/default.nix (100%)
 rename modules/{theme => themes}/fonts.nix (100%)
 create mode 100644 modules/themes/githubVariant.nix
 create mode 100644 modules/themes/theme.nix
 create mode 100644 modules/themes/themes.nix

diff --git a/dotfiles/neovim/lua/my/options.lua b/dotfiles/neovim/lua/my/options.lua
index 5c6d04e..f95d801 100644
--- a/dotfiles/neovim/lua/my/options.lua
+++ b/dotfiles/neovim/lua/my/options.lua
@@ -33,7 +33,7 @@ function M.setup()
     helpers.global("mapleader", "<Space>")
 
     -- Set theme
-    require('github-theme').setup({theme_style = "light", dark_float = true})
+    -- require('github-theme').setup({theme_style = "light", dark_float = true, transparent = true})
 end
 
 return M
diff --git a/dotfiles/neovim/lua/my/plugins/lualine.lua b/dotfiles/neovim/lua/my/plugins/lualine.lua
index 950ed80..c8a24c0 100644
--- a/dotfiles/neovim/lua/my/plugins/lualine.lua
+++ b/dotfiles/neovim/lua/my/plugins/lualine.lua
@@ -2,7 +2,7 @@ local M = {}
 
 function M.setup()
     require('lualine').setup({
-        theme = "github",
+        theme = vim.g.lualineTheme,
 
         -- Integration with other plugins
         extensions = {"nvim-tree"}
diff --git a/modules/applications/alacritty.nix b/modules/applications/alacritty.nix
index 29dc4a0..f40508d 100644
--- a/modules/applications/alacritty.nix
+++ b/modules/applications/alacritty.nix
@@ -1,29 +1,32 @@
-{ pkgs, ... }:
+{ pkgs, lib, ... }:
 
 let
-  githubAlacrittyTheme =
-    "${pkgs.githubNvimTheme}/terminal/alacritty/github_light.yml";
+  themes = pkgs.myThemes;
+
+  createTheme = (theme: {
+    xdg.configFile."alacritty/themes/${theme.name}.yml".text = builtins.toJSON
+      (lib.attrs.recursiveUpdate theme.alacritty.settings {
+        import = [ "~/.config/alacritty/alacritty.yml" ];
+      });
+  });
+
+  createThemeConfigs = lib.lists.foldr
+    (acc: theme: lib.attrs.recursiveUpdate acc (createTheme theme))
+    { }
+    themes;
 in
 {
+  imports = [
+    {
+      home-manager.users.adrielus = createThemeConfigs;
+    }
+  ];
+
   home-manager.users.adrielus.programs.alacritty = {
     enable = true;
 
     settings = {
-      import = [ githubAlacrittyTheme ];
-
-      window = {
-        decorations = "none";
-
-        padding = {
-          x = 8;
-          y = 8;
-        };
-
-        gtk_theme_variant = "light";
-      };
-
-      # transparent bg:)
-      # background_opacity = 0.6;
+      window.decorations = "none";
       fonts.normal.family = "Nerd Font Source Code Pro";
     };
   };
diff --git a/modules/applications/neovim.nix b/modules/applications/neovim.nix
index 16a22bb..ef1f155 100644
--- a/modules/applications/neovim.nix
+++ b/modules/applications/neovim.nix
@@ -1,10 +1,30 @@
-{ pkgs, ... }:
+{ pkgs, lib, ... }:
 let
+  themes = pkgs.myThemes;
+
   # config-nvim = "/etc/nixos/configuration/dotfiles/neovim";
   config-nvim = pkgs.vimUtils.buildVimPluginFrom2Nix {
     name = "config-nvim";
     src = ../../dotfiles/neovim;
   };
+
+  themePlugins = lib.lists.concatMap (theme: theme.neovim.plugins) themes;
+
+  loadTheme = (theme: ''
+    if currentTheme = "${theme.name}" then
+      ${theme.neovim.theme}
+
+      vim.g.lualineTheme = ${theme.neovim.lualineTheme}
+    end
+  '');
+
+  loadThemes = ''
+    local currentTheme = os.getenv("THEME");
+
+    ${pkgs.myHelpers.mergeLines (lib.lists.forEach themes loadTheme)};
+  '';
+
+
 in
 {
   home-manager.users.adrielus.programs.neovim = {
@@ -37,9 +57,8 @@ in
     ];
 
     plugins = with pkgs.vimPlugins;
-      with pkgs.vimExtraPlugins; with pkgs.myVimPlugins;  [
+      with pkgs.vimExtraPlugins; with pkgs.myVimPlugins; themePlugins + [
         config-nvim # my neovim config
-        github-nvim-theme # github theme for neovim
         nvim-lspconfig # configures lsps for me
         nvim-autopairs # close pairs for me
         telescope-nvim # fuzzy search for say opening files
diff --git a/modules/applications/shells/aliases.nix b/modules/applications/shells/aliases.nix
index 3534f35..9613d4f 100644
--- a/modules/applications/shells/aliases.nix
+++ b/modules/applications/shells/aliases.nix
@@ -23,4 +23,6 @@
 
   # Render git repo using gource
   "git-render" = "gource -f -s 1 -c 4 --key";
+
+  alacritty = "alacritty --config-file $XDG_CONFIG_HOME/alacritty/themes/$THEME.yml";
 }
diff --git a/modules/applications/shells/sessionVariables.nix b/modules/applications/shells/sessionVariables.nix
index c511c5a..b79204b 100644
--- a/modules/applications/shells/sessionVariables.nix
+++ b/modules/applications/shells/sessionVariables.nix
@@ -8,6 +8,9 @@ let
 
     # Sets neovim as default editor
     EDITOR = "nvim";
+
+    # Sets the current theme used by all programs
+    THEME = "github-light";
   };
 in
 {
diff --git a/modules/applications/tmux.nix b/modules/applications/tmux.nix
index b70107c..ed4d35a 100644
--- a/modules/applications/tmux.nix
+++ b/modules/applications/tmux.nix
@@ -1,4 +1,12 @@
-{ pkgs, ... }: {
+{ pkgs, lib, ... }:
+let
+  sourceTmuxTheme = (theme: ''
+    # Only load this theme if it's the current one
+    if '[[ "$THEME" =~ ${theme.name} ]]' 'source ${theme.tmux.path}'
+  '');
+  tmuxThemes = pkgs.myHelpers.mergeLines (lib.lists.forEach pkgs.myThemes sourceTmuxTheme);
+in
+{
   home-manager.users.adrielus.programs = {
     # Add tmux-navigator plugin to neovim
     neovim.extraPackages = [ pkgs.vimPlugins.vim-tmux-navigator ];
@@ -16,8 +24,8 @@
       ];
 
       extraConfig = ''
-        # Use github light theme
-        source-file ${pkgs.githubNvimTheme}/terminal/tmux/github_light.conf
+        # Load every theme available
+        ${tmuxThemes}
 
         # load the rest of the config
         source-file ${../../dotfiles/tmux/tmux.conf}
diff --git a/modules/applications/xmonad/Main.hs b/modules/applications/xmonad/Main.hs
index b0a6e26..634b461 100644
--- a/modules/applications/xmonad/Main.hs
+++ b/modules/applications/xmonad/Main.hs
@@ -95,8 +95,8 @@ main =
 
     startup :: X ()
     startup = do
-      spawn "xwallpaper --zoom ./background.jpg"
+      spawn "xwallpaper --zoom ./picutres/portal.png"
 
 -- spawn "Discord"
 -- spawn "google-chrome-stable"
--- spawn "alacritty"
\ No newline at end of file
+-- spawn "alacritty"
diff --git a/modules/default.nix b/modules/default.nix
index 33a93b6..96e6560 100644
--- a/modules/default.nix
+++ b/modules/default.nix
@@ -2,7 +2,7 @@
   imports = [
     ./dev
     ./applications
-    ./theme
+    ./themes
     ./overlays
 
     ./network.nix
diff --git a/modules/helpers.nix b/modules/helpers.nix
new file mode 100644
index 0000000..8ed12f7
--- /dev/null
+++ b/modules/helpers.nix
@@ -0,0 +1,8 @@
+{ lib, ... }: {
+  mergeLines = (lines: lib.foldr
+    (a: b: ''
+      ${a}
+      ${b}
+    '') ""
+    lines);
+}
diff --git a/modules/overlays/default.nix b/modules/overlays/default.nix
index 7059e18..8e78ff4 100644
--- a/modules/overlays/default.nix
+++ b/modules/overlays/default.nix
@@ -1,6 +1,7 @@
 { pkgs, ... }: {
   nixpkgs.overlays = [
     (import ./tweakSources.nix)
+    (import ./myPackages.nix)
     (import ./npm.nix)
 
     # I hope this works (spoiler: it did not)
diff --git a/modules/overlays/flakes.nix b/modules/overlays/flakes.nix
index 7216e0a..22edf56 100644
--- a/modules/overlays/flakes.nix
+++ b/modules/overlays/flakes.nix
@@ -59,10 +59,11 @@ in
     };
   };
 
-  githubNvimTheme = githubNvimTheme;
 
   # Vim plugins
   myVimPlugins = {
+    githubNvimTheme = githubNvimTheme;
+
     telescope-file-browser-nvim =
       plugin "file_browser" telescope-file-browser-nvim;
 
diff --git a/modules/overlays/myPackages.nix b/modules/overlays/myPackages.nix
new file mode 100644
index 0000000..982ffa3
--- /dev/null
+++ b/modules/overlays/myPackages.nix
@@ -0,0 +1,5 @@
+self: super:
+with self; {
+  myHelpers = self.callPackage (import ../helpers.nix) { };
+  myThemes = self.callPackage (import ../themes/themes.nix) { };
+}
diff --git a/modules/theme/default.nix b/modules/themes/default.nix
similarity index 100%
rename from modules/theme/default.nix
rename to modules/themes/default.nix
diff --git a/modules/theme/fonts.nix b/modules/themes/fonts.nix
similarity index 100%
rename from modules/theme/fonts.nix
rename to modules/themes/fonts.nix
diff --git a/modules/themes/githubVariant.nix b/modules/themes/githubVariant.nix
new file mode 100644
index 0000000..e7f0299
--- /dev/null
+++ b/modules/themes/githubVariant.nix
@@ -0,0 +1,29 @@
+variant: { pkgs, ... }: {
+  name = "github-${variant}";
+  neovim = {
+    plugins = [
+      pkgs.myVimPlugins.github-nvim-theme # github theme for neovim
+    ];
+
+    theme = ''
+      require('github-theme').setup({theme_style = "light", dark_float = true, transparent = true})
+    '';
+
+    lualineTheme = "github";
+  };
+  tmux.path = "${pkgs.githubNvimTheme}/terminal/tmux/github_light.conf";
+  alacritty.settings = {
+    import = [ "${pkgs.githubNvimTheme}/terminal/alacritty/github_light.yml" ];
+    window = {
+      padding = {
+        x = 8;
+        y = 8;
+      };
+
+      # transparent bg:)
+      background_opacity = 0.8;
+
+      gtk_theme_variant = "light";
+    };
+  };
+}
diff --git a/modules/themes/theme.nix b/modules/themes/theme.nix
new file mode 100644
index 0000000..139597f
--- /dev/null
+++ b/modules/themes/theme.nix
@@ -0,0 +1,2 @@
+
+
diff --git a/modules/themes/themes.nix b/modules/themes/themes.nix
new file mode 100644
index 0000000..57b2937
--- /dev/null
+++ b/modules/themes/themes.nix
@@ -0,0 +1,9 @@
+{ pkgs }:
+let githubVariant = import ./githubVariant.nix;
+in
+lib.map (theme: pkgs.callPackage theme { }) [
+  githubVariant
+  "light"
+  githubVariant
+  "dark"
+]