Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake ./tools/nix-dev-shell
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ compile_commands.json
/logs/
*.log

# Nix dev files
/.envrc
/.direnv/
/flake.nix
flake.lock
/default.nix

# initram
*.cpio
*.cpio.xz
*.cpio*
.direnv/
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_build/
result
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"strikethrough",
"substitution",
"tasklist",
"linkify"
]

# sphinx-multiversion 指定哪个分支为 lastest 版本
Expand Down
61 changes: 61 additions & 0 deletions docs/dev-skills/nix-skills.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Nix 技巧

## 同一终端内避免反复指定 `nix run .#start-x86_64`

需要注意的是,仅推荐重复使用 qemu 启动脚本 `dragonos-run`,因为 rootfs 在 `nix shell` eval 完毕后生成的仅仅是最终 `rootfs.tar` 拷贝到磁盘文件的过程,而 `rootfs.tar` 需要重新调用 nix build/shell/run 命令来生成。你的所有对 nix 的修改都需要通过重新执行 nix 命令来进行 eval 与构建,因此 `nix shell .#rootfs-x86_64` 意义不大。

```shell
❯ nix shell .#start-x86_64 .#rootfs-x86_64
❯ which dragonos-rootfs
/nix/store/rpsb6f76hxzcfblghfgch2jl8413w6m4-dragonos-rootfs/bin/dragonos-rootfs
❯ which dragonos-run
/nix/store/1sdnhsjihj2h3mkdx7x0v8zy9ldfijml-dragonos-run/bin/dragonos-run
❯ dragonos-run
# DragonOS QEMU Start
❯ make kernel # 重新构建内核
❯ dragonos-run # 不涉及启动脚本和rootfs的更改,直接qemu启动
```

## 查看可用构建产物

```shell
❯ nix flake show
warning: Git tree '/workspace' is dirty
git+file:///workspace
├───apps
│ └───x86_64-linux
│ ├───rootfs-riscv64: app: 构建 riscv64 rootfs 镜像
│ ├───rootfs-x86_64: app: 构建 x86_64 rootfs 镜像
│ ├───start-riscv64: app: 以 riscv64 启动DragonOS
│ └───start-x86_64: app: 以 x86_64 启动DragonOS
└───packages
└───x86_64-linux
├───rootfs-riscv64: package 'build-rootfs-image'
├───rootfs-x86_64: package 'build-rootfs-image'
├───start-riscv64: package 'run-dragonos'
└───start-x86_64: package 'run-dragonos'

user/apps/about ❯ nix flake show
git+file:///workspace?dir=user/apps/about
└───packages
├───aarch64-darwin
│ └───default omitted (use '--all-systems' to show)
├───aarch64-linux
│ └───default omitted (use '--all-systems' to show)
├───x86_64-darwin
│ └───default omitted (use '--all-systems' to show)
└───x86_64-linux
└───default: package 'about-static-x86_64-unknown-linux-musl-0.1.0'

tests/syscall/gvisor ❯ nix flake show
git+file:///workspace?dir=user/apps/tests/syscall/gvisor
└───packages
└───x86_64-linux
└───default: package 'gvisor-tests'
```

## 回收 nix 构建历史缓存

```shell
❯ nix store gc
```
32 changes: 32 additions & 0 deletions docs/dev-skills/write-docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 贡献文档

## 使用 nix

### 实时构建预览文档

sphinx-autobuild 作为默认 nix run 目标,写文档只需要

```shell
cd docs
nix run
```

然后访问 8000 端口即可。

### 构建文档为 drv (sphinx-build)

```shell
cd docs
nix build
```

### 预览构建好的文档 (python http-server)

```shell
cd docs
nix run .#release
```

## 使用 pip Makefile 构建文档

TODO
61 changes: 61 additions & 0 deletions docs/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions docs/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
description = "Reproducible Sphinx Documentation Build";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;

# ----------------------------------------------------------------
# 1. 定义精确的 Python 环境
# ----------------------------------------------------------------
# 这里显式列出依赖,确保无论在哪台机器,Sphinx 版本一致
pythonEnv = pkgs.python3.withPackages (ps: with ps; [
sphinx
sphinx-multiversion
sphinx-autobuild
# 在这里添加你的 theme 或 extension,例如:
sphinx-rtd-theme
myst-parser
sphinxcontrib-mermaid
linkify-it-py
]);

# ----------------------------------------------------------------
# 2. 从 Flake 输入中提取 Git 信息 (Pure 方式)
# ----------------------------------------------------------------
# Nix 在求值时知道当前的 revision,不需要在 build 时运行 git 命令
gitHash = if (self ? rev) then self.rev else "dirty-dev";
isDirty = if (self ? rev) then "0" else "1";

buildDir = "./_build";
port = 8000;

in
{
# ----------------------------------------------------------------
# Packages: 纯净构建 (nix build)
# ----------------------------------------------------------------
# 这种方式构建的是"当前代码的快照",完全不依赖 .git 目录
packages.default = pkgs.stdenv.mkDerivation {
name = "sphinx-docs";
src = ./.;

buildInputs = [ pythonEnv ];

# 将 Nix 提取的 Git 信息注入环境变量
# 这完全替代了 Makefile 中 $(shell git rev-parse) 的逻辑
env = {
LANGUAGE = "zh_CN";
CURRENT_GIT_COMMIT_HASH = gitHash;
CURRENT_GIT_COMMIT_DIRTY = isDirty;
SPHINXBUILD = "sphinx-build";
};

# 直接定义构建逻辑,不再依赖 Makefile
buildPhase = ''
echo "Building documentation for commit: $CURRENT_GIT_COMMIT_HASH"

# 使用 -W 将警告视为错误,确保构建质量
sphinx-build -M html . ${buildDir} \
-D language=$LANGUAGE \
-w _build/warnings.log
'';
Comment on lines +65 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不应该这样,因为ai翻译的文档很可能会有warning,比如reST的标题下方的===的长度不够宽之类的。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok这个我调一下。实际上我也不是很清楚现在DragonOS文档的 doc build 和 multiversion doc build 在部署上是怎么实现的,后续需要再补充一下。

此外英文那里 Device 标题被混元翻译为了中文 “设备”,xs


installPhase = ''
mkdir -p $out
cp -r ${buildDir}/* $out/
'';
};

# ----------------------------------------------------------------
# Apps: 快速运行工具 (nix run)
# ----------------------------------------------------------------
# 提供一个脚本来预览构建结果
apps.release = flake-utils.lib.mkApp {
drv = let targetDir = self.packages.${system}.default;
in pkgs.writeShellApplication {
name = "preview-docs";
runtimeInputs = [ pythonEnv ];
text = ''
echo "Open at http://localhost:${toString port}/index.html"
python3 -m http.server --directory "${targetDir}/html" ${toString port}
'';
};
};
app.develop = flake-utils.lib.mkApp {
drv = pkgs.writeShellApplication {
name = "sphinx-autobuild";
runtimeInputs = [ pythonEnv ];
text = ''
sphinx-autobuild . ${buildDir} --host 0.0.0.0 --port ${toString port}
'';
};
};
# 设置默认 run 行为
apps.default = self.app.${system}.develop;

# ----------------------------------------------------------------
# DevShell: 开发环境 (nix develop)
# ----------------------------------------------------------------
# 用于开发和需要访问 .git 的操作(如 sphinx-multiversion)
devShells.default = pkgs.mkShell {
packages = [ pythonEnv pkgs.git pkgs.gnumake ];

shellHook = ''
export LANGUAGE="zh_CN"
export SPHINXBUILD="sphinx-build"

# 在开发环境中,我们可以动态获取 git 状态
export CURRENT_GIT_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")

echo "🚀 Sphinx Dev Environment Loaded"
echo "-----------------------------------"
echo "Run 'make html' for standard build"
echo "Run 'make html-multiversion' for versioned build (Requires .git)"
echo "Run 'nix build' for clean production build"
'';
};
}
);
}
14 changes: 12 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

introduction/index
introduction/build_system
introduction/develop_nix
introduction/devcontainer
introduction/mirrors

.. toctree::
Expand Down Expand Up @@ -55,13 +57,21 @@
.. toctree::
:maxdepth: 1
:caption: 应用层

userland/appdev/index
userland/rootfs/index

.. toctree::
:maxdepth: 1
:caption: 开发指南

dev-skills/nix-skills
dev-skills/write-docs

.. toctree::
:maxdepth: 1
:caption: Q&A

questions/index


Expand Down
31 changes: 31 additions & 0 deletions docs/introduction/devcontainer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 使用 devcontainer 开发 DragonOS

本教程以 VSCode 为例,需要装有 Docker 的 Linux。

## 克隆仓库

```shell
git clone https://github.com/DragonOS-Community/DragonOS.git
code DragonOS
```

## 进入 devcontainer 环境

在 VSCode 右下角会有弹窗,选择 `Reopen in Container`。如果不可见,请根据下列步骤来进入:
- 下载 devcontainer 插件
- `ctrl+shift+p` 打开 VSCode 命令面板
- 输入 `devcontainer` 字样,会有 `Reopen in Container` 的选项,点击即会构建 devcontainer 环境

构建可能需要一些时间,尤其 msr 的插件在网络环境不好的情况下容易安装失败。

## 构建 DragonOS!

直接输入

```shell
make run-nographic
```

等待构建,最后会自动进入 DragonOS qemu 环境。

需要退出qemu环境,请输入 `ctrl+a` 然后按 `x`。
Loading
Loading