Improved default of options when used from home-manager

This commit is contained in:
DwarfMaster 2023-02-02 00:14:30 +01:00
parent 83456eceb6
commit 610632be8e
7 changed files with 49 additions and 97 deletions

View file

@ -1,8 +1,11 @@
{ pkgs, config, lib, ... }: { pkgs, config, lib, ... } @ args:
with lib; with lib;
let let
fromOs = import ./fromos.nix { inherit lib args; };
fontType = types.submodule { fontType = types.submodule {
options = { options = {
package = mkOption { package = mkOption {
@ -22,7 +25,7 @@ in {
serif = mkOption { serif = mkOption {
description = "Serif font."; description = "Serif font.";
type = fontType; type = fontType;
default = { default = fromOs [ "fonts" "serif" ] {
package = pkgs.dejavu_fonts; package = pkgs.dejavu_fonts;
name = "DejaVu Serif"; name = "DejaVu Serif";
}; };
@ -31,7 +34,7 @@ in {
sansSerif = mkOption { sansSerif = mkOption {
description = "Sans-serif font."; description = "Sans-serif font.";
type = fontType; type = fontType;
default = { default = fromOs [ "fonts" "sansSerif" ] {
package = pkgs.dejavu_fonts; package = pkgs.dejavu_fonts;
name = "DejaVu Sans"; name = "DejaVu Sans";
}; };
@ -40,7 +43,7 @@ in {
monospace = mkOption { monospace = mkOption {
description = "Monospace font."; description = "Monospace font.";
type = fontType; type = fontType;
default = { default = fromOs [ "fonts" "monospace" ] {
package = pkgs.dejavu_fonts; package = pkgs.dejavu_fonts;
name = "DejaVu Sans Mono"; name = "DejaVu Sans Mono";
}; };
@ -49,7 +52,7 @@ in {
emoji = mkOption { emoji = mkOption {
description = "Emoji font."; description = "Emoji font.";
type = fontType; type = fontType;
default = { default = fromOs [ "fonts" "emoji" ] {
package = pkgs.noto-fonts-emoji; package = pkgs.noto-fonts-emoji;
name = "Noto Color Emoji"; name = "Noto Color Emoji";
}; };

6
stylix/fromos.nix Normal file
View file

@ -0,0 +1,6 @@
{ lib, args }:
path: default:
if (args ? "osConfig" && args.osConfig.stylix.homeManagerIntegration.enable)
then lib.attrByPath path default args.osConfig.stylix
else default

View file

@ -1,10 +1,7 @@
{ palette-generator, base16 }: { palette-generator, base16 }:
{ config, lib, ... }@mod-args: { config, lib, ... }:
let let
inherit (lib) types;
from-nixos = mod-args ? "osConfig";
system-theme = config.stylix.useSystemTheme;
autoload = import ../autoload.nix { inherit lib; } "hm"; autoload = import ../autoload.nix { inherit lib; } "hm";
in { in {
imports = [ imports = [
@ -13,57 +10,4 @@ in {
./fonts.nix ./fonts.nix
(import ./palette.nix { inherit palette-generator base16; }) (import ./palette.nix { inherit palette-generator base16; })
] ++ autoload; ] ++ autoload;
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 =
if from-nixos
then mod-args.osConfig.stylix.homeManagerIntegration.enable
else 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;
}))
];
} }

View file

@ -7,7 +7,7 @@ args:
config = { config = {
xdg.configFile = { xdg.configFile = {
# See ../nixos/palette.nix for the rational behind these two options # See ../nixos/palette.nix for the rational behind these two options
"stylix/palette.json".source = config.stylix.generatedJSON; "stylix/palette.json".source = config.stylix.generated.json;
"stylix/palette.html".source = config.lib.stylix.colors { "stylix/palette.html".source = config.lib.stylix.colors {
template = builtins.readFile ../palette.html.mustache; template = builtins.readFile ../palette.html.mustache;
extension = ".html"; extension = ".html";

View file

@ -10,7 +10,7 @@ args:
# garbage collection, so future configurations can be evaluated without # garbage collection, so future configurations can be evaluated without
# having to generate the palette again. The generator is not kept, only # 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. # the palette which came from it, so this uses very little disk space.
"stylix/palette.json".source = config.stylix.generatedJSON; "stylix/palette.json".source = config.stylix.generated.json;
# We also provide a HTML version which is useful for viewing the colors # We also provide a HTML version which is useful for viewing the colors
# during development. # during development.

View file

@ -1,9 +1,11 @@
{ palette-generator, base16 }: { palette-generator, base16 }:
{ pkgs, lib, config, ... }: { pkgs, lib, config, ... }@args:
with lib; with lib;
let let
fromOs = import ./fromos.nix { inherit lib args; };
cfg = config.stylix; cfg = config.stylix;
paletteJSON = pkgs.runCommand "palette.json" { } '' paletteJSON = pkgs.runCommand "palette.json" { } ''
@ -11,7 +13,7 @@ let
''; '';
generatedPalette = importJSON paletteJSON; generatedPalette = importJSON paletteJSON;
generatedScheme = cfg.palette // { generatedScheme = generatedPalette // {
author = "Stylix"; author = "Stylix";
scheme = "Stylix"; scheme = "Stylix";
slug = "stylix"; slug = "stylix";
@ -21,7 +23,7 @@ in {
options.stylix = { options.stylix = {
polarity = mkOption { polarity = mkOption {
type = types.enum [ "either" "light" "dark" ]; type = types.enum [ "either" "light" "dark" ];
default = "either"; default = fromOs [ "polarity" ] "either";
description = '' description = ''
Use this option to force a light or dark theme. Use this option to force a light or dark theme.
@ -39,36 +41,28 @@ in {
This is set as the background of your desktop environment, if possible, This is set as the background of your desktop environment, if possible,
and used to generate a colour scheme if you don't set one manually. and used to generate a colour scheme if you don't set one manually.
''; '';
default = fromOs [ "image" ] null;
}; };
generatedJSON = mkOption { generated = {
type = types.path; json = mkOption {
description = "The result of palette-generator."; type = types.path;
readOnly = true; description = "The result of palette-generator.";
internal = true; readOnly = true;
default = paletteJSON; internal = true;
default = paletteJSON;
};
palette = mkOption {
type = types.attrs;
description = "The imported json";
readOnly = true;
internal = true;
default = generatedPalette;
};
}; };
palette = genAttrs [ # TODO proper removal of palette
"base00" "base01" "base02" "base03" "base04" "base05" "base06" "base07"
"base08" "base09" "base0A" "base0B" "base0C" "base0D" "base0E" "base0F"
] (base: mkOption {
description = ''
Hexadecimal color value for ${base}.
You can use this option to override single colors.
See <literal>stylix.base16Scheme</literal> if you want to import a
whole base16 scheme from a file.
You should not read from this option to access the chosen colors - use
<literal>lib.stylix.colors</literal> instead.
If <literal>stylix.base16Scheme</literal> is set to an external file,
those colors won't appear here.
'';
type = types.strMatching "[0-9a-fA-F]{6}";
default = generatedPalette.${base};
defaultText = literalDocBook "Automatically selected from the background image.";
});
base16Scheme = mkOption { base16Scheme = mkOption {
description = '' description = ''
@ -77,9 +71,12 @@ in {
This can be a path to a file, a string of YAML, or an attribute set. This can be a path to a file, a string of YAML, or an attribute set.
''; '';
type = with types; oneOf [ path lines attrs ]; type = with types; oneOf [ path lines attrs ];
default = generatedScheme; default =
if args ? "osConfig" && cfg.image != args.osConfig.stylix.image
then generatedScheme
else fromOs [ "base16Scheme" ] generatedScheme;
defaultText = literalDocBook '' defaultText = literalDocBook ''
The colors defined in <literal>stylix.palette</literal>. The colors used in the theming.
Those are automatically selected from the background image by default, Those are automatically selected from the background image by default,
but could be overridden manually. but could be overridden manually.

View file

@ -2,11 +2,13 @@
with lib; with lib;
{ let
fromOs = import ./fromos.nix { inherit lib args; };
in {
options.stylix.autoEnable = mkOption { options.stylix.autoEnable = mkOption {
description = "Whether to automatically enable styling for installed targets."; description = "Whether to automatically enable styling for installed targets.";
type = types.bool; type = types.bool;
default = true; default = fromOs [ "autoEnable" ] true;
}; };
config.lib.stylix.mkEnableTarget = config.lib.stylix.mkEnableTarget =