-
-
Notifications
You must be signed in to change notification settings - Fork 327
stylix: extract testbed modules to autoload.nix
#1520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| { | ||
| pkgs, | ||
| lib, | ||
| testbedFieldSeparator ? ":", | ||
| }: | ||
| let | ||
| isEnabled = pkgs.callPackage ./is-enabled.nix { }; | ||
|
|
||
| # Describes all testbed modules loaded from the modules directory. | ||
| # A list of attrsets, each containing: { module, path, name } | ||
| testbeds = 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 | ||
| path = testbeds + "/${testbed}"; | ||
| pathStr = toString path; | ||
| in | ||
| if type != "regular" then | ||
| throw "${pathStr} must be regular: ${type}" | ||
|
|
||
| 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: ${pathStr}" | ||
|
|
||
| else | ||
| { | ||
| inherit module path; | ||
| name = lib.removeSuffix ".nix" testbed; | ||
| } | ||
| ) files | ||
| )) | ||
| ]; | ||
|
|
||
| # Import all the testbed themes | ||
| themes = lib.pipe ./themes [ | ||
| builtins.readDir | ||
| (lib.filterAttrs (name: _: lib.strings.hasSuffix ".nix" name)) | ||
| (builtins.mapAttrs ( | ||
| name: type: | ||
| lib.throwIfNot (type == "regular") | ||
| "Unexpected filetype in testbed themes: ${toString ./themes/${name}} is a ${type}." | ||
| ./themes/${name} | ||
| )) | ||
| (lib.mapAttrs' (name: lib.nameValuePair (lib.strings.removeSuffix ".nix" name))) | ||
| ]; | ||
|
|
||
| # Construct a NixOS module for the testbed+theme combination. | ||
| # | ||
| # If the testbed module is enabled, returns an attrset containing it: | ||
| # { «name» = «modele»; } | ||
| # | ||
| # Returns an empty attrset if the testbed module is not enabled. | ||
| # { } | ||
| makeTestbedModule = | ||
| testbed: themeName: themeModule: | ||
| let | ||
| joinFields = builtins.concatStringsSep testbedFieldSeparator; | ||
|
|
||
| fields = | ||
| map | ||
| ( | ||
| field: | ||
| lib.throwIf (lib.hasInfix testbedFieldSeparator field) | ||
| "testbed field must not contain the '${testbedFieldSeparator}' testbed field separator: ${field}" | ||
| field | ||
| ) | ||
| [ | ||
| "testbed" | ||
| testbed.name | ||
| themeName | ||
| ]; | ||
|
|
||
| name = joinFields fields; | ||
| in | ||
| lib.optionalAttrs (isEnabled testbed.path) { | ||
| ${name} = { | ||
| # Unique key for de-duplication | ||
| key = joinFields [ | ||
| (toString ./.) | ||
| name | ||
| ]; | ||
|
|
||
| # Location displayed in errors | ||
| _file = name; | ||
|
|
||
| # Avoid accidental imports, e.g. in submodules or home-manager | ||
| _class = "nixos"; | ||
|
|
||
| imports = [ | ||
| testbed.path | ||
| themeModule | ||
| ]; | ||
|
|
||
| config.system.name = name; | ||
| }; | ||
| }; | ||
|
|
||
| # Generates a copy of each testbed for each of the imported themes. | ||
| # I.e. a testbeds*themes matrix | ||
| makeTestbeds = testbed: lib.mapAttrsToList (makeTestbedModule testbed) themes; | ||
| in | ||
| # Testbeds are merged using lib.attrsets.unionOfDisjoint to throw an error if | ||
| # any name collides. | ||
| builtins.foldl' lib.attrsets.unionOfDisjoint { } ( | ||
| builtins.concatMap makeTestbeds testbeds | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.