stylix: add overlay module (#1048)

Closes: https://github.com/danth/stylix/issues/865
Link: https://github.com/danth/stylix/pull/1048

Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
This commit is contained in:
awwpotato 2025-03-31 10:36:36 -07:00 committed by GitHub
parent c546582bae
commit eb19696b18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 143 additions and 74 deletions

View file

@ -73,6 +73,34 @@ one of the following applies:
- There is no reliable way to detect whether the target is installed, *and*
enabling it unconditionally would cause problems.
### Overlays
If your module is provided as an overlay it uses a special format, where config
is transparently passed to the platform (e.g. nixos) and overlay is a function
taking two arguments and returning an attrset:
```nix
{
lib,
config,
...
}:
{
options.stylix.targets.«name».enable =
config.lib.stylix.mkEnableOverlay "«human readable name»";
overlay =
final: prev:
lib.optionalAttrs
(config.stylix.enable && config.stylix.targets.«name».enable)
{
«name» = prev.«name».overrideAttrs (oldAttrs: {
});
};
}
```
## How to apply colors
Refer to the [style guide](./styling.md) to see how colors are named,

View file

@ -1,28 +0,0 @@
{ config, lib, ... }:
let
style = config.lib.stylix.colors {
template = ../gedit/template.xml.mustache;
extension = "xml";
};
in
{
options.stylix.targets.gnome-text-editor.enable =
config.lib.stylix.mkEnableTarget "GNOME Text Editor" true;
config =
lib.mkIf
(config.stylix.enable && config.stylix.targets.gnome-text-editor.enable)
{
nixpkgs.overlays = [
(_: prev: {
gnome-text-editor = prev.gnome-text-editor.overrideAttrs (oldAttrs: {
postFixup = ''
${oldAttrs.postFixup or ""}
cp ${style} $out/share/gnome-text-editor/styles/stylix.xml
'';
});
})
];
};
}

View file

@ -1,8 +1,5 @@
{ config, lib, ... }:
{
imports = [ ./common.nix ];
config =
lib.mkIf
(config.stylix.enable && config.stylix.targets.gnome-text-editor.enable)

View file

@ -1,3 +0,0 @@
{
imports = [ ./common.nix ];
}

View file

@ -0,0 +1,24 @@
{ config, lib, ... }:
let
style = config.lib.stylix.colors {
template = ../gedit/template.xml.mustache;
extension = "xml";
};
in
{
options.stylix.targets.gnome-text-editor.enable =
config.lib.stylix.mkEnableOverlay "GNOME Text Editor";
overlay =
_: prev:
lib.optionalAttrs
(config.stylix.enable && config.stylix.targets.gnome-text-editor.enable)
{
gnome-text-editor = prev.gnome-text-editor.overrideAttrs (oldAttrs: {
postFixup = ''
${oldAttrs.postFixup or ""}
cp ${style} $out/share/gnome-text-editor/styles/stylix.xml
'';
});
};
}

View file

@ -1,31 +0,0 @@
{
pkgs,
config,
lib,
...
}:
{
options.stylix.targets.nixos-icons.enable =
config.lib.stylix.mkEnableTarget "the NixOS logo" true;
config.nixpkgs.overlays =
lib.mkIf (config.stylix.enable && config.stylix.targets.nixos-icons.enable)
[
(_: super: {
nixos-icons = super.nixos-icons.overrideAttrs (oldAttrs: {
src = pkgs.applyPatches {
inherit (oldAttrs) src;
prePatch = with config.lib.stylix.colors; ''
substituteInPlace logo/nix-snowflake-white.svg --replace-fail '#ffffff' '#${base05}'
# Insert attribution comment after the XML prolog
sed \
--in-place \
'2i<!-- The original NixOS logo from ${oldAttrs.src.url} is licensed under https://creativecommons.org/licenses/by/4.0 and has been modified to match the ${scheme} color scheme. -->' \
logo/nix-snowflake-white.svg
'';
};
});
})
];
}

View file

@ -0,0 +1,31 @@
{
pkgs,
config,
lib,
...
}:
{
options.stylix.targets.nixos-icons.enable =
config.lib.stylix.mkEnableOverlay "the NixOS logo";
overlay =
_: super:
lib.optionalAttrs
(config.stylix.enable && config.stylix.targets.nixos-icons.enable)
{
nixos-icons = super.nixos-icons.overrideAttrs (oldAttrs: {
src = pkgs.applyPatches {
inherit (oldAttrs) src;
prePatch = with config.lib.stylix.colors; ''
substituteInPlace logo/nix-snowflake-white.svg --replace-fail '#ffffff' '#${base05}'
# Insert attribution comment after the XML prolog
sed \
--in-place \
'2i<!-- The original NixOS logo from ${oldAttrs.src.url} is licensed under https://creativecommons.org/licenses/by/4.0 and has been modified to match the ${scheme} color scheme. -->' \
logo/nix-snowflake-white.svg
'';
};
});
};
}

View file

@ -1,5 +1,9 @@
inputs:
{ lib, config, ... }:
{
lib,
config,
...
}:
# Imported modules which define new options must use an absolute path based
# on ${inputs.self}, otherwise those options will not appear in the generated
@ -19,6 +23,7 @@ in
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(import "${inputs.self}/stylix/overlays.nix" inputs)
] ++ autoload;
config.warnings =
lib.mkIf

View file

@ -12,6 +12,7 @@ in
"${inputs.self}/stylix/palette.nix"
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
(import "${inputs.self}/stylix/overlays.nix" inputs)
] ++ autoload;
# See https://github.com/nix-community/nix-on-droid/issues/436

View file

@ -1,5 +1,9 @@
inputs:
{ lib, config, ... }:
{
lib,
config,
...
}:
# Imported modules which define new options must use an absolute path based
# on ${inputs.self}, otherwise those options will not appear in the generated
@ -22,6 +26,7 @@ in
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(import "${inputs.self}/stylix/overlays.nix" inputs)
] ++ autoload;
config.warnings =
lib.mkIf

View file

@ -208,11 +208,18 @@ in
};
};
config = lib.optionalAttrs (options ? home-manager) (
lib.mkIf config.stylix.homeManagerIntegration.autoImport {
config = lib.optionalAttrs (options ? home-manager) lib.mkMerge [
(lib.mkIf config.stylix.homeManagerIntegration.autoImport {
home-manager.sharedModules =
[ config.stylix.homeManagerIntegration.module ]
[
config.stylix.homeManagerIntegration.module
]
++ (lib.optionals config.stylix.homeManagerIntegration.followSystem copyModules);
}
);
})
(lib.mkIf config.home-manager.useGlobalPkgs {
home-manager.sharedModules = lib.singleton {
config.stylix.overlays.enable = false;
};
})
];
}

View file

@ -1,5 +1,9 @@
inputs:
{ lib, config, ... }:
{
lib,
config,
...
}:
# Imported modules which define new options must use an absolute path based
# on ${inputs.self}, otherwise those options will not appear in the generated
@ -21,6 +25,7 @@ in
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(import "${inputs.self}/stylix/overlays.nix" inputs)
] ++ autoload;
config.warnings =
lib.mkIf

26
stylix/overlays.nix Normal file
View file

@ -0,0 +1,26 @@
inputs:
{
lib,
pkgs,
config,
...
}:
{
options.stylix.overlays.enable = config.lib.stylix.mkEnableTarget "packages via overlays" true;
imports = map (
f:
let
file = import f;
attrs =
if builtins.typeOf file == "lambda" then
file { inherit lib pkgs config; }
else
file;
in
{
options = attrs.options or { };
config.nixpkgs.overlays = [ attrs.overlay ];
}
) (import ./autoload.nix { inherit lib inputs; } "overlay");
}

View file

@ -35,7 +35,7 @@
let
cfg = config.stylix;
in
{
rec {
mkEnableTarget =
humanName: autoEnable:
lib.mkEnableOption "theming for ${humanName}"
@ -57,5 +57,7 @@
// lib.optionalAttrs autoEnable {
defaultText = lib.literalMD "`stylix.image != null`";
};
mkEnableOverlay =
humanName: mkEnableTarget humanName config.stylix.overlays.enable;
};
}