From d5906cdd798895b250f7e1106858d6f1802e71bb Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 13 Jun 2025 09:56:12 +0100 Subject: [PATCH 1/6] stylix: only read `autoEnable` in `mkEnableWallpaper`'s example Previously the example evaluated `config.stylix.image`, which should not be done by the docs. Since `mkEnableWallpaper` already assumes `autoEnable` is static when it evaluates `defaultText`, we can do the same for `example`. (cherry picked from commit ab2f48fc3a878be1cfd9dadc188fa03a56d8fc6e) --- stylix/target.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stylix/target.nix b/stylix/target.nix index f8b6861b2..0b12847c5 100644 --- a/stylix/target.nix +++ b/stylix/target.nix @@ -113,7 +113,11 @@ lib.literalExpression "config.stylix.image != null" else false; - example = config.stylix.image == null; + example = + if autoEnable then + lib.literalExpression "config.stylix.image == null" + else + true; }; mkEnableIf = From 7bfc4a2963f0452eba034180994fbd9079f94b12 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 13 Jun 2025 10:17:16 +0100 Subject: [PATCH 2/6] stylix: set defaultText in font size options (cherry picked from commit 84f02caad30191c6b1f8e0e6934bd4f08a2dc848) --- stylix/fonts.nix | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stylix/fonts.nix b/stylix/fonts.nix index 4d5dbb72f..1234df1a5 100644 --- a/stylix/fonts.nix +++ b/stylix/fonts.nix @@ -58,7 +58,12 @@ in mkFontSizeOption = { default, target }: lib.mkOption { - inherit default; + default = if builtins.isInt default then default else cfg.sizes.${default}; + defaultText = + if builtins.isInt default then + default + else + lib.literalExpression "config.stylix.fonts.sizes.${default}"; description = '' The font size used for ${target}. @@ -94,12 +99,12 @@ in terminal = mkFontSizeOption { target = "terminals and text editors"; - default = cfg.sizes.applications; + default = "applications"; }; popups = mkFontSizeOption { target = "notifications, popups, and other overlay elements of the desktop"; - default = cfg.sizes.desktop; + default = "desktop"; }; }; From 0f75c760050135e3526e84fb3cdd3d4ea24861b5 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sun, 4 May 2025 03:03:00 +0100 Subject: [PATCH 3/6] doc: use minimal module evals We don't actually need fully blown NixOS or home-manager configurations just to read the declared `options`. Instead, we can directly call `lib.evalModules` to build a minimal configuration containing only stylix modules. (cherry picked from commit bdf092d169f59224a3ec4683187981db8d0d346a) --- doc/default.nix | 37 +++++++++++-------------------------- doc/eval_compat.nix | 26 ++++++++++++++++++++++++++ doc/hm_compat.nix | 12 ++++++++++++ flake/packages.nix | 2 -- 4 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 doc/eval_compat.nix create mode 100644 doc/hm_compat.nix diff --git a/doc/default.nix b/doc/default.nix index 04f090231..343e69010 100644 --- a/doc/default.nix +++ b/doc/default.nix @@ -2,9 +2,6 @@ lib, pkgs, inputs, - nixosSystem, - homeManagerConfiguration, - system, callPackage, writeText, stdenvNoCC, @@ -17,38 +14,26 @@ let # Prefix to remove from option declaration file paths. rootPrefix = toString ../. + "/"; - nixosConfiguration = nixosSystem { - inherit system; - modules = [ - inputs.home-manager.nixosModules.home-manager - inputs.self.nixosModules.stylix - ]; - }; - - homeConfiguration = homeManagerConfiguration { - inherit pkgs; - modules = [ - inputs.self.homeModules.stylix - { - home = { - homeDirectory = "/home/book"; - stateVersion = "22.11"; - username = "book"; - }; - } - ]; - }; + evalDocs = + module: + lib.evalModules { + modules = [ ./eval_compat.nix ] ++ lib.toList module; + specialArgs = { inherit pkgs; }; + }; # TODO: Include Nix Darwin options platforms = { home_manager = { name = "Home Manager"; - configuration = homeConfiguration; + configuration = evalDocs [ + inputs.self.homeModules.stylix + ./hm_compat.nix + ]; }; nixos = { name = "NixOS"; - configuration = nixosConfiguration; + configuration = evalDocs inputs.self.nixosModules.stylix; }; }; diff --git a/doc/eval_compat.nix b/doc/eval_compat.nix new file mode 100644 index 000000000..08822105b --- /dev/null +++ b/doc/eval_compat.nix @@ -0,0 +1,26 @@ +{ lib, ... }: +{ + options = { + # Declare the arbitrarily named __stub attribute to allow modules to evaluate + # 'options.programs ? «OPTION»'. + # + # TODO: Replace 'options.programs ? «OPTION»' instances with + # 'options ? programs.«OPTION»' to remove this __stub workaround. + programs.__stub = lib.mkSinkUndeclaredOptions { }; + + # The config.lib option, as found in NixOS and home-manager. + # Many option declarations depend on `config.lib.stylix`. + lib = lib.mkOption { + type = lib.types.attrsOf lib.types.attrs; + description = '' + This option allows modules to define helper functions, constants, etc. + ''; + default = { }; + visible = false; + }; + }; + + # Third-party options are not included in the module eval, + # so disable checking options definitions have matching declarations + config._module.check = false; +} diff --git a/doc/hm_compat.nix b/doc/hm_compat.nix new file mode 100644 index 000000000..b0654e40e --- /dev/null +++ b/doc/hm_compat.nix @@ -0,0 +1,12 @@ +{ lib, ... }: +{ + # Declare the arbitrarily named __stub attribute to allow modules to evaluate + # 'options.services ? «OPTION»'. + # + # TODO: Replace 'options.services ? «OPTION»' instances with + # 'options ? services.«OPTION»' to remove this __stub workaround. + options.services.__stub = lib.mkSinkUndeclaredOptions { }; + + # Some modules use home-manager's `osConfig` arg + config._module.args.osConfig = null; +} diff --git a/flake/packages.nix b/flake/packages.nix index 70390d4db..7990ecb78 100644 --- a/flake/packages.nix +++ b/flake/packages.nix @@ -22,8 +22,6 @@ { doc = pkgs.callPackage ../doc { inherit inputs; - inherit (inputs.nixpkgs.lib) nixosSystem; - inherit (inputs.home-manager.lib) homeManagerConfiguration; }; serve-docs = pkgs.callPackage ../doc/server.nix { inherit (config.packages) doc; From d85e8f15408560c3c250006be1b04dc96f5372ff Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Thu, 19 Jun 2025 23:50:54 +0100 Subject: [PATCH 4/6] doc: replace `inputs` input with `self` The docs no longer needs access to arbitrary flake inputs. We should enforce this strictly by taking `self` instead. (cherry picked from commit 07af242a44ebaa889fac856b18e525f0a0f9ab1f) --- doc/default.nix | 8 ++++---- flake/packages.nix | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/default.nix b/doc/default.nix index 343e69010..1253c33cb 100644 --- a/doc/default.nix +++ b/doc/default.nix @@ -1,7 +1,7 @@ { lib, pkgs, - inputs, + self, callPackage, writeText, stdenvNoCC, @@ -27,13 +27,13 @@ let home_manager = { name = "Home Manager"; configuration = evalDocs [ - inputs.self.homeModules.stylix + self.homeModules.stylix ./hm_compat.nix ]; }; nixos = { name = "NixOS"; - configuration = evalDocs inputs.self.nixosModules.stylix; + configuration = evalDocs self.nixosModules.stylix; }; }; @@ -324,7 +324,7 @@ let # Permalink to view a source file on GitHub. If the commit isn't known, # then fall back to the latest commit. - declarationCommit = inputs.self.rev or "master"; + declarationCommit = self.rev or "master"; declarationPermalink = "https://github.com/nix-community/stylix/blob/${declarationCommit}"; # Renders a single option declaration. Example output: diff --git a/flake/packages.nix b/flake/packages.nix index 7990ecb78..d603d2af1 100644 --- a/flake/packages.nix +++ b/flake/packages.nix @@ -21,7 +21,7 @@ )) { doc = pkgs.callPackage ../doc { - inherit inputs; + inherit (inputs) self; }; serve-docs = pkgs.callPackage ../doc/server.nix { inherit (config.packages) doc; From a5d30792905f660df23ae03efe244a513680b2c0 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sun, 4 May 2025 03:40:30 +0100 Subject: [PATCH 5/6] doc: ensure `pkgs` is not used in option docs Providing a stub `pkgs` instance that has no contents ensures the docs does not _read_ `pkgs` anywhere. (cherry picked from commit e1ae98f979fb5360ec025277e19ea82719a37d5a) --- doc/default.nix | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/default.nix b/doc/default.nix index 1253c33cb..1b887713d 100644 --- a/doc/default.nix +++ b/doc/default.nix @@ -14,11 +14,24 @@ let # Prefix to remove from option declaration file paths. rootPrefix = toString ../. + "/"; + # A stub pkgs used while evaluating the stylix modules for the docs + noPkgs = { + # Needed for type-checking + inherit (pkgs) _type; + + # Permit access to (pkgs.formats.foo { }).type + formats = builtins.mapAttrs (_: fmt: args: { + inherit (fmt args) type; + }) pkgs.formats; + }; + evalDocs = module: lib.evalModules { modules = [ ./eval_compat.nix ] ++ lib.toList module; - specialArgs = { inherit pkgs; }; + specialArgs = { + pkgs = noPkgs; + }; }; # TODO: Include Nix Darwin options From 5d0f9d6d9ff262fd59105d5b361e3a10b36f2048 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 10 Jun 2025 19:33:29 +0100 Subject: [PATCH 6/6] doc: ensure `config` is not used in option docs With a few exceptions for `lib.stylix` functions. Once these exceptions are removed, we will have solved the issue with NixOS's documentation.nixos.includeAllModules option. See https://github.com/nix-community/stylix/issues/98 (cherry picked from commit 100b968012804d6526c5f48a32c30680916bc474) --- doc/default.nix | 35 +++++++++++++++++++++++++++++++++++ doc/eval_compat.nix | 25 ++++++------------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/doc/default.nix b/doc/default.nix index 1b887713d..25b8d5039 100644 --- a/doc/default.nix +++ b/doc/default.nix @@ -25,12 +25,47 @@ let }) pkgs.formats; }; + # A stub config used while evaluating the stylix modules for the docs + # + # TODO: remove all dependency on `config` and simplify to `noConfig = null`. + # Doing that should resolve https://github.com/nix-community/stylix/issues/98 + noConfig = + let + configuration = evalDocs { + # The config.lib option, as found in NixOS and home-manager. + # Required by the `target.nix` module. + options.lib = lib.mkOption { + type = lib.types.attrsOf lib.types.attrs; + description = '' + This option allows modules to define helper functions, constants, etc. + ''; + default = { }; + visible = false; + }; + + # The target.nix module defines functions that are currently needed to + # declare options + imports = [ ../stylix/target.nix ]; + }; + in + { + lib.stylix = { + inherit (configuration.config.lib.stylix) + mkEnableIf + mkEnableTarget + mkEnableTargetWith + mkEnableWallpaper + ; + }; + }; + evalDocs = module: lib.evalModules { modules = [ ./eval_compat.nix ] ++ lib.toList module; specialArgs = { pkgs = noPkgs; + config = noConfig; }; }; diff --git a/doc/eval_compat.nix b/doc/eval_compat.nix index 08822105b..daf4b87fe 100644 --- a/doc/eval_compat.nix +++ b/doc/eval_compat.nix @@ -1,24 +1,11 @@ { lib, ... }: { - options = { - # Declare the arbitrarily named __stub attribute to allow modules to evaluate - # 'options.programs ? «OPTION»'. - # - # TODO: Replace 'options.programs ? «OPTION»' instances with - # 'options ? programs.«OPTION»' to remove this __stub workaround. - programs.__stub = lib.mkSinkUndeclaredOptions { }; - - # The config.lib option, as found in NixOS and home-manager. - # Many option declarations depend on `config.lib.stylix`. - lib = lib.mkOption { - type = lib.types.attrsOf lib.types.attrs; - description = '' - This option allows modules to define helper functions, constants, etc. - ''; - default = { }; - visible = false; - }; - }; + # Declare the arbitrarily named __stub attribute to allow modules to evaluate + # 'options.programs ? «OPTION»'. + # + # TODO: Replace 'options.programs ? «OPTION»' instances with + # 'options ? programs.«OPTION»' to remove this __stub workaround. + options.programs.__stub = lib.mkSinkUndeclaredOptions { }; # Third-party options are not included in the module eval, # so disable checking options definitions have matching declarations