Separate stylix into a nixos module and a home-manager module

This commit is contained in:
DwarfMaster 2023-01-30 19:25:27 +01:00
parent 8c22082ab1
commit 76a254f9fe
9 changed files with 185 additions and 37 deletions

View file

@ -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
];
};
};

View file

@ -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 ];
};
};
}

65
stylix/hm/default.nix Normal file
View file

@ -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;
}))
];
}

16
stylix/hm/fonts.nix Normal file
View file

@ -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
];
};
}

17
stylix/hm/palette.nix Normal file
View file

@ -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";
};
};
};
}

24
stylix/nixos/default.nix Normal file
View file

@ -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; }
];
};
}

22
stylix/nixos/fonts.nix Normal file
View file

@ -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 ];
};
};
}

23
stylix/nixos/palette.nix Normal file
View file

@ -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";
};
};
};
}

View file

@ -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";
};
};
};
}