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 3 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 😄

@JoaquinTrinanes JoaquinTrinanes force-pushed the nushell/structured-settings branch from bd98b3f to 16aa273 Compare December 11, 2024 12:34
@nyabinary
Copy link

nyabinary commented Dec 28, 2024

bump @rycee :3
https://www.nushell.sh/blog/2024-12-24-nushell_0_101_0.html
Also, nushell 101 released with some config changes :3

@JoaquinTrinanes
Copy link
Contributor Author

@nyabinary do you think anything in this PR needs to change due to the release? The only thing I can think of right now is changing some examples to use const NU_* instead of $env.NU_*.

Also, according to the new docs using the env.nu file is not a "best practice" anymore. I could make it so that env vars are written to config.nu instead. But that should be a separate PR 🤔

@nyabinary
Copy link

@nyabinary do you think anything in this PR needs to change due to the release? The only thing I can think of right now is changing some examples to use const NU_* instead of $env.NU_*.

Also, according to the new docs using the env.nu file is not a "best practice" anymore. I could make it so that env vars are written to config.nu instead. But that should be a separate PR 🤔

Would be better to just do it in this PR tbh, maybe rename it to nushell: refactor?

@JoaquinTrinanes JoaquinTrinanes force-pushed the nushell/structured-settings branch from 16aa273 to 1e2e435 Compare December 28, 2024 19:25
- Remove 'with lib'
- More idiomatic lib calls
- Update config file examples with current best practices
@JoaquinTrinanes JoaquinTrinanes force-pushed the nushell/structured-settings branch from 1e2e435 to ea0b5ec Compare December 28, 2024 20:21
@JoaquinTrinanes
Copy link
Contributor Author

@nyabinary it was a simple-enough change that I included it in this PR to avoid the extra overhead ✨

|| aliasesStr != "" || cfg.settings != { };

aliasesStr = lib.concatLines
(lib.mapAttrsToList (k: v: "alias ${k} = ${v}") cfg.shellAliases);

Choose a reason for hiding this comment

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

Suggested change
(lib.mapAttrsToList (k: v: "alias ${k} = ${v}") cfg.shellAliases);
(lib.mapAttrsToList (k: v: "alias '${k}' = ${v}") cfg.shellAliases);

This should make it possible to define aliases with spaces. I figured it might make sense to include in this PR, if that's alright.

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
4 participants