treewide: remove unnecessary use of self (#1413)

Remove the unnecessary use of self to simplify path handling by reducing
importApply uses, and eliminating unsafeRemoveStringContext.

This is a follow-up to commit 9b61cc39b2 ("stylix: don't split
autoloaded modules into a separate derivation (#631)").

Be aware that this change emphasizes careful use of string
interpolation. Using "${./foo}" can inadvertently create store objects.
This behavior aligns with standard Nix practices, so it's a good habit
to be mindful of it. While there are valid cases for creating store
objects (for example, when a derivation needs to depend on a file), if
you're working directly with paths or files within your project, either
keep the value as a path or use toString instead of string
interpolation.

Closes: https://github.com/nix-community/stylix/issues/1393
Link: https://github.com/nix-community/stylix/pull/1413

Reviewed-by: awwpotato <awwpotato@voidq.com>
Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
This commit is contained in:
NAHO 2025-05-31 15:58:26 +02:00 committed by GitHub
commit 8762da957b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 125 additions and 157 deletions

View file

@ -13,6 +13,9 @@
}:
let
# Prefix to remove from option declaration file paths.
rootPrefix = toString ../. + "/";
nixosConfiguration = nixosSystem {
inherit system;
modules = [
@ -50,7 +53,7 @@ let
};
};
metadata = callPackage "${inputs.self}/stylix/meta.nix" { inherit inputs; };
metadata = callPackage ../stylix/meta.nix { };
# We construct an index of all Stylix options, using the following format:
#
@ -119,15 +122,10 @@ let
option,
}:
# Only include options which are declared by a module within Stylix.
if lib.hasPrefix "${inputs.self}/" declaration then
if lib.hasPrefix rootPrefix declaration then
let
# Part of this string may become an attribute name in the index, and
# attribute names aren't allowed to have string context. The context
# comes from `${inputs.self}`, which is removed by `removePrefix`.
# Therefore, this use of `unsafeDiscardStringContext` is safe.
pathWithContext = lib.removePrefix "${inputs.self}/" declaration;
path = builtins.unsafeDiscardStringContext pathWithContext;
pathComponents = lib.splitString "/" path;
subPath = lib.removePrefix rootPrefix (toString declaration);
pathComponents = lib.splitString "/" subPath;
in
# Options declared in the modules directory go to the Modules section,
# otherwise they're assumed to be shared between modules, and go to the
@ -236,29 +234,30 @@ let
};
}
else
insert {
inherit index platform option;
let
page = "src/options/platforms/${platform}.md";
path = ./. + "/${page}";
in
insert {
inherit
index
platform
page
option
;
emptyPage = {
referenceSection = "Platforms";
readme =
let
path = "${inputs.self}/doc/src/options/platforms/${platform}.md";
# This doesn't count as IFD because ${inputs.self} is a flake input
mainText =
if builtins.pathExists path then
builtins.readFile path
else
''
# ${platform.name}
> [!NOTE]
> Documentation is not available for this platform. Its
> main options are listed below, and you may find more
> specific options in the documentation for each module.
'';
in
mainText;
if builtins.pathExists path then
builtins.readFile path
else
''
# ${platform.name}
> [!NOTE]
> Documentation is not available for this platform. Its
> main options are listed below, and you may find more
> specific options in the documentation for each module.
'';
# Platform pages only initialise that platform, since showing other
# platforms here would be nonsensical.
@ -339,9 +338,6 @@ let
else
throw "unexpected value type: ${builtins.typeOf value}";
# Prefix to remove from file paths when listing where an option is declared.
declarationPrefix = "${inputs.self}";
# 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";
@ -354,12 +350,11 @@ let
declaration:
let
declarationString = toString declaration;
filePath = lib.removePrefix "${declarationPrefix}/" declarationString;
subPath = lib.removePrefix rootPrefix declarationString;
in
if lib.hasPrefix declarationPrefix declarationString then
"- [${filePath}](${declarationPermalink}/${filePath})"
else
throw "declaration not in ${declarationPrefix}: ${declarationString}";
lib.throwIfNot (lib.hasPrefix rootPrefix declarationString)
"declaration not in ${rootPrefix}: ${declarationString}"
"- [${subPath}](${declarationPermalink}/${subPath})";
# You can embed HTML inside a Markdown document, but to render further
# Markdown between the HTML tags, it must be surrounded by blank lines:

View file

@ -10,7 +10,7 @@
{ pkgs, ... }@args:
{
imports = [
(lib.modules.importApply "${self}/stylix/nixos" inputs)
../stylix/nixos
{
stylix = {
inherit inputs;
@ -27,7 +27,7 @@
{ pkgs, ... }@args:
{
imports = [
(lib.modules.importApply "${self}/stylix/hm" inputs)
../stylix/hm
{
stylix = {
inherit inputs;
@ -43,7 +43,7 @@
{ pkgs, ... }@args:
{
imports = [
(lib.modules.importApply "${self}/stylix/darwin" inputs)
../stylix/darwin
{
stylix = {
inherit inputs;
@ -60,7 +60,7 @@
{ pkgs, ... }@args:
{
imports = [
(lib.modules.importApply "${self}/stylix/droid" inputs)
../stylix/droid
{
stylix = {
paletteGenerator =

View file

@ -1,9 +1,4 @@
{
lib,
inputs,
self,
...
}:
{ lib, inputs, ... }:
{
perSystem =
@ -17,17 +12,17 @@
# Testbeds are virtual machines based on NixOS, therefore they are
# only available for Linux systems.
(lib.mkIf pkgs.stdenv.hostPlatform.isLinux (
import "${self}/stylix/testbed/default.nix" {
import ../stylix/testbed/default.nix {
inherit pkgs inputs lib;
}
))
{
docs = pkgs.callPackage "${self}/doc" {
docs = pkgs.callPackage ../doc {
inherit inputs;
inherit (inputs.nixpkgs.lib) nixosSystem;
inherit (inputs.home-manager.lib) homeManagerConfiguration;
};
palette-generator = pkgs.callPackage "${self}/palette-generator" { };
palette-generator = pkgs.callPackage ../palette-generator { };
}
];
};

View file

@ -1,13 +1,13 @@
{ lib, inputs }:
{ lib }:
# string -> [ path ]
# List include path for either nixos modules or hm modules
for:
platform:
builtins.concatLists (
lib.mapAttrsToList (
path: kind:
target: kind:
let
file = "${inputs.self}/modules/${path}/${for}.nix";
file = ../modules/${target}/${platform}.nix;
module = import file;
# Detect whether the file's value has an argument named `mkTarget`
@ -47,5 +47,5 @@ builtins.concatLists (
else
file
)
) (builtins.readDir "${inputs.self}/modules")
) (builtins.readDir ../modules)
)

View file

@ -1,29 +1,24 @@
inputs:
{
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
# documentation.
let
autoload = import ../autoload.nix { inherit lib inputs; } "darwin";
autoload = import ../autoload.nix { inherit lib; } "darwin";
in
{
imports = [
"${inputs.self}/stylix/darwin/fonts.nix"
"${inputs.self}/stylix/darwin/palette.nix"
"${inputs.self}/stylix/fonts.nix"
"${inputs.self}/stylix/home-manager-integration.nix"
"${inputs.self}/stylix/opacity.nix"
"${inputs.self}/stylix/palette.nix"
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(lib.modules.importApply "${inputs.self}/stylix/overlays.nix" inputs)
./fonts.nix
./palette.nix
../fonts.nix
../home-manager-integration.nix
../opacity.nix
../palette.nix
../pixel.nix
../target.nix
../release.nix
../overlays.nix
] ++ autoload;
config.warnings =
lib.mkIf

View file

@ -1,18 +1,17 @@
inputs:
{ lib, ... }:
let
autoload = import ../autoload.nix { inherit lib inputs; } "droid";
autoload = import ../autoload.nix { inherit lib; } "droid";
in
{
imports = [
"${inputs.self}/stylix/fonts.nix"
"${inputs.self}/stylix/home-manager-integration.nix"
"${inputs.self}/stylix/opacity.nix"
"${inputs.self}/stylix/palette.nix"
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
(lib.modules.importApply "${inputs.self}/stylix/overlays.nix" inputs)
../fonts.nix
../home-manager-integration.nix
../opacity.nix
../palette.nix
../pixel.nix
../target.nix
../overlays.nix
] ++ autoload;
# See https://github.com/nix-community/nix-on-droid/issues/436

View file

@ -1,32 +1,27 @@
inputs:
{
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
# documentation.
let
autoload = import ../autoload.nix { inherit lib inputs; } "hm";
autoload = import ../autoload.nix { inherit lib; } "hm";
in
{
imports = [
"${inputs.self}/stylix/cursor.nix"
"${inputs.self}/stylix/fonts.nix"
"${inputs.self}/stylix/hm/cursor.nix"
"${inputs.self}/stylix/hm/fonts.nix"
"${inputs.self}/stylix/hm/icon.nix"
"${inputs.self}/stylix/hm/palette.nix"
"${inputs.self}/stylix/icon.nix"
"${inputs.self}/stylix/opacity.nix"
"${inputs.self}/stylix/palette.nix"
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(lib.modules.importApply "${inputs.self}/stylix/overlays.nix" inputs)
./cursor.nix
./fonts.nix
./icon.nix
./palette.nix
../cursor.nix
../fonts.nix
../icon.nix
../opacity.nix
../palette.nix
../pixel.nix
../target.nix
../release.nix
../overlays.nix
] ++ autoload;
config.warnings =
lib.mkIf

View file

@ -1,16 +1,14 @@
{
pkgs,
lib,
inputs,
...
}:
builtins.mapAttrs
(
_: value:
if (builtins.typeOf value == "lambda") then
if builtins.isFunction value then
(value {
inherit pkgs;
lib = pkgs.lib.extend (
lib = lib.extend (
_: prev: {
maintainers = lib.attrsets.unionOfDisjoint prev.maintainers (import ./maintainers.nix);
}
@ -21,9 +19,9 @@ builtins.mapAttrs
)
(
lib.concatMapAttrs (
path: kind:
target: kind:
lib.optionalAttrs (kind == "directory") {
${path} = import "${inputs.self}/modules/${path}/meta.nix";
${target} = import ../modules/${target}/meta.nix;
}
) (builtins.readDir "${inputs.self}/modules")
) (builtins.readDir ../modules)
)

View file

@ -1,31 +1,26 @@
inputs:
{
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
# documentation.
let
autoload = import ../autoload.nix { inherit lib inputs; } "nixos";
autoload = import ../autoload.nix { inherit lib; } "nixos";
in
{
imports = [
"${inputs.self}/stylix/cursor.nix"
"${inputs.self}/stylix/fonts.nix"
"${inputs.self}/stylix/home-manager-integration.nix"
"${inputs.self}/stylix/nixos/cursor.nix"
"${inputs.self}/stylix/nixos/fonts.nix"
"${inputs.self}/stylix/nixos/palette.nix"
"${inputs.self}/stylix/opacity.nix"
"${inputs.self}/stylix/palette.nix"
"${inputs.self}/stylix/pixel.nix"
"${inputs.self}/stylix/target.nix"
"${inputs.self}/stylix/release.nix"
(lib.modules.importApply "${inputs.self}/stylix/overlays.nix" inputs)
./cursor.nix
./fonts.nix
./palette.nix
../cursor.nix
../fonts.nix
../home-manager-integration.nix
../opacity.nix
../palette.nix
../pixel.nix
../target.nix
../release.nix
../overlays.nix
] ++ autoload;
config.warnings =
lib.mkIf

View file

@ -1,4 +1,3 @@
inputs:
{
lib,
pkgs,
@ -33,5 +32,5 @@ inputs:
attrs.overlay
];
}
) (import ./autoload.nix { inherit lib inputs; } "overlay");
) (import ./autoload.nix { inherit lib; } "overlay");
}

View file

@ -8,43 +8,40 @@
let
isEnabled = pkgs.callPackage ./is-enabled.nix { };
autoload =
let
directory = "testbeds";
modules = "${inputs.self}/modules";
in
lib.pipe modules [
builtins.readDir
builtins.attrNames
(builtins.concatMap (
module:
autoload = lib.pipe ../../modules [
builtins.readDir
builtins.attrNames
(builtins.concatMap (
module:
let
testbeds = ../../modules/${module}/testbeds;
files = lib.optionalAttrs (builtins.pathExists testbeds) (
builtins.readDir testbeds
);
in
lib.mapAttrsToList (
testbed: type:
let
testbeds = "${modules}/${module}/${directory}";
files = lib.optionalAttrs (builtins.pathExists testbeds) (
builtins.readDir testbeds
);
path = testbeds + "/${testbed}";
pathStr = toString path;
in
lib.mapAttrsToList (
testbed: type:
if type != "regular" then
throw "${testbed} must be regular: ${type}"
if type != "regular" then
throw "${pathStr} must be regular: ${type}"
else if !lib.hasSuffix ".nix" testbed then
throw "testbed must be a Nix file: ${testbeds}/${testbed}"
else if !lib.hasSuffix ".nix" testbed then
throw "testbed must be a Nix file: ${pathStr}"
else if testbed == ".nix" then
throw "testbed must have a name: ${testbed}"
else if testbed == ".nix" then
throw "testbed must have a name: ${pathStr}"
else
{
inherit module;
name = lib.removeSuffix ".nix" testbed;
path = "${testbeds}/${testbed}";
}
) files
))
];
else
{
inherit module path;
name = lib.removeSuffix ".nix" testbed;
}
) files
))
];
makeTestbed =
testbed: themeName: themeModule: