Search

Dark theme | Light theme

April 19, 2026

Nushell Niceties: Wrapping External Commands With def --wrapped

The def keyword in Nushell is used to define a custom command. By adding the --wrapped flag you can tell Nushell to accept unknown flags and arguments and pass them on as strings. In the command parameters you use a "rest" parameter defined by …​ to capture the remaining arguments. Then in the command body you use the spread operator …​ to expand them again. This is very useful when you want to create a small wrapper around an external command with lots of options, but without having to define all those options yourself.

In the following example you see how this can be used to wrap the external command kubectl in a custom command with the name k. You can define the following wrapper command:

# Define custom command k with --wrapped option.
def --wrapped k [...rest] {
    # Forward all arguments passed to the this command
    # to the kubectl command.
    # Add extra -o json argument so the output is JSON.
    # Use from json to parse the JSON into Nushell data structure,
    # so you can use default Nushell commands for querying, selecting
    # and more.
    ^kubectl ...$rest -o json | from json
}

The following example shows how the custom k command can be used to query the JSON output using Nushell commands:

> k get pod api-7d9b6c7f9d-abcde -n demo
    | select metadata.name status.phase spec.nodeName
╭───────────────┬──────────────────────╮
│ metadata.name │ api-7d9b6c7f9d-abcde │
│ status.phase  │ Running              │
│ spec.nodeName │ worker-1             │
╰───────────────┴──────────────────────╯

# Using get items for JSON array output.
> k get pods -n demo
    | get items
    | where {|pod| $pod.status.phase in ['Pending', 'Failed'] and $pod.spec.nodeName == 'worker-2' }
    | select metadata.name status.phase spec.nodeName metadata.namespace
    | rename pod phase node namespace
╭───┬─────────────────────────┬─────────┬──────────┬───────────╮
│ # │           pod           │  phase  │   node   │ namespace │
├───┼─────────────────────────┼─────────┼──────────┼───────────┤
│ 0 │ worker-5b7d8b9f7f-r2mql │ Pending │ worker-2 │ demo      │
│ 1 │ batch-77c99f88c-hj2lm   │ Failed  │ worker-2 │ demo      │
╰───┴─────────────────────────┴─────────┴──────────┴───────────╯

Written with Nushell 0.112.2.