Skip to content
Edit this page

004: Nushell Integration

Context

Static clap_complete scripts complete fixed command flags, yet they cannot query dynamic tasks, targets, variables, or managed prefixes from winesmith.toml. They also reject arbitrary Windows /flag switches on winesmith exec and positional KEY=VALUE variables on winesmith run inside Nushell. Nushell 0.117+ provides extern --strict, @structured, def --wrapped, and $nu.user-autoload-dirs to support parse-time checking, structured pipelines, and dynamic project completion together.

Benchmark

Generic clap_complete_nushell outputs lack @structured JSON decoding, def --wrapped flag passthrough, and local project completers. Writing native winesmith externs and wrappers keeps interactive terminal output readable while letting piped commands (winesmith env list | where windows == "win7") work on Nushell tables directly.

Interaction flow

Autoload setup, typed parsing, structured piping, and dynamic tab completion operate across four steps:

  1. Run winesmith completions install nu once to write $XDG_CONFIG_HOME/nushell/autoload/winesmith.nu (default ~/.config/nushell/autoload/winesmith.nu, or --path <FILE>).
  2. Restart Nushell or source the file so fixed subcommands (init, setup, shell, doctor, env *, runner *, config, tasks, completions) validate flags at parse time via extern --strict.
  3. Pipe any of the seven query commands (doctor, env list, env inspect, runner list, runner detect, config, tasks) directly into Nushell filters. The @structured [--json] { from json } attribute injects --json and parses stdout into a table or record automatically.
  4. Use def --wrapped on winesmith run and winesmith exec so Windows /flags, -- operands, and KEY=VALUE arguments forward untouched to ^winesmith, while pressing TAB calls hidden winesmith __complete <kind>.

Visual design

This diagram shows how commands typed inside Nushell route through strict externs, @structured auto-decoding, wrapped passthroughs, and local __complete lookups:

flowchart LR
    Nu["Nushell 0.117+ Session"] -->|autoload| Script["~/.config/nushell/autoload/winesmith.nu"]
    Script -->|Fixed subcommands| Strict["extern --strict"]
    Script -->|Query subcommands| Struct["extern --strict + @structured [--json]"]
    Script -->|exec & run| Wrapped["def --wrapped (...$rest)"]
    Script -->|TAB completion| Hidden["^winesmith __complete <kind>"]
    Hidden --> Local["Local winesmith.toml & XDG stores (<50ms)"]
    Struct -->|Piped| Table["Native Nushell Table / Record"]
    Struct -->|Terminal| Text["Human-Readable Table"]

Implementation

Command signature and style rules

Every winesmith CLI flag and every command, extern, and completer emitted by winesmith completions nu MUST obey five rules:

  1. Declare explicit input -> output types on every def and extern. Write nothing when no value is consumed or returned, write the concrete output type (table, record, list<string>, string), and write any only when unknowable.
  2. Put each positional parameter and flag on its own line with its type after : and an inline # comment explaining its purpose without repeating the parameter name.
  3. Declare long two-dash flags exclusively (--all-targets, --force, --target, --verbose, --quiet, --help) and forbid single-dash short flags (-t, -v, -q, -D, -h) across both the Rust CLI and generated Nushell definitions.
  4. Use clear, consistent nouns and phrases across every command (directory rather than dir, manifest file to read rather than source).
  5. Place one space between the argument and #, start comments with a capital letter, end every comment with a period, keep comments on one line, and append Default: <value>. when a default exists.

Generated Nushell excerpt

Below is a representative slice of the script emitted by winesmith completions nu (src/completions/mod.rs):

# Return project task names and descriptions for tab completion.
def "nu-complete winesmith tasks" []: nothing -> table {
    ^winesmith __complete tasks | complete | if $in.exit_code == 0 { $in.stdout | from json } else { [] }
}

# Return project target profile names for tab completion.
def "nu-complete winesmith targets" []: nothing -> table {
    ^winesmith __complete targets | complete | if $in.exit_code == 0 { $in.stdout | from json } else { [] }
}

# List all managed Wine prefix directories.
@structured [--json] { from json }
extern --strict "winesmith env list" [
    --manifest-path: path # Manifest file to read. Default: nearest winesmith.toml.
    --json # Emit structured JSON output. Default: false.
    --verbose # Print detailed diagnostic logs to stderr. Default: false.
    --quiet # Suppress non-essential status messages. Default: false.
    --help # Show help information. Default: false.
]: nothing -> table

# Provision the runner, prefix directory, mounts, symlinks, toolchains, and lockfile.
extern --strict "winesmith setup" [
    --manifest-path: path # Manifest file to read. Default: nearest winesmith.toml.
    --target: string@"nu-complete winesmith targets" # Target profile to provision.
    --all-targets # Provision every declared target profile. Default: false.
    --locked # Fail when winesmith.lock is missing or outdated. Default: false.
    --reinstall # Rebuild prefix settings and reinstall toolchains. Default: false.
    --verbose # Print detailed diagnostic logs to stderr. Default: false.
    --quiet # Suppress non-essential status messages. Default: false.
    --help # Show help information. Default: false.
]: nothing -> nothing

# Run a declared Wine or Linux host task from the project manifest.
def --wrapped "winesmith run" [
    task?: string@"nu-complete winesmith tasks" # Task name declared in winesmith.toml.
    --manifest-path: path # Manifest file to read. Default: nearest winesmith.toml.
    --target: string@"nu-complete winesmith targets" # Target profile to execute.
    --all-targets # Execute the task across every declared target profile. Default: false.
    --locked # Fail when winesmith.lock is missing or outdated. Default: false.
    ...rest: string@"nu-complete winesmith task-vars" # Variable overrides in key=value form and extra flags.
]: nothing -> nothing {
    mut args = []
    if $manifest_path != null { $args = ($args | append ["--manifest-path" $manifest_path]) }
    if $target != null { $args = ($args | append ["--target" $target]) }
    if $all_targets { $args = ($args | append "--all-targets") }
    if $locked { $args = ($args | append "--locked") }
    if $task != null { $args = ($args | append $task) }
    ^winesmith run ...$args ...$rest
}

Dynamic completion provider

winesmith __complete <kind> reads only local manifests and XDG state directories without network requests or Wine launches across six categories:

  • tasks lists declared task names and descriptions from the nearest winesmith.toml.
  • task-vars lists [vars] keys formatted as <key>= with their default value in description.
  • targets lists [targets.*] names alongside each target's resolved Windows version and architecture.
  • envs lists managed prefix directories found under $XDG_DATA_HOME/winesmith/prefixes/.
  • runners lists installed portable runners, detected system Wine and Proton installations, and the default Kron4ek tag.
  • toolchains lists built-in ad-hoc recipe prefixes (dotnet@, python@, inno@, nsis@).

Edge cases and constraints

Automated tests enforce three invariants on the generated script and completion subcommand:

  • Invoking winesmith __complete outside a project directory MUST exit 0 and emit [] with empty stderr.
  • Integration tests run source <generated_file> directly inside host nu whenever available and check @structured piping plus extern --strict rejection of short flags.
  • Callers can bypass wrappers and @structured decoding at any time with ^winesmith ....