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

Calling save from config.nu not saving file #15222

Open
amk91 opened this issue Mar 1, 2025 · 6 comments
Open

Calling save from config.nu not saving file #15222

amk91 opened this issue Mar 1, 2025 · 6 comments
Labels
question the issue author asks something

Comments

@amk91
Copy link

amk91 commented Mar 1, 2025

Hello everybody, I'm trying to set up oh-my-posh in my nushell, but I'm having issues with saving the oh-my-posh configuration on file and then source it.
This is my entire config.nu file:

source ./catppuccin_mocha.nu

$env.config.show_banner = false

$env.PATH = $env.PATH | append ($nu.default-config-dir | path join "dive")

save -f ./test.txt

oh-my-posh init nu --config $"($env.POSH_THEMES_PATH)/stelbent.minimal.omp.json" --print | save -f ($nu.default-config-dir | path join 'ohmyposh.nu')
source ./ohmyposh.nu

mkdir ~/.cache/carapace
carapace _carapace nushell | save -f ~/.cache/carapace/init.nu
source ~/.cache/carapace/init.nu

I had the same issue with the carapace call.
Running the same command in the terminal works correctly. The file is generated correctly.

Am I missing some flags and/or commands to run before trying to save the file?
I'm running on Win11 24H2, Windows Terminal.

Thanks

@ysthakur
Copy link
Member

ysthakur commented Mar 1, 2025

If an error is encountered at some point in your config.nu, the commands below it won't be run. When you start Nushell, do you see any errors printed out?

It looks like save -f ./test.txt may be what's causing the error. save needs a pipeline input to actually save into the file (e.g. "foo" | save -f ./test.txt).

@ysthakur ysthakur added the question the issue author asks something label Mar 1, 2025
@weirdan
Copy link
Contributor

weirdan commented Mar 2, 2025

Can we now generate and source the file in the same file? I thought it was still impossible.

@ysthakur
Copy link
Member

ysthakur commented Mar 2, 2025

Oh good catch @weirdan. No it's not possible to generate and source a file in the same file. @amk91 This part of the docs may help you understand why. You'll need to generate ohmyposh.nu and Carapce's init.nu before config.nu is loaded, and then you only source them inside your config.nu.

As far as I know, these are your options:

  • Manually generate the Oh My Posh and Carapace files once, by running oh-my-posh init nu and carapace _carapace nushell once from the REPL
  • Generate the files in your env.nu so that they are available to be sourced in your config.nu

@NotTheDr01ds
Copy link
Contributor

NotTheDr01ds commented Mar 2, 2025

I recommend using the vendor autoload directories for these configs.

Just run the following one-time, and then the Carapace and Oh-My-Posh configs will be loaded on every startup after that:

let autoload_dir = ($nu.data-dir | path join 'vendor/autoload')
mkdir $autoload_dir
carapace _carapace nushell | save -f $"($autoload_dir)/carapace.nu"
oh-my-posh init nu --config $"($env.POSH_THEMES_PATH)/stelbent.minimal.omp.json" --print | save -f $"($autoload_dir)/ohmyposh.nu"

Remove all of the other files and the related items from config.nu.

@amk91
Copy link
Author

amk91 commented Mar 3, 2025

Thank you for the reply, I'll try that later today!

Oh good catch @weirdan. No it's not possible to generate and source a file in the same file. @amk91 This part of the docs may help you understand why. You'll need to generate ohmyposh.nu and Carapce's init.nu before config.nu is loaded, and then you only source them inside your config.nu.

As far as I know, these are your options:

* Manually generate the Oh My Posh and Carapace files once, by running `oh-my-posh init nu` and `carapace _carapace nushell` once from the REPL

* Generate the files in your `env.nu` so that they are available to be sourced in your `config.nu`

I didn't try to use the env.nu file because in the file it says that the usage of the env.nu file will be deprecated.

@NotTheDr01ds
Copy link
Contributor

NotTheDr01ds commented Mar 3, 2025

I didn't try to use the env.nu file because in the file it says that the usage of the env.nu file will be deprecated.

Well, might be deprecated at some point in the future. But yes, we're seeing if we can discourage its use and have alternate ways of doing things.

A similar method to the env.nu is to use the statements I provided in config.nu instead.

  • Putting them in config.nu means that they run every time you start a new Nushell. I think that's fairly inefficient for startup time, personally. When you put those statements in config.nu, they run, then the vendor autoloads run next.

    Advantage: If something in Carapace or Oh-My-Posh changes, your config will use it. Disadvantage: Startup performance.

  • Running the statements one-time

    Advantage: Faster startup performance. Disadvantage: If something in Carapace or Oh-My-Posh changes, you might get an error during startup and then need to re-run its init nu/_carapace nushell generation. This happens rarely enough that I think it's a good trade-off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question the issue author asks something
Projects
None yet
Development

No branches or pull requests

4 participants