From 98166d74cd8bd36a4b7e8795c26ec5b31a1a8811 Mon Sep 17 00:00:00 2001 From: Addison Beck Date: Tue, 12 Nov 2024 15:33:30 -0500 Subject: [PATCH] Automate toggling the system theme --- config/system-theme.nix | 1 + flake.nix | 28 ++++++- system/with/home-manager.nix | 6 +- system/with/user/me.nix | 1 + .../with/program/neovim/colors/default.nix | 4 +- .../with/user/with/program/neovim/default.nix | 78 +++++++++---------- system/with/user/with/program/stylix.nix | 4 +- .../with/user/with/program/toggle-theme.nix | 10 +++ 8 files changed, 81 insertions(+), 51 deletions(-) create mode 100644 config/system-theme.nix create mode 100644 system/with/user/with/program/toggle-theme.nix diff --git a/config/system-theme.nix b/config/system-theme.nix new file mode 100644 index 0000000..4cabd13 --- /dev/null +++ b/config/system-theme.nix @@ -0,0 +1 @@ +"light" diff --git a/flake.nix b/flake.nix index f481a09..dbefd89 100644 --- a/flake.nix +++ b/flake.nix @@ -46,12 +46,14 @@ inherit (self) outputs; supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"]; forAllSystemTypes = fn: nixpkgs.lib.genAttrs supportedSystems fn; + systemTheme = import ./config/system-theme.nix; + rootPath = ./.; in { nixosConfigurations = { minecraft = inputs.nixpkgs.lib.nixosSystem { system = "x86_64-linux"; specialArgs = { - inherit inputs outputs nixpkgs; + inherit inputs outputs nixpkgs rootPath systemTheme; pkgs-forked = import inputs.nixpkgs-forked { system = "x86_64-linux"; config.allowUnfree = true; @@ -64,7 +66,8 @@ bw = nix-darwin.lib.darwinSystem { system = "aarch64-darwin"; specialArgs = { - inherit inputs outputs nixpkgs; + inherit inputs outputs nixpkgs rootPath systemTheme; + hostname = "bw"; pkgs-forked = import inputs.nixpkgs-forked { system = "aarch64-darwin"; config.allowUnfree = true; @@ -82,7 +85,8 @@ air = nix-darwin.lib.darwinSystem { system = "aarch64-darwin"; specialArgs = { - inherit inputs outputs nixpkgs; + inherit inputs outputs nixpkgs rootPath systemTheme; + hostname = "air"; pkgs-forked = import inputs.nixpkgs-forked { system = "aarch64-darwin"; config.allowUnfree = true; @@ -156,6 +160,24 @@ pkgs.nixd ]; }; + toggle-theme = let + newSystemTheme = if systemTheme == "dark" + then { name = "light"; darwinBool = "false"; } + else { name = "dark"; darwinBool = "true"; }; + in pkgs.mkShell { + inputsFrom = with self.devShells.${system}; [ + building + ]; + packages = [ + (pkgs.writeScriptBin "nix-toggle-theme" '' + cd ~/nix + echo "\"${newSystemTheme.name}\"" > "config/system-theme.nix" + osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to ${newSystemTheme.darwinBool}' + rebuild $1 + kill -SIGUSR1 $KITTY_PID + '') + ]; + }; }); }; } diff --git a/system/with/home-manager.nix b/system/with/home-manager.nix index f90a420..56daa77 100644 --- a/system/with/home-manager.nix +++ b/system/with/home-manager.nix @@ -3,9 +3,13 @@ outputs, nixpkgs, pkgs-forked, + rootPath, + systemTheme, + hostname, ... }: { home-manager.useGlobalPkgs = true; home-manager.useUserPackages = false; - home-manager.extraSpecialArgs = {inherit inputs outputs nixpkgs pkgs-forked;}; + home-manager.extraSpecialArgs = {inherit inputs outputs nixpkgs pkgs-forked + rootPath systemTheme hostname;}; } diff --git a/system/with/user/me.nix b/system/with/user/me.nix index 06865e5..e76e886 100644 --- a/system/with/user/me.nix +++ b/system/with/user/me.nix @@ -100,6 +100,7 @@ #./with/program/opentoonz.nix <- This doesn't work ./with/program/gimp.nix ./with/program/audacity.nix + ./with/program/toggle-theme.nix ]; home.sessionPath = []; diff --git a/system/with/user/with/program/neovim/colors/default.nix b/system/with/user/with/program/neovim/colors/default.nix index b01e189..a5e9581 100644 --- a/system/with/user/with/program/neovim/colors/default.nix +++ b/system/with/user/with/program/neovim/colors/default.nix @@ -1,6 +1,6 @@ -{...}: { +{systemTheme, ...}: { options = { - background = "dark"; + background = "${systemTheme}"; termguicolors = false; }; highlights = { diff --git a/system/with/user/with/program/neovim/default.nix b/system/with/user/with/program/neovim/default.nix index d7e2ff2..328a371 100644 --- a/system/with/user/with/program/neovim/default.nix +++ b/system/with/user/with/program/neovim/default.nix @@ -3,6 +3,7 @@ pkgs, lib, inputs, + systemTheme, ... }: let options = @@ -10,28 +11,53 @@ import ./options {} ) // ( - (import ./colors {}).options + (import ./colors { inherit systemTheme; }).options ); in { programs.nixvim = { enable = true; vimAlias = true; opts = options; - highlight = (import ./colors {}).highlights; - colorschemes = (import ./colors {}).colorscheme; - highlightOverride = (import ./colors {}).highlightOverrides; - + highlight = (import ./colors { inherit systemTheme; }).highlights; + colorschemes = (import ./colors { inherit systemTheme; }).colorscheme; + highlightOverride = (import ./colors { inherit systemTheme; }).highlightOverrides; + plugins = ((import ./plugins { inherit inputs pkgs; }).plugins); + extraPlugins = ((import ./plugins { inherit inputs pkgs; }).extraPlugins); + autoCmd = ((import ./auto-commands {}).autoCommands); + keymaps = + [ + # Custom nixvim style keymaps can be added here if needed, but I + # stick to using `mkVimKeymaps` and the `commands` data structure it + # references. + ] + ++ (import ./commands {inherit lib;}).keymaps; + userCommands = + { + # Custom nixvim style commands can be added here if needed, but I + # stick to using `mkVimUserCommand` and the `commands` data structure + # it references. + } + // (import ./commands {inherit lib;}).userCommands; + diagnostics = { + signs = false; + underline = true; + update_in_insert = false; + float = { + focused = false; + style = "minimal"; + border = "rounded"; + source = "always"; + header = ""; + prefix = ""; + }; + }; globals.netrw_banner = 0; - # very cool - extraConfigVim = '' set laststatus=0 hi! link StatusLine Normal hi! link StatusLineNC Normal set statusline=%{repeat('─',winwidth('.'))} ''; - - extraConfigLua = '' require('where-am-i').setup({ features = { @@ -130,39 +156,5 @@ in { italic = true, }) ''; - diagnostics = { - signs = false; - underline = true; - update_in_insert = false; - float = { - focused = false; - style = "minimal"; - border = "rounded"; - source = "always"; - header = ""; - prefix = ""; - }; - }; - - plugins = ((import ./plugins { inherit inputs pkgs; }).plugins); - extraPlugins = ((import ./plugins { inherit inputs pkgs; }).extraPlugins); - - autoCmd = ((import ./auto-commands {}).autoCommands); - - keymaps = - [ - # Custom nixvim style keymaps can be added here if needed, but I - # stick to using `mkVimKeymaps` and the `commands` data structure it - # references. - ] - ++ (import ./commands {inherit lib;}).keymaps; - - userCommands = - { - # Custom nixvim style commands can be added here if needed, but I - # stick to using `mkVimUserCommand` and the `commands` data structure - # it references. - } - // (import ./commands {inherit lib;}).userCommands; }; } diff --git a/system/with/user/with/program/stylix.nix b/system/with/user/with/program/stylix.nix index 51b7d86..05bb36c 100644 --- a/system/with/user/with/program/stylix.nix +++ b/system/with/user/with/program/stylix.nix @@ -1,7 +1,7 @@ -{pkgs, ...}: { +{pkgs, systemTheme, ...}: { stylix.enable = true; stylix.image = ../wallpaper/empty.png; - stylix.base16Scheme = "${pkgs.base16-schemes}/share/themes/gruvbox-dark-hard.yaml"; + stylix.base16Scheme = "${pkgs.base16-schemes}/share/themes/gruvbox-${systemTheme}-hard.yaml"; stylix.fonts = { serif = { diff --git a/system/with/user/with/program/toggle-theme.nix b/system/with/user/with/program/toggle-theme.nix new file mode 100644 index 0000000..d02b17e --- /dev/null +++ b/system/with/user/with/program/toggle-theme.nix @@ -0,0 +1,10 @@ +#defaults write -g NSRequiresAquaSystemAppearance -bool ${newSystemTheme.darwinBool} +{pkgs, hostname, ...}: { + home.packages = [ + (pkgs.writeShellScriptBin "toggle-theme" '' + cd ~/nix + nix develop .#toggle-theme --command nix-toggle-theme ${hostname} + '') + ]; +} +