From 76a254f9fece9bbc34df043d09f43b76fa18cc1b Mon Sep 17 00:00:00 2001 From: DwarfMaster Date: Mon, 30 Jan 2023 19:25:27 +0100 Subject: [PATCH] Separate stylix into a nixos module and a home-manager module --- flake.nix | 14 ++++++--- stylix/fonts.nix | 18 ----------- stylix/hm/default.nix | 65 ++++++++++++++++++++++++++++++++++++++++ stylix/hm/fonts.nix | 16 ++++++++++ stylix/hm/palette.nix | 17 +++++++++++ stylix/nixos/default.nix | 24 +++++++++++++++ stylix/nixos/fonts.nix | 22 ++++++++++++++ stylix/nixos/palette.nix | 23 ++++++++++++++ stylix/palette.nix | 23 +++++--------- 9 files changed, 185 insertions(+), 37 deletions(-) create mode 100644 stylix/hm/default.nix create mode 100644 stylix/hm/fonts.nix create mode 100644 stylix/hm/palette.nix create mode 100644 stylix/nixos/default.nix create mode 100644 stylix/nixos/fonts.nix create mode 100644 stylix/nixos/palette.nix diff --git a/flake.nix b/flake.nix index 968484a..3e06cc3 100644 --- a/flake.nix +++ b/flake.nix @@ -59,6 +59,10 @@ nixosModules.stylix = { pkgs, ... }@args: { imports = [ + (import ./stylix/nixos { + inherit (self.packages.${pkgs.system}) palette-generator; + base16 = base16.lib args; + }) ./modules/alacritty.nix ./modules/bemenu.nix ./modules/chromium.nix @@ -82,13 +86,15 @@ ./modules/swaylock.nix ./modules/vim.nix ./modules/vscode/default.nix - (import ./stylix/palette.nix { + ]; + }; + + homeManagerModules.stylix = { pkgs, ... }@args: { + imports = [ + (import ./stylix/hm { inherit (self.packages.${pkgs.system}) palette-generator; base16 = base16.lib args; }) - ./stylix/fonts.nix - ./stylix/pixel.nix - ./stylix/target.nix ]; }; }; diff --git a/stylix/fonts.nix b/stylix/fonts.nix index b4466db..5b15606 100644 --- a/stylix/fonts.nix +++ b/stylix/fonts.nix @@ -3,8 +3,6 @@ with lib; let - cfg = config.stylix.fonts; - fontType = types.submodule { options = { package = mkOption { @@ -57,20 +55,4 @@ in { }; }; }; - - config.fonts = { - fonts = [ - cfg.monospace.package - cfg.serif.package - cfg.sansSerif.package - cfg.emoji.package - ]; - - fontconfig.defaultFonts = { - monospace = [ cfg.monospace.name ]; - serif = [ cfg.serif.name ]; - sansSerif = [ cfg.sansSerif.name ]; - emoji = [ cfg.emoji.name ]; - }; - }; } diff --git a/stylix/hm/default.nix b/stylix/hm/default.nix new file mode 100644 index 0000000..5b5351f --- /dev/null +++ b/stylix/hm/default.nix @@ -0,0 +1,65 @@ +{ palette-generator, base16 }: +{ config, lib, ... }@mod-args: + +let + inherit (lib) types; + from-nixos = mod-args ? "osConfig"; + system-theme = config.stylix.useSystemTheme; +in { + imports = [ + ../pixel.nix + ../target.nix + ./fonts.nix + (import ./palette.nix { inherit palette-generator base16; }) + ]; + + options.stylix = { + useSystemTheme = lib.mkOption { + type = types.bool; + description = '' + Use NixOS stylix theme for this user. + + Doesn't do anything if the home-manager configuration is not used + from NixOS. + ''; + default = false; + }; + }; + + config = lib.mkMerge [ + (lib.mkIf (system-theme && !from-nixos) { + warnings = [ + "stylix.useSystemTheme is ignored if the config is not used from NixOS" + ]; + }) + (lib.mkIf (system-theme && from-nixos) (let + cfg = mod-args.osConfig.stylix; + mk = lib.mkOverride 99; + in { + stylix.autoEnable = mk cfg.autoEnable; + stylix.fonts.serif = mk cfg.fonts.serif; + stylix.fonts.sansSerif = mk cfg.fonts.sansSerif; + stylix.fonts.monospace = mk cfg.fonts.monospace; + stylix.fonts.emoji = mk cfg.fonts.emoji; + stylix.polarity = mk cfg.polarity; + stylix.image = mk cfg.image; + stylix.palette.base00 = cfg.palette.base00; + stylix.palette.base01 = cfg.palette.base01; + stylix.palette.base02 = cfg.palette.base02; + stylix.palette.base03 = cfg.palette.base03; + stylix.palette.base04 = cfg.palette.base04; + stylix.palette.base05 = cfg.palette.base05; + stylix.palette.base06 = cfg.palette.base06; + stylix.palette.base07 = cfg.palette.base07; + stylix.palette.base08 = cfg.palette.base08; + stylix.palette.base09 = cfg.palette.base09; + stylix.palette.base0A = cfg.palette.base0A; + stylix.palette.base0B = cfg.palette.base0B; + stylix.palette.base0C = cfg.palette.base0C; + stylix.palette.base0D = cfg.palette.base0D; + stylix.palette.base0E = cfg.palette.base0E; + stylix.palette.base0F = cfg.palette.base0F; + stylix.base16Scheme = cfg.base16Scheme; + })) + ]; +} diff --git a/stylix/hm/fonts.nix b/stylix/hm/fonts.nix new file mode 100644 index 0000000..d6072cb --- /dev/null +++ b/stylix/hm/fonts.nix @@ -0,0 +1,16 @@ +{ config, ... }: + +let + cfg = config.stylix.fonts; +in { + imports = [ ../fonts.nix ]; + config = { + fonts.fontconfig.enable = true; + home.packages = [ + cfg.monospace.package + cfg.serif.package + cfg.sansSerif.package + cfg.emoji.package + ]; + }; +} diff --git a/stylix/hm/palette.nix b/stylix/hm/palette.nix new file mode 100644 index 0000000..b48970e --- /dev/null +++ b/stylix/hm/palette.nix @@ -0,0 +1,17 @@ +args: +{ config, ... }: + +{ + imports = [ (import ../palette.nix args) ]; + + config = { + xdg.configFile = { + # See ../nixos/palette.nix for the rational behind these two options + "stylix/palette.json".source = config.stylix.generatedJSON; + "stylix/palette.html".source = config.lib.stylix.colors { + template = builtins.readFile ../palette.html.mustache; + extension = ".html"; + }; + }; + }; +} diff --git a/stylix/nixos/default.nix b/stylix/nixos/default.nix new file mode 100644 index 0000000..3c3834d --- /dev/null +++ b/stylix/nixos/default.nix @@ -0,0 +1,24 @@ +{ palette-generator, base16 }@args: +{ config, lib, ... }: + +let + cfg = config.stylix.home-manager; +in { + imports = [ + ../pixel.nix + ../target.nix + ./fonts.nix + (import ./palette.nix { inherit palette-generator base16; }) + ]; + + options.stylix.home-manager = { + enable = lib.mkEnableOption "home-manager support"; + useSystemTheme = lib.mkEnableOption "system theme in home-manager"; + }; + + config = lib.mkIf cfg.enable { + home-manager.sharedModules = [ + { stylix.useSystemTheme = lib.mkOverride 99 cfg.useSystemTheme; } + ]; + }; +} diff --git a/stylix/nixos/fonts.nix b/stylix/nixos/fonts.nix new file mode 100644 index 0000000..bfeaee5 --- /dev/null +++ b/stylix/nixos/fonts.nix @@ -0,0 +1,22 @@ +{ config, ... }: + +let + cfg = config.stylix.fonts; +in { + imports = [ ../fonts.nix ]; + config.fonts = { + fonts = [ + cfg.monospace.package + cfg.serif.package + cfg.sansSerif.package + cfg.emoji.package + ]; + + fontconfig.defaultFonts = { + monospace = [ cfg.monospace.name ]; + serif = [ cfg.serif.name ]; + sansSerif = [ cfg.sansSerif.name ]; + emoji = [ cfg.emoji.name ]; + }; + }; +} diff --git a/stylix/nixos/palette.nix b/stylix/nixos/palette.nix new file mode 100644 index 0000000..1b40a59 --- /dev/null +++ b/stylix/nixos/palette.nix @@ -0,0 +1,23 @@ +args: +{ config, ... }: + +{ + imports = [ (import ../palette.nix args) ]; + + config = { + environment.etc = { + # Making palette.json part of the system closure will protect it from + # garbage collection, so future configurations can be evaluated without + # having to generate the palette again. The generator is not kept, only + # the palette which came from it, so this uses very little disk space. + "stylix/palette.json".source = config.stylix.generatedJSON; + + # We also provide a HTML version which is useful for viewing the colors + # during development. + "stylix/palette.html".source = config.lib.stylix.colors { + template = builtins.readFile ../palette.html.mustache; + extension = ".html"; + }; + }; + }; +} diff --git a/stylix/palette.nix b/stylix/palette.nix index affff8c..2464f64 100644 --- a/stylix/palette.nix +++ b/stylix/palette.nix @@ -41,6 +41,14 @@ in { ''; }; + generatedJSON = mkOption { + type = types.path; + description = "The result of palette-generator."; + readOnly = true; + internal = true; + default = paletteJSON; + }; + palette = genAttrs [ "base00" "base01" "base02" "base03" "base04" "base05" "base06" "base07" "base08" "base09" "base0A" "base0B" "base0C" "base0D" "base0E" "base0F" @@ -83,20 +91,5 @@ in { # This attrset can be used like a function too, see # https://github.com/SenchoPens/base16.nix#mktheme lib.stylix.colors = base16.mkSchemeAttrs cfg.base16Scheme; - - environment.etc = mkIf (cfg.base16Scheme == generatedScheme) { - # Making palette.json part of the system closure will protect it from - # garbage collection, so future configurations can be evaluated without - # having to generate the palette again. The generator is not kept, only - # the palette which came from it, so this uses very little disk space. - "stylix/palette.json".source = paletteJSON; - - # We also provide a HTML version which is useful for viewing the colors - # during development. - "stylix/palette.html".source = config.lib.stylix.colors { - template = builtins.readFile ./palette.html.mustache; - extension = ".html"; - }; - }; }; }