From f060e4059b408b2cc1891ce655d0f6bef4e21a5b Mon Sep 17 00:00:00 2001 From: Daniel Thwaites Date: Tue, 11 Jun 2024 02:21:04 +0100 Subject: [PATCH] stylix: clean up `fromOs` (#407) --- docs/src/configuration.md | 52 +++++++++++-------- stylix/cursor.nix | 13 ++--- stylix/darwin/default.nix | 36 +------------ stylix/fonts.nix | 18 +++---- stylix/fromos.nix | 8 --- stylix/home-manager-integration.nix | 79 +++++++++++++++++++++++++++++ stylix/nixos/default.nix | 36 +------------ stylix/opacity.nix | 15 +++--- stylix/palette.nix | 20 ++------ stylix/target.nix | 4 +- 10 files changed, 138 insertions(+), 143 deletions(-) delete mode 100644 stylix/fromos.nix create mode 100644 stylix/home-manager-integration.nix diff --git a/docs/src/configuration.md b/docs/src/configuration.md index 70c1985be..78a10762d 100644 --- a/docs/src/configuration.md +++ b/docs/src/configuration.md @@ -169,28 +169,36 @@ Or even choose monospace for everything: } ``` -## Multi-user configurations - -For those apps which are configured through Home Manager, Stylix allows you to -choose a different theme for each user. This can be done by setting the theme -within Home Manager for that user rather than at the system level. - -By default, all users follow the system theme. This can be turned off by -setting `stylix.homeManagerIntegration.followSystem = false`, in which case you -must explicitly set a theme for each user. Setting that option is not required -just to be able to override an individual theme. - -If you would like to disable all Home Manager activity for a user, you can set -`stylix.homeManagerIntegration.autoImport = false`, then manually import the -Home Manager module for the users for which it should be enabled. - -Note that if the wallpaper image for a user is different to the rest of the -system, a separate theme will always be generated for them, even though their -`base16Scheme` option has not been overridden. If you want that user to follow -the system theme while having a different wallpaper, you will need to manually -copy the system theme into their configuration. (This behaviour is necessary as -otherwise it would be impossible to use a generated theme for a user while -having a manually created theme for the rest of the system.) +## Home Manager inheritance + +By default, if Home Manager is used as part of NixOS, then Stylix will be +automatically installed for all users, and the NixOS theme will become their +default settings. + +This is convenient for single-user systems, since you can configure everything +once at the system level and it will automatically carry over. For multi-user +systems, you can override the settings within Home Manager to select a different +theme for each user. + +You may prefer to disable inheritance entirely, and set up the Home Manager +version of Stylix yourself if required. Refer to the options +[`stylix.homeManagerIntegration.autoImport`](options/nixos.md#stylixhomemanagerintegrationautoimport) +and +[`stylix.homeManagerIntegration.followSystem`](options/nixos.md#stylixhomemanagerintegrationfollowsystem) +to customize this. + +> [!NOTE] +> +> There is a special case involving the +> [`stylix.base16Scheme`](options/nixos.md#stylixbase16scheme) +> option: +> +> If the wallpaper in a Home Manager configuration is changed, then Home Manager +> will stop inheriting the color scheme from NixOS. This allows Home Manager +> configurations to use the automatic palette generator without being overridden. +> +> Similarly, [`stylix.override`](options/nixos.md#stylixoverride) is not inherited +> if the color scheme is different. ## Turning targets on and off diff --git a/stylix/cursor.nix b/stylix/cursor.nix index a92c41c23..594251179 100644 --- a/stylix/cursor.nix +++ b/stylix/cursor.nix @@ -1,26 +1,23 @@ -{ pkgs, config, lib, ... } @ args: +{ pkgs, lib, ... } @ args: with lib; -let - cfg = config.stylix.cursor; - fromOs = import ./fromos.nix { inherit lib args; }; -in { +{ options.stylix.cursor = { name = mkOption { description = "The cursor name within the package."; type = types.str; - default = fromOs [ "cursor" "name" ] "Vanilla-DMZ"; + default = "Vanilla-DMZ"; }; package = mkOption { description = "Package providing the cursor theme."; type = types.package; - default = fromOs [ "cursor" "package" ] pkgs.vanilla-dmz; + default = pkgs.vanilla-dmz; }; size = mkOption { description = "The cursor size."; type = types.int; - default = fromOs [ "cursor" "size" ] 32; + default = 32; }; }; } diff --git a/stylix/darwin/default.nix b/stylix/darwin/default.nix index 1262d7f8f..2a29746c9 100644 --- a/stylix/darwin/default.nix +++ b/stylix/darwin/default.nix @@ -1,9 +1,8 @@ inputs: { palette-generator, base16, homeManagerModule }: -{ options, config, lib, ... }: +{ lib, ... }: let - hm = config.stylix.homeManagerIntegration; autoload = import ../autoload.nix { inherit lib; } "darwin"; in { imports = [ @@ -12,37 +11,6 @@ in { ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) (import ../templates.nix inputs) + (import ../home-manager-integration.nix homeManagerModule) ] ++ autoload; - - options.stylix.homeManagerIntegration = { - followSystem = lib.mkOption { - description = '' - When this option is `true`, Home Manager will follow - the system theme by default, rather than requiring a theme to be set. - - This will only affect Home Manager configurations which are built - within the nix-darwin configuration. - ''; - type = lib.types.bool; - default = true; - }; - - autoImport = lib.mkOption { - description = '' - Whether to enable Stylix automatically for every user. - - This only applies to users for which Home Manager is set up within the - nix-darwin configuration. - ''; - type = lib.types.bool; - default = options ? home-manager; - defaultText = lib.literalMD '' - `true` when Home Manager is present. - ''; - }; - }; - - config = lib.mkIf hm.autoImport { - home-manager.sharedModules = [ homeManagerModule ]; - }; } diff --git a/stylix/fonts.nix b/stylix/fonts.nix index 75ab2ad6f..218e30244 100644 --- a/stylix/fonts.nix +++ b/stylix/fonts.nix @@ -5,8 +5,6 @@ with lib; let cfg = config.stylix.fonts; - fromOs = import ./fromos.nix { inherit lib args; }; - fontType = types.submodule { options = { package = mkOption { @@ -26,7 +24,7 @@ in { serif = mkOption { description = "Serif font."; type = fontType; - default = fromOs [ "fonts" "serif" ] { + default = { package = pkgs.dejavu_fonts; name = "DejaVu Serif"; }; @@ -35,7 +33,7 @@ in { sansSerif = mkOption { description = "Sans-serif font."; type = fontType; - default = fromOs [ "fonts" "sansSerif" ] { + default = { package = pkgs.dejavu_fonts; name = "DejaVu Sans"; }; @@ -44,7 +42,7 @@ in { monospace = mkOption { description = "Monospace font."; type = fontType; - default = fromOs [ "fonts" "monospace" ] { + default = { package = pkgs.dejavu_fonts; name = "DejaVu Sans Mono"; }; @@ -53,7 +51,7 @@ in { emoji = mkOption { description = "Emoji font."; type = fontType; - default = fromOs [ "fonts" "emoji" ] { + default = { package = pkgs.noto-fonts-emoji; name = "Noto Color Emoji"; }; @@ -66,7 +64,7 @@ in { the desktop. ''; type = types.ints.unsigned; - default = fromOs [ "fonts" "sizes" "desktop" ] 10; + default = 10; }; applications = mkOption { @@ -74,7 +72,7 @@ in { The font size used by applications. ''; type = types.ints.unsigned; - default = fromOs [ "fonts" "sizes" "applications" ] 12; + default = 12; }; terminal = mkOption { @@ -82,7 +80,7 @@ in { The font size for terminals/text editors. ''; type = types.ints.unsigned; - default = fromOs [ "fonts" "sizes" "terminal" ] cfg.sizes.applications; + default = cfg.sizes.applications; }; popups = mkOption { @@ -91,7 +89,7 @@ in { elements of the desktop. ''; type = types.ints.unsigned; - default = fromOs [ "fonts" "sizes" "popups" ] cfg.sizes.desktop; + default = cfg.sizes.desktop; }; }; diff --git a/stylix/fromos.nix b/stylix/fromos.nix deleted file mode 100644 index b9ee88695..000000000 --- a/stylix/fromos.nix +++ /dev/null @@ -1,8 +0,0 @@ -{ lib, args }: - -path: default: -if ( args ? "osConfig" - && args.osConfig ? "stylix" - && args.osConfig.stylix.homeManagerIntegration.followSystem) - then lib.attrByPath path default args.osConfig.stylix - else default diff --git a/stylix/home-manager-integration.nix b/stylix/home-manager-integration.nix new file mode 100644 index 000000000..5afe202b1 --- /dev/null +++ b/stylix/home-manager-integration.nix @@ -0,0 +1,79 @@ +homeManagerModule: +{ lib, config, options, ... }: + +let + copyModules = builtins.map + ( + { path, condition ? lib.const true }: + { config, osConfig, ... }: + lib.mkIf (condition config) + (lib.setAttrByPath path (lib.mkDefault (lib.getAttrFromPath path osConfig))) + ) + [ + { path = [ "stylix" "autoEnable" ]; } + { + path = [ "stylix" "base16Scheme" ]; + condition = homeConfig: config.stylix.image == homeConfig.stylix.image; + } + { path = [ "stylix" "cursor" "name" ]; } + { path = [ "stylix" "cursor" "package" ]; } + { path = [ "stylix" "cursor" "size" ]; } + { path = [ "stylix" "enable" ]; } + { path = [ "stylix" "fonts" "serif" ]; } + { path = [ "stylix" "fonts" "sansSerif" ]; } + { path = [ "stylix" "fonts" "monospace" ]; } + { path = [ "stylix" "fonts" "emoji" ]; } + { path = [ "stylix" "fonts" "sizes" "desktop" ]; } + { path = [ "stylix" "fonts" "sizes" "applications" ]; } + { path = [ "stylix" "fonts" "sizes" "terminal" ]; } + { path = [ "stylix" "fonts" "sizes" "popups" ]; } + { path = [ "stylix" "image" ]; } + { path = [ "stylix" "imageScalingMode" ]; } + { path = [ "stylix" "opacity" "desktop" ]; } + { path = [ "stylix" "opacity" "applications" ]; } + { path = [ "stylix" "opacity" "terminal" ]; } + { path = [ "stylix" "opacity" "popups" ]; } + { + path = [ "stylix" "override" ]; + condition = homeConfig: config.stylix.base16Scheme == homeConfig.stylix.base16Scheme; + } + { path = [ "stylix" "polarity" ]; } + ]; + +in { + options.stylix.homeManagerIntegration = { + followSystem = lib.mkOption { + description = '' + When this option is `true`, Home Manager configurations will follow + the NixOS configuration by default, rather than using the standard + default settings. + + This only applies to Home Manager configurations managed by + [`stylix.homeManagerIntegration.autoImport`](#stylixhomemanagerintegrationautoimport). + ''; + type = lib.types.bool; + default = true; + example = false; + }; + + autoImport = lib.mkOption { + description = '' + Whether to import Stylix automatically for every Home Manager user. + + This only works if you are using `home-manager.users.«name»` within + your NixOS configuration, rather than running Home Manager independently. + ''; + type = lib.types.bool; + default = true; + example = false; + }; + }; + + config = + lib.optionalAttrs (options ? home-manager) + (lib.mkIf config.stylix.homeManagerIntegration.autoImport { + home-manager.sharedModules = + [ homeManagerModule ] ++ + (lib.optionals config.stylix.homeManagerIntegration.followSystem copyModules); + }); +} diff --git a/stylix/nixos/default.nix b/stylix/nixos/default.nix index 0477bc90c..56c4741b5 100644 --- a/stylix/nixos/default.nix +++ b/stylix/nixos/default.nix @@ -1,9 +1,8 @@ inputs: { palette-generator, base16, homeManagerModule }: -{ options, config, lib, ... }: +{ lib, ... }: let - hm = config.stylix.homeManagerIntegration; autoload = import ../autoload.nix { inherit lib; } "nixos"; in { imports = [ @@ -14,37 +13,6 @@ in { ./fonts.nix (import ./palette.nix { inherit palette-generator base16; }) (import ../templates.nix inputs) + (import ../home-manager-integration.nix homeManagerModule) ] ++ autoload; - - options.stylix.homeManagerIntegration = { - followSystem = lib.mkOption { - description = '' - When this option is `true`, Home Manager will follow - the system theme by default, rather than requiring a theme to be set. - - This will only affect Home Manager configurations which are built - within the NixOS configuration. - ''; - type = lib.types.bool; - default = true; - }; - - autoImport = lib.mkOption { - description = '' - Whether to enable Stylix automatically for every user. - - This only applies to users for which Home Manager is set up within the - NixOS configuration. - ''; - type = lib.types.bool; - default = options ? home-manager; - defaultText = lib.literalMD '' - `true` when Home Manager is present. - ''; - }; - }; - - config = lib.optionalAttrs (options ? home-manager) (lib.mkIf hm.autoImport { - home-manager.sharedModules = [ homeManagerModule ]; - }); } diff --git a/stylix/opacity.nix b/stylix/opacity.nix index df0850c0d..3790cd76d 100644 --- a/stylix/opacity.nix +++ b/stylix/opacity.nix @@ -1,31 +1,28 @@ -{ pkgs, config, lib, ... } @ args: +{ lib, ... } @ args: with lib; -let - cfg = config.stylix.opacity; - fromOs = import ./fromos.nix { inherit lib args; }; -in { +{ options.stylix.opacity = { desktop = mkOption { description = "The opacity of the windows of bars/widgets, the amount of applications supported is currently limited"; type = types.float; - default = fromOs [ "opacity" "desktop" ] 1.0; + default = 1.0; }; applications = mkOption { description = "The opacity of the windows of applications, the amount of applications supported is currently limited"; type = types.float; - default = fromOs [ "opacity" "applications" ] 1.0; + default = 1.0; }; terminal = mkOption { description = "The opacity of the windows of terminals, this works across all terminals supported by stylix"; type = types.float; - default = fromOs [ "opacity" "terminal" ] 1.0; + default = 1.0; }; popups = mkOption { description = "The opacity of the windows of notifications/popups, the amount of applications supported is currently limited"; type = types.float; - default = fromOs [ "opacity" "popups" ] 1.0; + default = 1.0; }; }; } diff --git a/stylix/palette.nix b/stylix/palette.nix index f3570720a..515107ecd 100644 --- a/stylix/palette.nix +++ b/stylix/palette.nix @@ -4,8 +4,6 @@ with lib; let - fromOs = import ./fromos.nix { inherit lib args; }; - cfg = config.stylix; paletteJSON = let @@ -24,12 +22,6 @@ let in json; generatedScheme = importJSON paletteJSON; - override = - (if cfg.base16Scheme == fromOs [ "base16Scheme" ] {} - then fromOs [ "override" ] {} - else {}) - // cfg.override; - in { # TODO link to doc on how to do instead imports = [ @@ -54,7 +46,7 @@ in { options.stylix = { polarity = mkOption { type = types.enum [ "either" "light" "dark" ]; - default = fromOs [ "polarity" ] "either"; + default = "either"; description = '' Use this option to force a light or dark theme. @@ -72,12 +64,11 @@ in { 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. ''; - default = fromOs [ "image" ] null; }; imageScalingMode = mkOption { type = types.enum [ "stretch" "fill" "fit" "center" "tile" ]; - default = fromOs [ "imageScalingMode" ] "fill"; + default = "fill"; description = '' Wallpaper scaling mode; @@ -118,10 +109,7 @@ in { This can be a path to a file, a string of YAML, or an attribute set. ''; type = with types; oneOf [ path lines attrs ]; - default = - if cfg.image != fromOs [ "image" ] null - then generatedScheme - else fromOs [ "base16Scheme" ] generatedScheme; + default = generatedScheme; defaultText = literalMD '' The colors used in the theming. @@ -146,7 +134,7 @@ in { config = { # This attrset can be used like a function too, see # https://github.com/SenchoPens/base16.nix/blob/b390e87cd404e65ab4d786666351f1292e89162a/README.md#theme-step-22 - lib.stylix.colors = (base16.mkSchemeAttrs cfg.base16Scheme).override override; + lib.stylix.colors = (base16.mkSchemeAttrs cfg.base16Scheme).override cfg.override; lib.stylix.scheme = base16.mkSchemeAttrs cfg.base16Scheme; stylix.generated.fileTree = { diff --git a/stylix/target.nix b/stylix/target.nix index d9507e6b9..695a0a103 100644 --- a/stylix/target.nix +++ b/stylix/target.nix @@ -12,7 +12,7 @@ with lib; are ignored. ''; type = types.bool; - default = import ./fromos.nix { inherit lib args; } [ "enable" ] false; + default = false; example = true; }; @@ -28,7 +28,7 @@ with lib; detected automatically. ''; type = types.bool; - default = import ./fromos.nix { inherit lib args; } [ "autoEnable" ] true; + default = true; example = false; }; };