Skip to content

feat: add Nix language support#330

Open
uxtechie wants to merge 3 commits into
colbymchenry:mainfrom
uxtechie:add-nix-support
Open

feat: add Nix language support#330
uxtechie wants to merge 3 commits into
colbymchenry:mainfrom
uxtechie:add-nix-support

Conversation

@uxtechie
Copy link
Copy Markdown

@uxtechie uxtechie commented May 22, 2026

Summary

Add full Nix (.nix) language support to CodeGraph, including symbol extraction, import resolution, and export detection.

Closes #332

Related: #324 — While that issue requests distributing CodeGraph as a Nix flake (packaging), this PR adds support for parsing and indexing Nix source files as a new language in the extraction engine.

What this PR does

Symbol Extraction (src/extraction/languages/nix.ts)

  • Functions: Extracts named function bindings with parameter signatures, including:
    • Simple parameters: y = arg: arg + 1; → signature (arg)
    • Destructured parameters: z = { name }: ... → signature { name }
    • Curried functions: f = a: b: c: body → signature a : b : c
    • Destructuring with @ alias: { x, y } @ args and args @ { x, y } patterns
  • Variables: Extracts non-function bindings as variable nodes
  • Inherit declarations: Processes inherit and inherit (expr) as variable nodes
  • Call references: Extracts function calls from apply_expression, distinguishing import expressions (emitted as imports references) from regular calls
  • Export detection: Implements isExportedNode() helper that traverses AST ancestors to determine if a symbol is in the top-level attribute set (exported) vs. inside a let binding or nested attribute set (private)

Import Resolution

  • src/resolution/import-resolver.ts: Registers .nix and /default.nix extensions in EXTENSION_RESOLUTION
  • src/resolution/index.ts: Adds file-level import resolution for imports-kind references — resolves directly to file: node IDs instead of symbol name matching. Also adjusts the pre-filter to avoid discarding valid import references

Supporting Files

  • src/extraction/grammars.ts: Registers tree-sitter-nix grammar (vendored WASM, loaded from src/extraction/wasm/ like Pascal, Scala, Lua, and Luau)
  • src/extraction/languages/index.ts: Registers Nix extractor
  • src/types.ts: Adds nix to the Language type
  • src/extraction/wasm/tree-sitter-nix.wasm: Bundled WASM parser (no new runtime or dev dependencies added — the grammar was compiled offline and vendored directly)

Test Coverage

Extraction tests (__tests__/extraction.test.ts)

  • Basic variable and function extraction
  • inherit and inherit (expr) handling
  • Import expression extraction (relative paths and builtins.import)
  • Function call reference extraction
  • Destructured and curried function signatures
  • Export/scope detection (top-level exported vs. let-local vs. nested private)

Resolution tests (__tests__/resolution.test.ts)

  • Relative Nix import path resolution to file nodes (import ../core/ports.nix)

Verification

  • npm run build compiles cleanly with no type errors
  • All 745 tests pass (29 test files), including all new Nix-specific tests
  • No regressions in any other language extractor

Dependencies

No new runtime or dev dependencies. The tree-sitter-nix WASM grammar is vendored in src/extraction/wasm/ (same approach used for Pascal, Scala, Lua, and Luau). The package.json dependencies and devDependencies sections are unchanged from main.

Files Changed

File Change
src/extraction/languages/nix.ts New — Full Nix extractor
src/extraction/wasm/tree-sitter-nix.wasm New — Vendored WASM grammar
src/extraction/grammars.ts Register nix grammar
src/extraction/languages/index.ts Register nix extractor
src/types.ts Add nix to Language type
src/resolution/import-resolver.ts Add .nix extensions
src/resolution/index.ts File-level import resolution
__tests__/extraction.test.ts Nix extraction test suite
__tests__/resolution.test.ts Nix import resolution test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add Nix language support (.nix)

1 participant