Skip to content

stylix: add imports to mkTarget#1363

Merged
0xda157 merged 1 commit intonix-community:masterfrom
0xda157:mkTarget-import-warning
May 31, 2025
Merged

stylix: add imports to mkTarget#1363
0xda157 merged 1 commit intonix-community:masterfrom
0xda157:mkTarget-import-warning

Conversation

@0xda157
Copy link
Copy Markdown
Contributor

@0xda157 0xda157 commented May 23, 2025

imports is necessary for renamed/removed options. Note that imports is "unsafe" and might cause failure to check != null (maybe a different way would be better?)

cc @trueNAHO @MattSturgeon @Flameopathic

Things done

Notify maintainers

@0xda157 0xda157 marked this pull request as ready for review May 23, 2025 02:32
@0xda157 0xda157 force-pushed the mkTarget-import-warning branch from 7f9cd69 to 04c67c2 Compare May 23, 2025 02:35
@MattSturgeon
Copy link
Copy Markdown
Member

Note that imports is an "unsafe" input and might cause failure to check != null

One approach would be to have mkTarget apply mkTarget to each import?

Or maybe that doesn't make sense because not all imports will need an enable option. If so, perhaps mkTarget should be split into a function for creating "whole" targets and another for creating "parts" of targets?

A "part" could have extra options, config elements, and imports. A "whole" target has those and also an enable option.

Although it'd be better if we don't need this complexity. I.e. if imports are only used for creating additional "sub" targets with an enable option.

@0xda157
Copy link
Copy Markdown
Contributor Author

0xda157 commented May 23, 2025

One approach would be to have mkTarget apply mkTarget to each import?

Or maybe that doesn't make sense because not all imports will need an enable option. If so, perhaps mkTarget should be split into a function for creating "whole" targets and another for creating "parts" of targets?

A "part" could have extra options, config elements, and imports. A "whole" target has those and also an enable option.

Although it'd be better if we don't need this complexity. I.e. if imports are only used for creating additional "sub" targets with an enable option.

I was think adding imports so we could use mkRenamedOption and mkRemovedOption, I don't think adding imports to import other full modules is necessary.

@MattSturgeon
Copy link
Copy Markdown
Member

MattSturgeon commented May 23, 2025

I was think adding imports so we could use mkRenamedOption and mkRemovedOption, I don't think adding imports to import other full modules is necessary.

Ah. That makes sense.

If you want to prevent importing arbitrary modules, but want to support renaming or removing options, the only way I can think of is by having mkTarget map some other list applying the relevant functions.

I.e. adding a renames attribute and a removed attribute to mkTarget's args.

However this won't scale well as you'd have to have a hard-coded attr for each function you wanted to support. E.g. mkChangedOptionModule, mkMergedOptionModule, mkAliasOptionModule, etc are used less often, but would need changes to mkTarget if you ever needed to use them.


You could have a more flexible design, by having a single option whose entries specify the function used:

mkTarget {
  modules = [
    {
      function = "mkRenamedOptionModule";
      apply = fn: fn [ "foo" "bar" ] [ "x" "y" ];
    }
  ];
}

Which is still kinda clunky, but provides a nice middle ground of not being too inflexible while still restricting to lib.modules.${function} functions.

@Flameopathic
Copy link
Copy Markdown
Contributor

The way I envisioned this working was basically as follows:

{ mkTarget, lib, ... }:
{
  imports = [
    /* etc */
  ];
} // mkTarget { /* etc */ }

I don't see a reason to limit what people can import or make warnings for; are there any other reasons that this pattern would be undesirable (or possibly non-functional)?

@0xda157
Copy link
Copy Markdown
Contributor Author

0xda157 commented May 23, 2025

The way I envisioned this working was basically as follows:

{ mkTarget, lib, ... }:
{
  imports = [
    /* etc */
  ];
} // mkTarget { /* etc */ }

I don't see a reason to limit what people can import or make warnings for; are there any other reasons that this pattern would be undesirable (or possibly non-functional)?

Manually having to call mkTarget is bad ux and we should remove it once we've transferred all the modules to mkTarget (if they can all be transferred to mkTarget). Treating mkTarget as something you merge with the rest of the module seems really janky and undesirable in the mean time.


In my view the final form of mkTarget would be each module is either

{ lib, pkgs } -> MkTargetBody

or

{ lib, pkgs } -> [ MkTargetBody ]

e.g.

{ lib, pkgs }:
{
  name = "foo";
  humanName = "Foo";

  configElements = [ ];
}

or

{ lib, pkgs }:
map 
  (
    { name, humanName }:
    {
       inherit name humanName;
       
       configElements = [ ];
    }
  )
  [
    {
      name = "foo";
      humanName = "Foo";
    }
    {
      name = "bar";
      humanName = "Bar";
    }
  ]

@trueNAHO
Copy link
Copy Markdown
Member

trueNAHO commented May 23, 2025

You could have a more flexible design, by having a single option whose entries specify the function used:

mkTarget {
  modules = [
    {
      function = "mkRenamedOptionModule";
      apply = fn: fn [ "foo" "bar" ] [ "x" "y" ];
    }
  ];
}

Which is still kinda clunky, but provides a nice middle ground of not being too inflexible while still restricting to lib.modules.${function} functions.

Yes, this seems like the easiest way to scale this, while ensuring imports being local to their specific mkTarget instance. Should the parameter be called imports instead of modules to make it more obvious what is being interfaced?

However this won't scale well as you'd have to have a hard-coded attr for each function you wanted to support. E.g. mkChangedOptionModule, mkMergedOptionModule, mkAliasOptionModule, etc are used less often, but would need changes to mkTarget if you ever needed to use them.

Although, this [{ function :: str, apply :: Fn }] pattern generally interfaces lib.modules.<FUNCTION>, there are not many functions that would have to be re-exported in mkTarget, and lib.modules.<FUNCTION> is very likely to receive substantial new functions only rarely. This would improve the API into something like the following (feel free to suggest improvements):

mkTarget {
  imports.mkRenamedOptionModule = [[[ "foo" "bar" ] [ "x" "y" ]]];
}

In my view the final form of mkTarget would be each module is either

{ lib, pkgs } -> MkTargetBody

or

{ lib, pkgs } -> [ MkTargetBody ]

e.g.

{ lib, pkgs }:
{
  name = "foo";
  humanName = "Foo";

  configElements = [ ];
}

or

{ lib, pkgs }:
map
  (
    { name, humanName }:
    {
       inherit name humanName;

       configElements = [ ];
    }
  )
  [
    {
      name = "foo";
      humanName = "Foo";
    }
    {
      name = "bar";
      humanName = "Bar";
    }
  ]

For reference, this differs from my proposal in #1341 (comment) by not treating "simple" modules as pure attribute sets.

@0xda157 0xda157 force-pushed the mkTarget-import-warning branch from 04c67c2 to 496a1d5 Compare May 23, 2025 17:56
@0xda157 0xda157 changed the title stylix: add imports and warnings to mkTarget stylix: add imports to mkTarget May 23, 2025
@Flameopathic
Copy link
Copy Markdown
Contributor

I don't see a reason to limit what people can import or make warnings for; are there any other reasons that this pattern would be undesirable (or possibly non-functional)?

Manually having to call mkTarget is bad ux and we should remove it once we've transferred all the modules to mkTarget (if they an all be transferred to mkTarget). Treating mkTarget as something you merge with the rest of the module seems really janky and undesirable in the mean time.

I see what you mean. In that case, I support this PR as-is, as I do not see a reason to limit what can be imported.

Copy link
Copy Markdown
Member

@trueNAHO trueNAHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason to limit what people can import or make warnings for; are there any other reasons that this pattern would be undesirable (or possibly non-functional)?

Manually having to call mkTarget is bad ux and we should remove it once we've transferred all the modules to mkTarget (if they an all be transferred to mkTarget). Treating mkTarget as something you merge with the rest of the module seems really janky and undesirable in the mean time.

I see what you mean. In that case, I support this PR as-is, as I do not see a reason to limit what can be imported.

Considering the ongoing mkTarget migration may require this functionality, I am fine interfacing imports for now and potentially polishing this in the future: #1363 (comment).

@0xda157 0xda157 merged commit 093087e into nix-community:master May 31, 2025
8 checks passed
@0xda157 0xda157 deleted the mkTarget-import-warning branch May 31, 2025 18:17
stylix-automation bot pushed a commit that referenced this pull request May 31, 2025
Link: #1363

Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
(cherry picked from commit 093087e)
@stylix-automation
Copy link
Copy Markdown
Contributor

Successfully created backport PR for release-25.05:

0xda157 added a commit that referenced this pull request May 31, 2025
Link: #1363

Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
(cherry picked from commit 093087e)
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Jul 18, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Jul 19, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Jul 19, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Jul 30, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Aug 4, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Nov 21, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Nov 21, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Dec 10, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
trueNAHO added a commit to trueNAHO/stylix that referenced this pull request Dec 10, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
(cherry picked from commit 9afd823)
0xda157 pushed a commit that referenced this pull request Dec 10, 2025
Fixes: 093087e ("stylix: add imports to mkTarget (#1363)")
(cherry picked from commit 9afd823)
rwxae pushed a commit to rwxae/stylix that referenced this pull request Jan 5, 2026
Fixes: 093087e ("stylix: add imports to mkTarget (nix-community#1363)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants