Skip to content
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

nushell: Structured settings #6184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JoaquinTrinanes
Copy link
Contributor

@JoaquinTrinanes JoaquinTrinanes commented Dec 8, 2024

Description

Closes #5527 (but it should be fixed on new nushell versions regardless of this PR if programs.nushell.configFile is set in an adequate way)

Hence, this PR reintroduces the programs.nushell.settings option. It then flattens the config and assigns each value to $env.config. The flattening step is done to avoid overwriting previously defined config values (otherwise, completions.quick = true would become $env.config.completions = { quick: true }, deleting all other completions config).

{
  show_banner = false;
  completions.external = {
    enable = true;
    max_results = 200;
  };
}

becomes

$env.config.completions.external.enable = true
$env.config.completions.external.max_results = 200
$env.config.show_banner = false

One thing I'm wondering is if the code should have a special cases for lists/records, and append/merge them instead of assigning them. This could be implemented on a followup PR if we see the need 🤔

Checklist

  • Change is backwards compatible.

  • Code formatted with ./format.

  • Code tested through nix-shell --pure tests -A run.all or nix develop --ignore-environment .#all using Flakes.

  • Test cases updated/added. See example.

  • Commit messages are formatted like

    {component}: {description}
    
    {long description}
    

    See CONTRIBUTING for more information and recent commit messages for examples.

Maintainer CC

@Philipp-M @aidalgol

@JoaquinTrinanes JoaquinTrinanes force-pushed the nushell/structured-settings branch 2 times, most recently from 5d96d63 to bd98b3f Compare December 9, 2024 16:30
Comment on lines 196 to 211
(let
flattenSettings = settings:
let
unravel = prefixes: value:
if (lib.isAttrs value && !isNushellInline value) then
lib.flatten
(map (key: unravel (prefixes ++ [ key ]) value.${key})
(builtins.attrNames value))
else
lib.nameValuePair (lib.concatStringsSep "." prefixes) value;
in lib.listToAttrs (unravel [ ] settings);

flattenedSettings = flattenSettings cfg.settings;
in lib.mkIf (cfg.settings != { }) (lib.concatLines (lib.mapAttrsToList
(key: value: "$env.config.${key} = ${toNushell { } value}")
flattenedSettings)))
Copy link
Member

@rycee rycee Dec 10, 2024

Choose a reason for hiding this comment

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

An alternative implementation that I personally find slightly more readable and is potentially a bit more efficient since it avoids the intermediate attribute set (although I imagine you'll need many settings for it to be noticeable 🙂).

I haven't tested it beyond the test case and feel free to reject the suggestion if you prefer the original version:

Suggested change
(let
flattenSettings = settings:
let
unravel = prefixes: value:
if (lib.isAttrs value && !isNushellInline value) then
lib.flatten
(map (key: unravel (prefixes ++ [ key ]) value.${key})
(builtins.attrNames value))
else
lib.nameValuePair (lib.concatStringsSep "." prefixes) value;
in lib.listToAttrs (unravel [ ] settings);
flattenedSettings = flattenSettings cfg.settings;
in lib.mkIf (cfg.settings != { }) (lib.concatLines (lib.mapAttrsToList
(key: value: "$env.config.${key} = ${toNushell { } value}")
flattenedSettings)))
(let
flattenSettings = let
joinDot = a: b: "${if a == "" then "" else "${a}."}${b}";
unravel = prefix: value:
if lib.isAttrs value && !isNushellInline value then
lib.concatMap (key: unravel (joinDot prefix key) value.${key})
(builtins.attrNames value)
else
[ (lib.nameValuePair prefix value) ];
in unravel "";
mkLine = { name, value }: ''
$env.config.${name} = ${toNushell { } value}
'';
settingsLines =
lib.concatMapStrings mkLine (flattenSettings cfg.settings);
in lib.mkIf (cfg.settings != { }) settingsLines)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooh thanks! I prefer your implementation, it's way easier to understand 😄

- Remove 'with lib'
- More idiomatic lib calls
@JoaquinTrinanes JoaquinTrinanes force-pushed the nushell/structured-settings branch from bd98b3f to 16aa273 Compare December 11, 2024 12:34
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.

bug: nushell module configuration overwrites default basic configuration
2 participants