|
| 1 | +--- |
| 2 | +name: spacecraft-nu-guidelines |
| 3 | +description: Expert guidelines for writing high-performance, clean, and type-safe Nushell (Nu) script code. Triggers on any request involving Nushell scripts, command definitions, custom pipelines, closures, modules, environment variables, or config.nu configurations. By Spacecraft Software. |
| 4 | +--- |
| 5 | + |
| 6 | +# Spacecraft Nushell Guidelines |
| 7 | + |
| 8 | +**You are an expert Nushell (Nu) systems and language engineer at Spacecraft Software.** Always follow these rules when writing, reviewing, or refactoring Nushell script files (`.nu`) or terminal pipelines. Never deviate. These guidelines ensure type safety, correct environment scoping, efficient pipeline execution, and clean, readable scripting. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Core Philosophy |
| 13 | + |
| 14 | +- **Pipelines of Structured Data**: Nushell communicates via typed structures (tables, records, lists, paths) rather than untyped raw byte/text streams. Avoid string-parsing hacks. |
| 15 | +- **Fail Early with Parsed Checking**: Nushell parses and checks the entire script (including types, command names, and imports) before executing a single line. Ensure type annotations are precise. |
| 16 | +- **Value-Centric Execution**: Every command and block returns a value. There are no "statements" that produce nothing; even commands that exit normally return `null` (`nothing`). |
| 17 | +- **Implicit Returns**: The last evaluated expression in a function, block, or pipeline is automatically returned. Avoid verbose `return` keywords. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Mandatory Syntax & Layout Conventions |
| 22 | + |
| 23 | +### 1. Spacing & Operators |
| 24 | +- **Mandatory Single Space**: Put exactly one space before and after the pipe operator `|`, variable assignment `=`, arithmetic/comparison operators, and between arguments/options. |
| 25 | + - **Correct**: `let result = 4 + 2; ls | where size > 1mb` |
| 26 | + - **Incorrect**: `let result=4+2`, `ls|where size>1mb`, `ls | where size > 1mb` (consecutive spaces are forbidden unless inside a string). |
| 27 | +- **Operators vs. Redirection**: In Nushell, `>` is strictly the greater-than operator, not file redirection. Use pipes and the `save` command or native redirection operators (`o>`, `e>`, `o+e>`). |
| 28 | + - **Correct**: `"Hello" | save output.txt` or `"Hello" o> output.txt` |
| 29 | + - **Incorrect**: `echo "Hello" > output.txt` |
| 30 | + |
| 31 | +### 2. Lists & Records |
| 32 | +- **Omit Commas**: Do not use commas to separate items in lists or fields in records. Commas are optional and discouraged in Spacecraft Software style. |
| 33 | + - **Correct**: `let list = [1 2 3]`, `let record = {name: "Alice" role: "admin"}` |
| 34 | + - **Incorrect**: `let list = [1, 2, 3]`, `let record = {name: "Alice", role: "admin"}` |
| 35 | +- **Spacing inside delimiters**: |
| 36 | + - Lists: Put no spaces right after the opening `[` or before the closing `]`. |
| 37 | + - **Correct**: `[1 2 3]` |
| 38 | + - Records & Blocks: Put exactly one space after `{` and before `}`. |
| 39 | + - **Correct**: `{ name: "Alice" }` |
| 40 | + - **Incorrect**: `{name: "Alice"}` |
| 41 | + |
| 42 | +### 3. Blocks & Closures |
| 43 | +- **Parameter formatting**: Put one space after separating commas and one space after the closing parameter pipe `|`. |
| 44 | + - **Correct**: `{ |x, y| $x + $y }` |
| 45 | + - **Incorrect**: `{|x,y|$x + $y}`, `{ |x,y| $x + $y }` |
| 46 | +- **Line length**: |
| 47 | + - Keep interactive one-liners under 80 characters. |
| 48 | + - For scripts, wrap pipelines, lists, or records exceeding 80 characters onto multiple lines: |
| 49 | + ```nu |
| 50 | + # Multi-line pipeline |
| 51 | + ls |
| 52 | + | where size > 50mb |
| 53 | + | sort-by modified |
| 54 | + | first 5 |
| 55 | + ``` |
| 56 | +
|
| 57 | +--- |
| 58 | +
|
| 59 | +## Variables & Mutability |
| 60 | +
|
| 61 | +Choose the variable declaration keyword based on timing and mutability: |
| 62 | +
|
| 63 | +| Keyword | Timing | Reassignable | Use Cases | |
| 64 | +| :--- | :--- | :--- | :--- | |
| 65 | +| **`let`** | Runtime | No (Immutable) | Normal variables, pipeline intermediates, temporary values. | |
| 66 | +| **`mut`** | Runtime | Yes (Mutable) | Loop accumulators, complex state tracking, iterative building. | |
| 67 | +| **`const`** | Parse-time | No (Constant) | Module import paths, static configuration, compile-time metadata. | |
| 68 | +
|
| 69 | +### Variable Mutation Rules |
| 70 | +- **Sigil Requirement**: Use the `$` prefix whenever referencing, reassigning, or mutating a variable. |
| 71 | + - **Correct**: `mut x = 1; $x = 2; $x += 1` |
| 72 | + - **Incorrect**: `mut x = 1; x = 2; x += 1` |
| 73 | +- **Closure Restrictions**: Closures (`{ |x| ... }`) cannot capture mutable variables (`mut`). If a closure needs to update a value, use `reduce` or pass values explicitly instead of mutating outer variables. |
| 74 | + - **Correct**: |
| 75 | + ```nu |
| 76 | + # Functional reduction |
| 77 | + let total = ([1 2 3] | reduce --fold 0 { |item, acc| $acc + $item }) |
| 78 | + ``` |
| 79 | + - **Incorrect**: |
| 80 | + ```nu |
| 81 | + mut total = 0 |
| 82 | + [1 2 3] | each { |item| $total += $item } # Fails: Closure cannot capture mutable variable |
| 83 | + ``` |
| 84 | +
|
| 85 | +--- |
| 86 | +
|
| 87 | +## Custom Commands (`def`) |
| 88 | +
|
| 89 | +Custom commands are typed, self-documenting functions. |
| 90 | +
|
| 91 | +### 1. Naming & Documentation |
| 92 | +- **Kebab-Case**: Command names must be lower-case kebab-case (e.g., `get-latest-logs`). |
| 93 | +- **Help Text**: Always provide description comments immediately above the command definition. These are parsed to populate the `help` system. |
| 94 | +
|
| 95 | +### 2. Signatures & Types |
| 96 | +- **Explicit Annotations**: Annotate all parameters and return types. |
| 97 | + ```nu |
| 98 | + # Calculates the square of an integer |
| 99 | + def square [ |
| 100 | + num: int # The number to square |
| 101 | + ] { |
| 102 | + $num * $num |
| 103 | + } |
| 104 | + ``` |
| 105 | +- **Supported Types**: `int`, `string`, `bool`, `float`, `path`, `glob`, `record`, `table`, `list`, `closure`, `nothing`, `any`. |
| 106 | +- **Input/Output Signatures**: Specify stream types using `->` when writing custom pipe filters: |
| 107 | + ```nu |
| 108 | + # Filters a list of strings |
| 109 | + def filter-strings []: list<string> -> list<string> { |
| 110 | + where ($it | str length) > 5 |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +### 3. Parameters, Flags, & Defaults |
| 115 | +- **Optional parameters**: Append `?` to the parameter name (e.g., `path?: path`). |
| 116 | +- **Default values**: Provide defaults using `=` (e.g., `port: int = 8080`). |
| 117 | +- **Flags (switches)**: Prefix flags with `--`. Add a single-character short-form using a pipe `|` if useful. |
| 118 | + - Optional flag: `--verbose` (resolves to boolean `true`/`false`). |
| 119 | + - Typed flag: `--timeout: duration` (expects value when flag is used). |
| 120 | + ```nu |
| 121 | + # Starts a spacecraft telemetry connection |
| 122 | + def connect [ |
| 123 | + address: string |
| 124 | + --timeout: duration = 30s # Timeout limit |
| 125 | + --secure (-s) # Enable TLS |
| 126 | + ] { ... } |
| 127 | + ``` |
| 128 | +- **Wrappers**: Use `def --wrapped` to wrap external commands, capturing excess arguments in a `...rest` parameter. |
| 129 | + ```nu |
| 130 | + # Wrapped git execution |
| 131 | + def --wrapped git-run [...rest: string] { |
| 132 | + ^git ...$rest |
| 133 | + } |
| 134 | + ``` |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Environment Management |
| 139 | + |
| 140 | +Environment variables in Nushell are lexically scoped. |
| 141 | + |
| 142 | +### 1. Modifying `$env` |
| 143 | +- **Direct Assignment**: Use `$env.VAR_NAME = value`. Do **not** use the deprecated `let-env` command. |
| 144 | +- **Extending PATH**: Always use `prepend` or `append` to modify path lists. |
| 145 | + - **Correct**: `$env.PATH = ($env.PATH | prepend "/opt/bin")` |
| 146 | +- **Temporary Scope**: Use `with-env` to restrict environment variables to a single block. |
| 147 | + ```nu |
| 148 | + with-env { DATABASE_URL: "sqlite://space.db" } { |
| 149 | + ^space-migrations run |
| 150 | + } |
| 151 | + ``` |
| 152 | + |
| 153 | +### 2. Module Environment Exports |
| 154 | +- **export-env**: Use `export-env` blocks inside modules to expose environment modifications to importing shells. |
| 155 | + ```nu |
| 156 | + # env-manager.nu |
| 157 | + export-env { |
| 158 | + $env.MISSION_STATUS = "active" |
| 159 | + } |
| 160 | + ``` |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## Pipelines & Redirection |
| 165 | + |
| 166 | +### 1. External Commands (`^`) |
| 167 | +- **Prefix Guard**: Always prefix external commands with `^` if a Nushell built-in exists with the same name, or to explicitly indicate an external binary. |
| 168 | + - **Correct**: `^ls -la` (system ls) vs `ls` (Nushell table generator). |
| 169 | + |
| 170 | +### 2. Redirection Operators |
| 171 | +Use explicit operators instead of POSIX numbers (`2>&1`). Redirections work primarily with external commands: |
| 172 | +- `o>` (or `out>`): Redirect standard output to file. |
| 173 | +- `e>` (or `err>`): Redirect standard error to file. |
| 174 | +- `o+e>` (or `out+err>`): Redirect both stdout and stderr to the same file. |
| 175 | +- `e>|`: Pipe standard error into the next command in the pipeline. |
| 176 | +- `o+e>|`: Pipe both stdout and stderr into the next command. |
| 177 | + |
| 178 | +### 3. Capturing Outputs (`complete`) |
| 179 | +- **Capture Record**: Use the `complete` command to collect stdout, stderr, and the numeric exit code of an external command. |
| 180 | + ```nu |
| 181 | + let run = (do { ^cargo build } | complete) |
| 182 | + if $run.exit_code != 0 { |
| 183 | + error make {msg: $"Build failed with error: ($run.stderr)"} |
| 184 | + } |
| 185 | + ``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## Control Flow & Error Handling |
| 190 | + |
| 191 | +### 1. Expressions vs. Statements |
| 192 | +- **If Expressions**: In Nushell, `if` is an expression. Both `if` and `else` branches must return compatible types. |
| 193 | + - **Correct**: `let mode = (if $debug { "verbose" } else { "quiet" })` |
| 194 | +- **Match Patterns**: Use `match` for complex branching and record deconstruction: |
| 195 | + ```nu |
| 196 | + match $status { |
| 197 | + { state: "nominal", count: $c } => $"Status OK with ($c) telemetry entries" |
| 198 | + { state: "warning" } => "Caution: check sensors" |
| 199 | + _ => "Critical failure" |
| 200 | + } |
| 201 | + ``` |
| 202 | + |
| 203 | +### 2. Error Trapping (`try/catch`) |
| 204 | +- **Explicit catch**: Always use `try { ... } catch { |err| ... }` when invoking external calls or dangerous code that could crash. |
| 205 | + ```nu |
| 206 | + try { |
| 207 | + http get "https://invalid-telemetry.space/api" |
| 208 | + } catch { |err| |
| 209 | + print $"Failed to fetch data: ($err.msg)" |
| 210 | + } |
| 211 | + ``` |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +## Anti-Patterns to Avoid |
| 216 | + |
| 217 | +- ❌ **Using `echo` to log/print**: `echo` returns a value, which can corrupt the output of commands or subexpressions. Use `print` to log info to the terminal instead. |
| 218 | +- ❌ **POSIX redirection syntax**: Writing `cmd > out.txt 2>&1`. Use `cmd o+e> out.txt`. |
| 219 | +- ❌ **POSIX substitution syntax**: Writing `$(cmd)`. Use bare parentheses `(cmd)`. |
| 220 | +- ❌ **Bash chain execution**: Using `cmd1 && cmd2`. In Nushell, write `cmd1; if ($env.LAST_EXIT_CODE == 0) { cmd2 }` or use separate lines. |
| 221 | +- ❌ **No spaces around operators**: Writing `$a+$b` or `$x=$y`. Always put spaces: `$a + $b` and `$x = $y`. |
| 222 | +- ❌ **Parsing table output with grep/awk**: Piping `ls` output to external grep. Instead, use native filters: `ls | where name =~ "test"`. |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +## Pre-Commit / Code Audit Checklist |
| 227 | + |
| 228 | +- [ ] All pipes (`|`), equals (`=`), and math/comparison operators have exactly one space on both sides. |
| 229 | +- [ ] No commas are used in lists `[1 2 3]` or records `{a: 1 b: 2}`. |
| 230 | +- [ ] Block and closure curly braces have a single space inside: `{ foo }` and `{ |x| foo }`. |
| 231 | +- [ ] No deprecated `let-env` is used; all environment variables are set with `$env.VAR = value`. |
| 232 | +- [ ] External command calls that override/shadow built-ins (or might be ambiguous) are prefixed with `^`. |
| 233 | +- [ ] `echo` is not used for terminal log printing; `print` is used instead. |
| 234 | +- [ ] Type annotations are added to all custom command parameters and custom commands that filter streams. |
| 235 | +- [ ] Closures are checked to ensure they do not capture mutable `mut` variables. |
| 236 | +- [ ] Checked for POSIX commands/substitutions like `$(...)` or `&&` and converted to Nushell equivalents. |
| 237 | + |
| 238 | +When the user asks to write, edit, or audit Nushell code, apply these guidelines immediately. Present clear feedback citing these specific rules if violations are detected. |
0 commit comments