z1-0/nix-ast

GitHub: z1-0/nix-ast

一个 Nix 库,在 Nix 语言内部暴露完整的 AST 解析、遍历、转换、渲染与求值能力,底层借助 Haskell 的 hnix 实现。

Stars: 51 | Forks: 0

# nix-ast `nix-ast` 直接在 Nix 本身中暴露了完整的 Nix AST API(通过 IFD,即 Import From Derivation),并在内部使用了 hnix 的 Haskell 解析器和求值器。你可以将任何 Nix 表达式解析为结构化的 AST,使用纯 Nix 函数对其进行遍历和转换,将其重新渲染回源代码,甚至对其进行求值。所有这些都可以在你的 Nix 表达式中完成。 ## 功能 | 功能 | 描述 | | ----------------------------- | -------------------------------------------------------------------------------------- | | **Parse** | 将任何 Nix 表达式(文件、字符串或 stdin)解析为结构化、带类型的 AST | | **Render** | 将 AST 转换回格式化、可导入的 Nix 代码 | | **Eval** | 使用 hnix 的求值器对 AST 进行求值,并以 JSON 值的形式获取结果 | | **toAST** | 将原生的 Nix 值(整数、字符串、列表、attrset)转换为 AST 节点(纯函数,无 IFD) | | **fromAST** | 将 AST 转换回原生的 Nix 值,即 `toAST` 的逆操作(纯函数,无 IFD) | | **Pattern Matching** | 基于 tag 的类型安全分派,使用 `match ast { Tag = handler; _ = fallback; }` | | **Traversal** | 递归的 `transform`、`rewrite`、`universe`、`children`/`rebuild` 和 `contexts` | | **Type-checked Constructors** | 所有 `mk*` 构建器都会在运行时验证参数类型,并提供描述性的错误信息 | | **CLI Tool** | 用于 shell/管道工作流的 `nix-ast eval` / `nix-ast parse` / `nix-ast render` | ## 快速开始 ### 使用 Nix 库 将 `nix-ast` 添加到你的 flake inputs 中: ``` inputs.nix-ast.url = "github:z1-0/nix-ast"; ``` 该库通过 `inputs.nix-ast.lib` 暴露了三个主要的 API 组: | API | 描述 | | ---------------------------------------- | -------------------------------------------------- | | `lib.parse` `lib.render` `lib.eval` | 基于 IFD:桥接 hnix 的解析器/求值器 | | `lib.toAST` `lib.fromAST` | 纯 Nix:在 Nix 值和 AST 节点之间进行转换 | | `lib.match` `lib.syntax` `lib.traversal` | 在 AST 节点构建完成后对其进行操作 | ``` # 解析 → 转换 → 渲染 asts = lib.parse pkgs [./config.nix]; transformed = lib.traversal.transform (node: ...) (builtins.head asts); outPaths = lib.render pkgs [transformed]; config = import (builtins.head outPaths); # 或者:解析 → 求值(跳过渲染) result = lib.eval pkgs asts; ``` ### 使用 CLI ``` # 从表达式字符串解析 nix-ast parse --expr '{ x = 1; }' > ast.json # 从 stdin 解析文件(每行一个路径) echo ./config.nix | nix-ast parse > ast.json # 将 AST 渲染回 Nix nix-ast render --json '{"tag":"Set","recursive":false,"bindings":[...]}' # 将批次渲染到输出目录 nix-ast render < asts.json --out-dir ./out # 使用 hnix 求值 AST nix-ast eval --json '{"tag":"Constant","contents":{"tag":"Int","contents":42}}' # 管道工作流:解析 → 求值 nix-ast parse --expr '{ x = 1 + 2; }' | nix-ast eval ``` ## 架构 ``` ┌─────────────────────────────────────────────────────────┐ │ Nix (your flake) │ │ │ │ lib.parse lib.render lib.eval │ │ │ │ │ │ │ ▼ ▼ ▼ │ │ ┌──────────────────────────────┐ │ │ │ IFD: nix-ast CLI (Haskell) │ ← hnix parser/eval │ │ └──────────────────────────────┘ │ │ │ │ lib.toAST lib.fromAST (pure Nix, no IFD) │ │ lib.match lib.syntax lib.traversal │ └─────────────────────────────────────────────────────────┘ ``` 该库是分层的:`parse`/`render`/`eval` 通过 IFD derivation 桥接到 Haskell,而 `toAST`/`fromAST`/`match`/`syntax`/`traversal` 则完全在纯 Nix 中对生成的 AST 值进行操作。 ## 安装 ``` # 无需安装直接运行 nix run github:z1-0/nix-ast -- parse --expr '{ x = 1; }' # 或者添加到你的 flake inputs inputs.nix-ast.url = "github:z1-0/nix-ast"; ``` ## 了解更多 - **[API 参考](https://z1-0.github.io/nix-ast/api/)**:parse、render、eval、construct、match 和 traverse AST 节点 - [核心函数](https://z1-0.github.io/nix-ast/api/core-functions.html):`parse`、`render`、`eval`、`toAST`、`fromAST` - [Pattern Matching](https://z1-0.github.io/nix-ast/api/match.html):基于节点类型的 tag 分派 - [Syntax](https://z1-0.github.io/nix-ast/api/syntax.html):运行时验证的构造器和谓词 - [Traversal](https://z1-0.github.io/nix-ast/api/traversal.html):children、rebuild、transform、universe - **[AST 参考](https://z1-0.github.io/nix-ast/ast/)**:包含字段、JSON 表示和示例的每一种节点类型 - [Expr](https://z1-0.github.io/nix-ast/ast/expr/):19 个表达式构造器 - [Atom](https://z1-0.github.io/nix-ast/ast/atom/):原始常量(Bool、Int、Float、Null、Uri) - [Binding](https://z1-0.github.io/nix-ast/ast/binding/):Inherit、NamedVar - [KeyName](https://z1-0.github.io/nix-ast/ast/key-name/):StaticKey、DynamicKey - [Params](https://z1-0.github.io/nix-ast/ast/params/):Param、ParamSet - [String](https://z1-0.github.io/nix-ast/ast/string/):DoubleQuoted、Indented - [Antiquoted](https://z1-0.github.io/nix-ast/ast/antiquoted/):Plain、Antiquoted、EscapedNewline - **[CLI 参考](https://z1-0.github.io/nix-ast/cli.html)**:`nix-ast eval` / `nix-ast parse` / `nix-ast render` ## 许可证 BSD-3-Clause:详情请参阅 [LICENSE](./LICENSE)。
标签:Nix, SOC Prime, 代码分析, 代码转换, 凭证管理, 开发工具, 默认DNS解析器