mirror of
https://github.com/ryan4yin/nixos-and-flakes-book
synced 2024-11-22 20:23:10 +00:00
feat: dev env
This commit is contained in:
parent
a59b8f63a1
commit
3ae3f70da4
4 changed files with 40 additions and 19 deletions
|
@ -1,5 +1,20 @@
|
||||||
# Dev Environments
|
# Dev Environments
|
||||||
|
|
||||||
|
On NixOS, in the global environment (home-manager), you can install only some general
|
||||||
|
development tools and SDKs, such as `git`, `vim`, `emacs`, `tmux`, `zsh`, etc. For the
|
||||||
|
dependencies of the project itself, it is best to have a separate `flake.nix` for each
|
||||||
|
project to manage their respective development environments.
|
||||||
|
|
||||||
|
For simplicity, you might also consider creating some general `flake.nix` templates for
|
||||||
|
common languages in advance, which can be copied and modified as needed.
|
||||||
|
|
||||||
|
Various plugins for editors like `neovim` will also have their own dependencies. These
|
||||||
|
dependencies can be considered to be added to the IDE's own environment through parameters
|
||||||
|
like `programs.neovim.extraPackages` in home-manager, ensuring that the IDE itself can run
|
||||||
|
properly without polluting the global environment.
|
||||||
|
|
||||||
|
## Templates for Development Environments
|
||||||
|
|
||||||
We have learned how to build development environments, but it's a bit tedious to write
|
We have learned how to build development environments, but it's a bit tedious to write
|
||||||
`flake.nix` for each project.
|
`flake.nix` for each project.
|
||||||
|
|
||||||
|
@ -7,8 +22,8 @@ Luckily, some people in the community have done this for us. The following repos
|
||||||
contains development environment templates for most programming languages. Just copy and
|
contains development environment templates for most programming languages. Just copy and
|
||||||
paste them:
|
paste them:
|
||||||
|
|
||||||
- [dev-templates](https://github.com/the-nix-way/dev-templates)
|
|
||||||
- [MordragT/nix-templates](https://github.com/MordragT/nix-templates)
|
- [MordragT/nix-templates](https://github.com/MordragT/nix-templates)
|
||||||
|
- [the-nix-way/dev-templates](https://github.com/the-nix-way/dev-templates)
|
||||||
|
|
||||||
If you think the structure of `flake.nix` is still too complicated and want a simpler way,
|
If you think the structure of `flake.nix` is still too complicated and want a simpler way,
|
||||||
you can consider using the following project, which encapsulates Nix more thoroughly and
|
you can consider using the following project, which encapsulates Nix more thoroughly and
|
||||||
|
@ -81,7 +96,7 @@ this:
|
||||||
> is located in the `/nix/store` directory, and these modification commands can only be
|
> is located in the `/nix/store` directory, and these modification commands can only be
|
||||||
> executed during the Nix build phase.
|
> executed during the Nix build phase.
|
||||||
|
|
||||||
- [DavHau/mach-nix](https://github.com/DavHau/mach-nix) (currently unmaintained)
|
- [python venv demo](https://github.com/MordragT/nix-templates/blob/master/python-venv/flake.nix)
|
||||||
- [poetry2nix](https://github.com/nix-community/poetry2nix)
|
- [poetry2nix](https://github.com/nix-community/poetry2nix)
|
||||||
|
|
||||||
The advantage of these tools is that they utilize the lock mechanism of Nix Flakes to
|
The advantage of these tools is that they utilize the lock mechanism of Nix Flakes to
|
||||||
|
|
|
@ -4,14 +4,6 @@ NixOS's reproducibility makes it ideal for building development environments. Ho
|
||||||
you're used to other distros, you may encounter problems because NixOS has its own logic.
|
you're used to other distros, you may encounter problems because NixOS has its own logic.
|
||||||
We'll briefly explain this below.
|
We'll briefly explain this below.
|
||||||
|
|
||||||
On NixOS, it's recommended to only install common tools in the global environment, such as
|
|
||||||
`git`, `vim`, `emacs`, `tmux`, `zsh`, etc. The development environment of each language
|
|
||||||
should be an independent environment for each project.
|
|
||||||
|
|
||||||
You should NOT install the development environment of each language in the global
|
|
||||||
environment. The project environment should be completely isolated from each other and
|
|
||||||
will not affect each other.
|
|
||||||
|
|
||||||
In the following sections, we'll introduce how the development environment works in NixOS.
|
In the following sections, we'll introduce how the development environment works in NixOS.
|
||||||
|
|
||||||
## Creating a Custom Shell Environment with `nix shell`
|
## Creating a Custom Shell Environment with `nix shell`
|
||||||
|
|
|
@ -1,13 +1,26 @@
|
||||||
# Dev Environments
|
# Dev Environments
|
||||||
|
|
||||||
|
在 NixOS 上,全局环境中(home-manager)可以只安装一些通用的开发工具与 SDK,比如
|
||||||
|
`git`、`vim`、`emacs`、`tmux`、`zsh` 等等。对于项目本身的依赖,最好是每个项目都有一个独立
|
||||||
|
的 `flake.nix` 用于管理各自的开发环境。
|
||||||
|
|
||||||
|
为了简便,你也可以考虑提前为常用语言创建一些通用的 `flake.nix` 模板,在需要的时候复制模板
|
||||||
|
改一改就能用了。
|
||||||
|
|
||||||
|
`neovim` 等编辑器的各种插件本身也会有各种依赖,这些依赖可以考虑通过 home-manager 的
|
||||||
|
`programs.neovim.extraPackages` 等参数来将其加入到 IDE 本身的环境中,这样就能保证 IDE 本身
|
||||||
|
能正常运行,又不会污染全局环境。
|
||||||
|
|
||||||
|
## 开发环境的配置模板
|
||||||
|
|
||||||
前面我们已经学习了构建开发环境的实现原理,但是每次都要自己写一堆重复性较高的 `flake.nix`,
|
前面我们已经学习了构建开发环境的实现原理,但是每次都要自己写一堆重复性较高的 `flake.nix`,
|
||||||
略显繁琐。
|
略显繁琐。
|
||||||
|
|
||||||
幸运的是,社区已经有人为我们做好了这件事,如下这个仓库中包含了绝大多数编程语言的开发环境模
|
幸运的是,社区已经有人为我们做好了这件事,如下这个仓库中包含了绝大多数编程语言的开发环境模
|
||||||
板,直接复制粘贴下来就能用:
|
板,直接复制粘贴下来就能用:
|
||||||
|
|
||||||
- [the-nix-way/dev-templates](https://github.com/the-nix-way/dev-templates)
|
|
||||||
- [MordragT/nix-templates](https://github.com/MordragT/nix-templates)
|
- [MordragT/nix-templates](https://github.com/MordragT/nix-templates)
|
||||||
|
- [the-nix-way/dev-templates](https://github.com/the-nix-way/dev-templates)
|
||||||
|
|
||||||
如果你觉得 `flake.nix` 的结构还是太复杂了,希望能有更简单的方法,也可以考虑使用下面这个项
|
如果你觉得 `flake.nix` 的结构还是太复杂了,希望能有更简单的方法,也可以考虑使用下面这个项
|
||||||
目,它对 Nix 做了更彻底的封装,对用户提供了更简单的定义:
|
目,它对 Nix 做了更彻底的封装,对用户提供了更简单的定义:
|
||||||
|
@ -68,7 +81,7 @@ source ./env/bin/activate
|
||||||
> `flake.nix` 来安装 Python 依赖!因为数据还是在 `/nix/store` 中,这类修改命令必须在 Nix的
|
> `flake.nix` 来安装 Python 依赖!因为数据还是在 `/nix/store` 中,这类修改命令必须在 Nix的
|
||||||
> 构建阶段才能执行...
|
> 构建阶段才能执行...
|
||||||
|
|
||||||
- [DavHau/mach-nix](https://github.com/DavHau/mach-nix)
|
- [python venv demo](https://github.com/MordragT/nix-templates/blob/master/python-venv/flake.nix)
|
||||||
- [poetry2nix](https://github.com/nix-community/poetry2nix)
|
- [poetry2nix](https://github.com/nix-community/poetry2nix)
|
||||||
|
|
||||||
这俩工具的好处是,能利用上 Nix Flakes 的锁机制来提升可复现能力,缺点是多了一层封装,底层变
|
这俩工具的好处是,能利用上 Nix Flakes 的锁机制来提升可复现能力,缺点是多了一层封装,底层变
|
||||||
|
@ -76,3 +89,11 @@ source ./env/bin/activate
|
||||||
|
|
||||||
最后,在一些更复杂的项目上,上述两种方案可能都行不通,这时候最佳的解决方案,就是改用容器
|
最后,在一些更复杂的项目上,上述两种方案可能都行不通,这时候最佳的解决方案,就是改用容器
|
||||||
了,比如 Docker、Podman 等,容器的限制没 Nix 这么严格,能提供最佳的兼容性。
|
了,比如 Docker、Podman 等,容器的限制没 Nix 这么严格,能提供最佳的兼容性。
|
||||||
|
|
||||||
|
## Go 开发环境
|
||||||
|
|
||||||
|
Go 是静态链接,天然就少了很多麻烦,基本能在 NixOS 上无痛使用,不需要额外处理。
|
||||||
|
|
||||||
|
## 其他开发环境
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
|
@ -4,13 +4,6 @@
|
||||||
的环境搭建经验用在 NixOS 上,可能会遇到许多问题,因为 NixOS 有自己的一套逻辑在,下面我们先
|
的环境搭建经验用在 NixOS 上,可能会遇到许多问题,因为 NixOS 有自己的一套逻辑在,下面我们先
|
||||||
对此稍作说明。
|
对此稍作说明。
|
||||||
|
|
||||||
在 NixOS 上,全局环境中只建议安装一些通用的工具,比如 `git`、`vim`、`emacs`、`tmux`、`zsh`
|
|
||||||
等等,而各语言的开发环境,最好是每个项目都有一个独立的环境。为了简便,你也可以考虑提前为常
|
|
||||||
用语言创建一些通用的开发环境,在需要时切换进去。
|
|
||||||
|
|
||||||
总而言之,NixOS 上的开发环境不要一股脑都装在全局,弄成一个个相互隔离、不会互相影响的项目环
|
|
||||||
境会优雅很多。
|
|
||||||
|
|
||||||
在本章中我们先学习一下 Nix Flakes 开发环境的实现原理,后面的章节再按使用场景介绍一些更具体
|
在本章中我们先学习一下 Nix Flakes 开发环境的实现原理,后面的章节再按使用场景介绍一些更具体
|
||||||
的内容。
|
的内容。
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue