diff --git a/docs/src/modules.md b/docs/src/modules.md index 8da82730..dd1e8def 100644 --- a/docs/src/modules.md +++ b/docs/src/modules.md @@ -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, diff --git a/modules/gnome-text-editor/common.nix b/modules/gnome-text-editor/common.nix deleted file mode 100644 index 8d72ecc7..00000000 --- a/modules/gnome-text-editor/common.nix +++ /dev/null @@ -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 - ''; - }); - }) - ]; - }; -} diff --git a/modules/gnome-text-editor/hm.nix b/modules/gnome-text-editor/hm.nix index b9fb01d9..ab878e61 100644 --- a/modules/gnome-text-editor/hm.nix +++ b/modules/gnome-text-editor/hm.nix @@ -1,8 +1,5 @@ { config, lib, ... }: - { - imports = [ ./common.nix ]; - config = lib.mkIf (config.stylix.enable && config.stylix.targets.gnome-text-editor.enable) diff --git a/modules/gnome-text-editor/nixos.nix b/modules/gnome-text-editor/nixos.nix deleted file mode 100644 index 6e953ddc..00000000 --- a/modules/gnome-text-editor/nixos.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ - imports = [ ./common.nix ]; -} diff --git a/modules/gnome-text-editor/overlay.nix b/modules/gnome-text-editor/overlay.nix new file mode 100644 index 00000000..ec723ac5 --- /dev/null +++ b/modules/gnome-text-editor/overlay.nix @@ -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 + ''; + }); + }; +} diff --git a/modules/nixos-icons/nixos.nix b/modules/nixos-icons/nixos.nix deleted file mode 100644 index e45d774c..00000000 --- a/modules/nixos-icons/nixos.nix +++ /dev/null @@ -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' \ - logo/nix-snowflake-white.svg - ''; - }; - }); - }) - ]; -} diff --git a/modules/nixos-icons/overlay.nix b/modules/nixos-icons/overlay.nix new file mode 100644 index 00000000..087641ff --- /dev/null +++ b/modules/nixos-icons/overlay.nix @@ -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' \ + logo/nix-snowflake-white.svg + ''; + }; + }); + }; +} diff --git a/stylix/darwin/default.nix b/stylix/darwin/default.nix index 58063455..7de7db7c 100644 --- a/stylix/darwin/default.nix +++ b/stylix/darwin/default.nix @@ -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 diff --git a/stylix/droid/default.nix b/stylix/droid/default.nix index cc6e0757..b1afb2bc 100644 --- a/stylix/droid/default.nix +++ b/stylix/droid/default.nix @@ -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 diff --git a/stylix/hm/default.nix b/stylix/hm/default.nix index 0df2301b..69f5686f 100644 --- a/stylix/hm/default.nix +++ b/stylix/hm/default.nix @@ -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 diff --git a/stylix/home-manager-integration.nix b/stylix/home-manager-integration.nix index 6f2019a5..c4efa4f3 100644 --- a/stylix/home-manager-integration.nix +++ b/stylix/home-manager-integration.nix @@ -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; + }; + }) + ]; } diff --git a/stylix/nixos/default.nix b/stylix/nixos/default.nix index 32180f3f..7f5f3e79 100644 --- a/stylix/nixos/default.nix +++ b/stylix/nixos/default.nix @@ -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 diff --git a/stylix/overlays.nix b/stylix/overlays.nix new file mode 100644 index 00000000..af7b8da5 --- /dev/null +++ b/stylix/overlays.nix @@ -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"); +} diff --git a/stylix/target.nix b/stylix/target.nix index 12b8494b..fa12bb02 100644 --- a/stylix/target.nix +++ b/stylix/target.nix @@ -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; }; }