1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-07-30 13:47:46 +00:00
nu_scripts/modules/background_task
shimeoki 45934922f1
fix: pause in background_task (#1138)
i found out that it worked incorrectly, so i fixed it based on other
functions in the module
2025-06-29 13:07:57 -05:00
..
README.md In task module mention job command (nushell background jobs) (#1107) 2025-04-29 11:41:17 -05:00
task.nu fix: pause in background_task (#1138) 2025-06-29 13:07:57 -05:00

Background tasks with pueue

Makes Nushell "support" background tasks.

Note: Nushell has native background jobs support through the job command. The task commands introduced here spawn new and independent processes rather than background threads like job. The task processes will keep running even when you exit the current Nushell process, the job background threads will not.

Prerequisite

Install pueue and make sure pueued is running and that pueue is in PATH.

Usage

You will get tab completions and suggestions when you install the module. Please check those.

To install the module, copy the task.nu to the $env.NU_LIB_DIRS directory, then do:

use task.nu

In your Nushell config under ~/.config/nushell.

Q&A

How can I pass data to a background task?

You can use environment variables, since they are inherited from the parent when spawning a process.

$env.FOO = 123

task spawn {
  echo $env.FOO
}

If you want to pass serialized data, you can do this:

let foo = { a: 1 b: 2 c: 3 }

with-env { FOO: ($foo | to json) } {
  task spawn {
    let foo = ($env.FOO | from json)

    echo $foo
  }
}

How can I reuse custom commands in a background task?

You can define these commands in a separate module, like so:

# --- in foo.nu ---
export def bar [] { echo bar }

# --- in main.nu ---
task spawn {
  use foo.nu

  foo bar
}

Troubleshooting

  • On some setups (e.g. NixOS with nu installed as a binary in user's $HOME), sh (which pueue delegates tasks to run) might fail to find nu in the $PATH. In this case hard-coding the location of your nu binary in the task spawn function definition in task.nu can solve the issue.