diff options
author | rebecca “wiggles” turner <rbt@sent.as> | 2024-09-10 00:17:01 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@localhost> | 2024-09-10 00:17:01 +0000 |
commit | cc183fdbc14ce105a5661d646983f791978b9d5c (patch) | |
tree | b391cbe1186938f7c0f66d960181e08c628de70f | |
parent | f5ae72d44552d9d75e3826c7e59403cbdb13d43a (diff) | |
parent | fc4a160878986ac412c3991f55eab5bc139ce698 (diff) |
Merge "repl-overlays: Provide an elaborate example" into main
-rw-r--r-- | src/libexpr/eval-settings.hh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libexpr/eval-settings.hh b/src/libexpr/eval-settings.hh index 444e298a0..f7ad2d786 100644 --- a/src/libexpr/eval-settings.hh +++ b/src/libexpr/eval-settings.hh @@ -185,6 +185,54 @@ struct EvalSettings : Config else { } ``` + + Here's a more elaborate `repl-overlay`, which provides the following + variables: + - The original, unmodified variables are aliased to `original`. + - `legacyPackages.${system}` (if it exists) or `packages.${system}` + (otherwise) is aliased to `pkgs`. + - All attribute set variables with a `${system}` attribute are + abbreviated in the same manner; e.g. `devShells.${system}` is + shortened to `devShells`. + + For example, the following attribute set: + + ```nix + info: final: attrs: let + # Equivalent to nixpkgs `lib.optionalAttrs`. + optionalAttrs = predicate: attrs: + if predicate + then attrs + else {}; + + # If `attrs.${oldName}.${info.currentSystem}` exists, alias `${newName}` to + # it. + collapseRenamed = oldName: newName: + optionalAttrs (builtins.hasAttr oldName attrs + && builtins.hasAttr info.currentSystem attrs.${oldName}) + { + ${newName} = attrs.${oldName}.${info.currentSystem}; + }; + + # Alias `attrs.${oldName}.${info.currentSystem} to `${newName}`. + collapse = name: collapseRenamed name name; + + # Alias all `attrs` keys with an `${info.currentSystem}` attribute. + collapseAll = + builtins.foldl' + (prev: name: prev // collapse name) + {} + (builtins.attrNames attrs); + in + # Preserve the original bindings as `original`. + (optionalAttrs (! attrs ? original) + { + original = attrs; + }) + // (collapseRenamed "packages" "pkgs") + // (collapseRenamed "legacyPackages" "pkgs") + // collapseAll + ``` )"}; }; |