-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
[RFC] add ability to merge structured configs #42838
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,57 +1,21 @@ | ||
| { lib | ||
| # we pass the kernel version here to keep a nice syntax `whenOlder "4.13"` | ||
| # kernelVersion, e.g., config.boot.kernelPackages.version | ||
| , version | ||
| , mkValuePreprocess ? null | ||
| }: | ||
| { lib, version }: | ||
|
|
||
| with lib; | ||
| rec { | ||
| # Common patterns | ||
| when = cond: opt: if cond then opt else null; | ||
| whenAtLeast = ver: when (versionAtLeast version ver); | ||
| whenOlder = ver: when (versionOlder version ver); | ||
| whenBetween = verLow: verHigh: when (versionAtLeast version verLow && versionOlder version verHigh); | ||
| # Common patterns/legacy | ||
| whenAtLeast = ver: mkIf (versionAtLeast version ver); | ||
| whenOlder = ver: mkIf (versionOlder version ver); | ||
| # range is (inclusive, exclusive) | ||
| whenBetween = verLow: verHigh: mkIf (versionAtLeast version verLow && versionOlder version verHigh); | ||
|
|
||
| # Keeping these around in case we decide to change this horrible implementation :) | ||
| option = x: if x == null then null else "?${x}"; | ||
| yes = "y"; | ||
| no = "n"; | ||
| module = "m"; | ||
|
|
||
| mkValue = val: | ||
| let | ||
| isNumber = c: elem c ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]; | ||
| in | ||
| if val == "" then "\"\"" | ||
| else if val == yes || val == module || val == no then val | ||
| else if all isNumber (stringToCharacters val) then val | ||
| else if substring 0 2 val == "0x" then val | ||
| else val; # FIXME: fix quoting one day | ||
| # Keeping these around in case we decide to change this horrible implementation :) | ||
| option = x: | ||
| x // { optional = true; }; | ||
|
|
||
| yes = { tristate = "y"; }; | ||
| no = { tristate = "n"; }; | ||
| module = { tristate = "m"; }; | ||
| freeform = x: { freeform = x; }; | ||
|
|
||
| # generate nix intermediate kernel config file of the form | ||
| # | ||
| # VIRTIO_MMIO m | ||
| # VIRTIO_BLK y | ||
| # VIRTIO_CONSOLE n | ||
| # NET_9P_VIRTIO? y | ||
| # | ||
| # Use mkValuePreprocess to preprocess option values, aka mark 'modules' as | ||
| # 'yes' or vice-versa | ||
| # Borrowed from copumpkin https://github.com/NixOS/nixpkgs/pull/12158 | ||
| # returns a string, expr should be an attribute set | ||
| generateNixKConf = exprs: mkValuePreprocess: | ||
| let | ||
| mkConfigLine = key: rawval: | ||
| let | ||
| val = if builtins.isFunction mkValuePreprocess then mkValuePreprocess rawval else rawval; | ||
| in | ||
| if val == null | ||
| then "" | ||
| else if hasPrefix "?" val | ||
| then "${key}? ${mkValue (removePrefix "?" val)}\n" | ||
| else "${key} ${mkValue val}\n"; | ||
| mkConf = cfg: concatStrings (mapAttrsToList mkConfigLine cfg); | ||
| in mkConf exprs; | ||
| } |
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,137 @@ | ||
| { lib, config, ... }: | ||
|
|
||
| with lib; | ||
| let | ||
| findWinner = candidates: winner: | ||
| any (x: x == winner) candidates; | ||
|
|
||
| # winners is an ordered list where first item wins over 2nd etc | ||
| mergeAnswer = winners: locs: defs: | ||
| let | ||
| values = map (x: x.value) defs; | ||
| freeformAnswer = intersectLists values winners; | ||
| inter = intersectLists values winners; | ||
| winner = head winners; | ||
| in | ||
| if defs == [] then abort "This case should never happen." | ||
| else if winner == [] then abort "Give a valid list of winner" | ||
| else if inter == [] then mergeOneOption locs defs | ||
| else if findWinner values winner then | ||
| winner | ||
| else | ||
| mergeAnswer (tail winners) locs defs; | ||
|
|
||
| mergeFalseByDefault = locs: defs: | ||
| if defs == [] then abort "This case should never happen." | ||
| else if any (x: x == false) defs then false | ||
| else true; | ||
|
|
||
| kernelItem = types.submodule { | ||
| options = { | ||
| tristate = mkOption { | ||
| type = types.enum [ "y" "m" "n" null ] // { | ||
| merge = mergeAnswer [ "y" "m" "n" ]; | ||
| }; | ||
| default = null; | ||
| internal = true; | ||
| visible = true; | ||
| description = '' | ||
| Use this field for tristate kernel options expecting a "y" or "m" or "n". | ||
| ''; | ||
| }; | ||
|
|
||
| freeform = mkOption { | ||
| type = types.nullOr types.str // { | ||
| merge = mergeEqualOption; | ||
| }; | ||
| default = null; | ||
| example = ''MMC_BLOCK_MINORS.freeform = "32";''; | ||
| description = '' | ||
| Freeform description of a kernel configuration item value. | ||
| ''; | ||
| }; | ||
|
|
||
| optional = mkOption { | ||
| type = types.bool // { merge = mergeFalseByDefault; }; | ||
| default = false; | ||
| description = '' | ||
| Wether option should generate a failure when unused. | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| mkValue = with lib; val: | ||
| let | ||
| isNumber = c: elem c ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]; | ||
|
|
||
| in | ||
| if (val == "") then "\"\"" | ||
| else if val == "y" || val == "m" || val == "n" then val | ||
| else if all isNumber (stringToCharacters val) then val | ||
| else if substring 0 2 val == "0x" then val | ||
| else val; # FIXME: fix quoting one day | ||
|
|
||
|
|
||
| # generate nix intermediate kernel config file of the form | ||
| # | ||
| # VIRTIO_MMIO m | ||
| # VIRTIO_BLK y | ||
| # VIRTIO_CONSOLE n | ||
| # NET_9P_VIRTIO? y | ||
| # | ||
| # Borrowed from copumpkin https://github.com/NixOS/nixpkgs/pull/12158 | ||
| # returns a string, expr should be an attribute set | ||
| # Use mkValuePreprocess to preprocess option values, aka mark 'modules' as 'yes' or vice-versa | ||
| # use the identity if you don't want to override the configured values | ||
| generateNixKConf = exprs: | ||
| let | ||
| mkConfigLine = key: item: | ||
| let | ||
| val = if item.freeform != null then item.freeform else item.tristate; | ||
| in | ||
| if val == null | ||
| then "" | ||
| else if (item.optional) | ||
| then "${key}? ${mkValue val}\n" | ||
| else "${key} ${mkValue val}\n"; | ||
|
|
||
| mkConf = cfg: concatStrings (mapAttrsToList mkConfigLine cfg); | ||
| in mkConf exprs; | ||
|
|
||
| in | ||
| { | ||
|
|
||
| options = { | ||
|
|
||
| intermediateNixConfig = mkOption { | ||
| readOnly = true; | ||
| type = types.lines; | ||
| example = '' | ||
| USB? y | ||
| DEBUG n | ||
| ''; | ||
| description = '' | ||
| The result of converting the structured kernel configuration in settings | ||
| to an intermediate string that can be parsed by generate-config.pl to | ||
| answer the kernel `make defconfig`. | ||
| ''; | ||
| }; | ||
|
|
||
| settings = mkOption { | ||
| type = types.attrsOf kernelItem; | ||
| example = literalExample '' with lib.kernel; { | ||
teto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "9P_NET" = yes; | ||
| USB = optional yes; | ||
| MMC_BLOCK_MINORS = freeform "32"; | ||
| }''; | ||
| description = '' | ||
| Structured kernel configuration. | ||
| ''; | ||
| }; | ||
| }; | ||
|
|
||
| config = { | ||
| intermediateNixConfig = generateNixKConf config.settings; | ||
teto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
Oops, something went wrong.
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.