From a2c5d32b9786231cf006709d9127a0944996bb41 Mon Sep 17 00:00:00 2001 From: winston Date: Thu, 13 Jul 2023 08:45:39 +0200 Subject: [PATCH] feat: dark mode sync module --- home/apps/colorscheme-sync.nix | 56 ++++++++++++++++++++++++++++++ home/apps/default.nix | 1 + home/apps/sway.nix | 7 ---- home/apps/wezterm/keys.lua | 16 +++++++++ home/apps/wezterm/wezterm.lua | 13 ++----- home/apps/zsh.nix | 29 ++++++++++++---- machines/common/linux/xsession.nix | 7 ++-- 7 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 home/apps/colorscheme-sync.nix diff --git a/home/apps/colorscheme-sync.nix b/home/apps/colorscheme-sync.nix new file mode 100644 index 0000000..e6df6d5 --- /dev/null +++ b/home/apps/colorscheme-sync.nix @@ -0,0 +1,56 @@ +{ + config, + pkgs, + lib, + ... +}: let + # TODO: de-duplicate across modules + lat = 48.210033; + lng = 16.363449; +in { + home.packages = [ + (pkgs.writeShellApplication { + name = "dark-mode-ternary"; + runtimeInputs = [pkgs.gnugrep]; + text = let + queryCommand = + if pkgs.stdenv.isLinux + then "dbus-send --session --print-reply=literal --dest=org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read string:'org.freedesktop.appearance' string:'color-scheme' | grep -q 'uint32 1'" + else if pkgs.stdenv.isDarwin + then "defaults read -g AppleInterfaceStyle &>/dev/null" + else throw "Unsupported platform"; + in '' + [[ -z "''${1-}" ]] && [[ -z "''${2-}" ]] && echo "Usage: $0 " && exit 1 + + if ${queryCommand}; then + echo "$1" + else + echo "$2" + fi + ''; + }) + ]; + + services.darkman = { + enable = true; + config = { + inherit lat lng; + useGeoclue = false; + }; + activationScript = let + starship = lib.getExe config.programs.starship.package; + zsh = lib.getExe config.programs.zsh.package; + in { + dark = '' + ${starship} config palette catppuccin_mocha + ${zsh} -ic "fast-theme XDG:catppuccin-mocha" + ''; + light = '' + ${starship} config palette catppuccin_latte + ${zsh} -ic "fast-theme XDG:catppuccin-latte" + ''; + }; + }; + + programs.zsh.shellAliases.cat = "bat --theme=$(dark-mode-ternary 'Catppuccin-mocha' 'Catppuccin-latte')"; +} diff --git a/home/apps/default.nix b/home/apps/default.nix index a2b98b7..27c231c 100644 --- a/home/apps/default.nix +++ b/home/apps/default.nix @@ -1,6 +1,7 @@ { imports = [ ./browsers.nix + ./colorscheme-sync.nix ./discord.nix ./fonts.nix ./git.nix diff --git a/home/apps/sway.nix b/home/apps/sway.nix index 0df8f22..033a751 100644 --- a/home/apps/sway.nix +++ b/home/apps/sway.nix @@ -37,13 +37,6 @@ in { services = lib.mkIf isLinux { clipman.enable = true; - darkman = { - enable = true; - config = { - inherit lat lng; - useGeoclue = false; - }; - }; gnome-keyring = { enable = true; components = ["secrets"]; diff --git a/home/apps/wezterm/keys.lua b/home/apps/wezterm/keys.lua index 4f9fd5f..b267dfb 100644 --- a/home/apps/wezterm/keys.lua +++ b/home/apps/wezterm/keys.lua @@ -31,6 +31,21 @@ local openUrl = act.QuickSelectArgs({ end), }) +local changeCtpFlavor = act.InputSelector({ + title = "Change Catppuccin flavor", + choices = { + { label = "Mocha" }, + { label = "Macchiato" }, + { label = "Frappe" }, + { label = "Latte" }, + }, + action = wezterm.action_callback(function(window, _, _, label) + if label then + window:set_config_overrides({ color_scheme = "Catppuccin " .. label }) + end + end), +}) + -- use 'Backslash' to split horizontally map("\\", "LEADER", act.SplitHorizontal({ domain = "CurrentPaneDomain" })) -- and 'Minus' to split vertically @@ -53,6 +68,7 @@ map("l", "LEADER|SHIFT", act.AdjustPaneSize({ "Right", 5 })) -- spawn & close map("c", "LEADER", act.SpawnTab("CurrentPaneDomain")) map("x", "LEADER", act.CloseCurrentPane({ confirm = true })) +map("t", "LEADER", changeCtpFlavor) map("t", { "SHIFT|CTRL", "SUPER" }, act.SpawnTab("CurrentPaneDomain")) map("w", { "SHIFT|CTRL", "SUPER" }, act.CloseCurrentTab({ confirm = true })) map("n", { "SHIFT|CTRL", "SUPER" }, act.SpawnWindow) diff --git a/home/apps/wezterm/wezterm.lua b/home/apps/wezterm/wezterm.lua index c23d30a..adf188b 100644 --- a/home/apps/wezterm/wezterm.lua +++ b/home/apps/wezterm/wezterm.lua @@ -1,12 +1,6 @@ ---@type wezterm local wezterm = require("wezterm") -local utils = require("utils") - -local c = {} -if wezterm.config_builder then - c = wezterm.config_builder() - c:set_strict_mode(true) -end +local c = wezterm.config_builder() require("keys").apply(c) @@ -32,13 +26,12 @@ c.launch_menu = { } c.command_palette_font_size = 13.0 c.window_frame = { font_size = 13.0 } -c.window_background_opacity = 0.9 +c.window_background_opacity = 0.95 wezterm.plugin .require("https://github.com/catppuccin/wezterm") .apply_to_config(c, { - flavor = "mocha", - sync = utils.is_darwin(), + sync = true, sync_flavors = { light = "latte", dark = "mocha" }, }) wezterm.plugin diff --git a/home/apps/zsh.nix b/home/apps/zsh.nix index 3206ed4..105b2c4 100644 --- a/home/apps/zsh.nix +++ b/home/apps/zsh.nix @@ -4,7 +4,6 @@ pkgs, ... }: let - inherit (pkgs.stdenv.hostPlatform) isDarwin; symlink = fileName: {recursive ? false}: { source = config.lib.file.mkOutOfStoreSymlink "${flakePath}/${fileName}"; recursive = recursive; @@ -26,7 +25,20 @@ in { sync_frequency = "5m"; }; }; - bat.enable = true; + bat = let + themes = pkgs.fetchFromGitHub { + owner = "catppuccin"; + repo = "bat"; + rev = "ba4d16880d63e656acced2b7d4e034e4a93f74b1"; + sha256 = "sha256-6WVKQErGdaqb++oaXnY3i6/GuH2FhTgK0v4TN4Y0Wbw="; + }; + in { + enable = true; + themes = { + "Catppuccin-latte" = builtins.readFile (themes + /Catppuccin-latte.tmTheme); + "Catppuccin-mocha" = builtins.readFile (themes + /Catppuccin-mocha.tmTheme); + }; + }; btop = { enable = true; settings = { @@ -120,15 +132,18 @@ in { file = "share/zsh-nix-shell/nix-shell.plugin.zsh"; } { - src = zsh-fast-syntax-highlighting; + src = zsh-fast-syntax-highlighting.overrideAttrs (old: { + src = fetchFromGitHub { + owner = "zdharma-continuum"; + repo = "fast-syntax-highlighting"; + rev = "cf318e06a9b7c9f2219d78f41b46fa6e06011fd9"; + hash = "sha256-RVX9ZSzjBW3LpFs2W86lKI6vtcvDWP6EPxzeTcRZua4="; + }; + }); file = "share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh"; } ]); shellAliases = { - cat = - if isDarwin - then "bat --theme=\$(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo Catppuccin-mocha || echo Catppuccin-latte)" - else "bat"; cp = "cp -i"; mv = "mv -i"; rm = "rm -i"; diff --git a/machines/common/linux/xsession.nix b/machines/common/linux/xsession.nix index c267d3c..48b5185 100644 --- a/machines/common/linux/xsession.nix +++ b/machines/common/linux/xsession.nix @@ -40,11 +40,12 @@ gtk = true; }; }; - # needed for gnome3 pinentry - services.dbus.packages = [pkgs.gcr]; + + # gcr needed for gnome3 pinentry + services.dbus.packages = with pkgs; [darkman gcr]; xdg.portal.enable = true; xdg.portal.wlr.enable = true; - xdg.portal.extraPortals = [pkgs.xdg-desktop-portal-gtk]; + xdg.portal.extraPortals = with pkgs; [darkman xdg-desktop-portal-gtk]; services = { # mounting