Skip to content

Commit

Permalink
stylix: clean up fromOs (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
danth authored Jun 11, 2024
1 parent daac8f5 commit f060e40
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 143 deletions.
52 changes: 30 additions & 22 deletions docs/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 5 additions & 8 deletions stylix/cursor.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}
36 changes: 2 additions & 34 deletions stylix/darwin/default.nix
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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 ];
};
}
18 changes: 8 additions & 10 deletions stylix/fonts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ with lib;
let
cfg = config.stylix.fonts;

fromOs = import ./fromos.nix { inherit lib args; };

fontType = types.submodule {
options = {
package = mkOption {
Expand All @@ -26,7 +24,7 @@ in {
serif = mkOption {
description = "Serif font.";
type = fontType;
default = fromOs [ "fonts" "serif" ] {
default = {
package = pkgs.dejavu_fonts;
name = "DejaVu Serif";
};
Expand All @@ -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";
};
Expand All @@ -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";
};
Expand All @@ -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";
};
Expand All @@ -66,23 +64,23 @@ in {
the desktop.
'';
type = types.ints.unsigned;
default = fromOs [ "fonts" "sizes" "desktop" ] 10;
default = 10;
};

applications = mkOption {
description = ''
The font size used by applications.
'';
type = types.ints.unsigned;
default = fromOs [ "fonts" "sizes" "applications" ] 12;
default = 12;
};

terminal = mkOption {
description = ''
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 {
Expand All @@ -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;
};
};

Expand Down
8 changes: 0 additions & 8 deletions stylix/fromos.nix

This file was deleted.

79 changes: 79 additions & 0 deletions stylix/home-manager-integration.nix
Original file line number Diff line number Diff line change
@@ -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);
});
}
36 changes: 2 additions & 34 deletions stylix/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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 ];
});
}
15 changes: 6 additions & 9 deletions stylix/opacity.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}
Loading

0 comments on commit f060e40

Please sign in to comment.