mirror of
https://github.com/ryan4yin/nixos-and-flakes-book
synced 2024-11-23 12:43:08 +00:00
feat: home manager vs nixos
This commit is contained in:
parent
a94d3f0700
commit
75fce20fde
4 changed files with 42 additions and 4 deletions
|
@ -22,12 +22,15 @@ The following are classic Nix commands and associated concepts that are no longe
|
|||
1. In Flakes, the functionality of `nix-channel` is entirely replaced by the `inputs` section in `flake.nix`.
|
||||
2. `nix-env`: `nix-env` is a core command-line tool for classic Nix used to manage software packages in the user environment.
|
||||
1. It installs packages from the data sources added by `nix-channel`, causing the installed package's version to be influenced by the channel. Packages installed with `nix-env` are not automatically recorded in Nix's declarative configuration and are completely independent of its control, making them challenging to reproduce on other machines. Therefore, it is not recommended to use this tool.
|
||||
2. The corresponding command in Flakes is `nix profile`, which is also not recommended for use.
|
||||
2. The corresponding command in Flakes is `nix profile`. Personally, I don't recommend it either.
|
||||
3. `nix-shell`: `nix-shell` creates a temporary shell environment, which is useful for development and testing.
|
||||
1. In Flakes, this tool is divided into three sub-commands: `nix develop`, `nix shell`, and `nix run`. We will discuss these three commands in detail in the "[Development](../development/intro.md)" chapter.
|
||||
4. `nix-build`: `nix-build` builds Nix packages and places the build results in `/nix/store`, but it does not record them in Nix's declarative configuration.
|
||||
1. In Flakes, `nix-build` is replaced by `nix build`.
|
||||
5. ...
|
||||
5. `nix-collect-garbage`: Garbage collection command used to clean up unused Store Objects in `/nix/store`.
|
||||
1. In Nix Flakes, the corresponding command is `nix store gc --debug`.
|
||||
6. And other less commonly used commands are not listed here.
|
||||
1. You can refer to the detailed command comparison list in [Try to explain nix commands](https://qiita.com/Sumi-Sumi/items/6de9ee7aab10bc0dbead?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en).
|
||||
|
||||
> NOTE: `nix-env -qa` may still be useful in some cases, as it returns all packages installed in the system.
|
||||
|
||||
|
|
|
@ -221,3 +221,19 @@ To find the options we can use in `home.nix`, referring to the following documen
|
|||
- [Home Manager - Appendix A. Configuration Options](https://nix-community.github.io/home-manager/options.html): A list of all options, it is recommended to search for keywords in it.
|
||||
- [Home Manager Option Search](https://mipmip.github.io/home-manager-option-search/) is another option search tool with better UI.
|
||||
- [home-manager](https://github.com/nix-community/home-manager): Some options are not listed in the official documentation, or the documentation is not clear enough, you can directly search and read the corresponding source code in this home-manager repo.
|
||||
|
||||
## Home Manager vs NixOS
|
||||
|
||||
When it comes to managing software packages and configurations, you often have the choice of using either NixOS modules (`configuration.nix`) or Home Manager (`home.nix`). This poses a dilemma: **What are the differences between putting packages or configuration files in NixOS modules versus Home Manager configurations, and how should you decide?**
|
||||
|
||||
First, let's understand the differences. Packages and configuration files installed through NixOS modules are global to the entire system. Global configurations are typically stored in `/etc`, and globally installed packages are linked accordingly. Regardless of the user you switch to, you can access and use these packages and configurations.
|
||||
|
||||
On the other hand, everything installed through Home Manager is specific to the corresponding user. Once you switch to another user, those configurations and packages become unavailable.
|
||||
|
||||
Based on these characteristics, here is a general recommended approach:
|
||||
|
||||
- NixOS modules: Install core system components and other software packages required by almost all users, including the root user.
|
||||
- For example, if you want a package to be accessible even when you switch to the root user, or if you want a configuration to take effect for the root user as well, you should install it through a NixOS module.
|
||||
- Home Manager: Use Home Manager to install all other configurations and software specific to individual users.
|
||||
|
||||
In summary, NixOS modules are suitable for installing system-wide components and packages that need to be accessible to multiple users, while Home Manager is ideal for managing user-specific configurations and software.
|
||||
|
|
|
@ -26,12 +26,15 @@ Nix 于 2020 年推出了 `nix-command` & `flakes` 两个新特性,它们提
|
|||
1. Nix Flakes 在 `flake.nix` 中通过 `inputs` 声明依赖包的数据源,通过 `flake.lock` 锁定依赖版本,完全取代掉了 `nix-channel` 的功能。
|
||||
2. `nix-env`: 用于管理用户环境的软件包,是传统 Nix 的核心命令行工具。它从 `nix-channel` 定义的数据源中安装软件包,所以安装的软件包版本受 channel 影响。
|
||||
1. 通过 `nix-env` 安装的包不会被自动记录到 Nix 的声明式配置中,是完全脱离掌控的,无法在其他主机上复现,因此不推荐使用。
|
||||
2. 在 Nix Flakes 中对应的命令为 `nix profile`,此命令也同样不推荐使用。
|
||||
2. 在 Nix Flakes 中对应的命令为 `nix profile`,我个人也不太推荐直接使用它.
|
||||
3. `nix-shell`: nix-shell 用于创建一个临时的 shell 环境
|
||||
1. 这玩意儿可能有点复杂了,因此在 Nix Flakes 中它被拆分成了三个子命令 `nix develop`, `nix shell` 以及 `nix run`,我们会在「[构建开发环境](../development/intro.md)」一章详细介绍这三个命令。
|
||||
4. `nix-build`: 用于构建 Nix 包,它会将构建结果放到 `/nix/store` 路径下,但是不会记录到 Nix 的声明式配置中。
|
||||
1. 在 Nix Flakes 中对应的命令为 `nix build`
|
||||
5. ...
|
||||
5. `nix-collect-garbage`: 垃圾回收指令,用于清理 `/nix/store` 中未被使用的 Store Objects.
|
||||
1. 在 Nix Flakes 中对应的命令为 `nix store gc --debug`.
|
||||
6. 以及其他使用地较少的命令,就不一一列出了.
|
||||
1. 详细的命令对比列表可以看看 [Try to explain nix commands](https://qiita-com.translate.goog/Sumi-Sumi/items/6de9ee7aab10bc0dbead?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en)
|
||||
|
||||
> 可能也就 `nix-env -qa` 这个命令偶尔还会有些用了,它返回当前系统下安装的所有软件包。
|
||||
|
||||
|
|
|
@ -224,3 +224,19 @@ nix flake new example -t github:nix-community/home-manager#nixos
|
|||
- [Home Manager - Appendix A. Configuration Options](https://nix-community.github.io/home-manager/options.html): 一份包含了所有配置项的列表,建议在其中关键字搜索。
|
||||
- [Home Manager Option Search](https://mipmip.github.io/home-manager-option-search/): 一个更方便的 option 搜索工具。
|
||||
- [home-manager](https://github.com/nix-community/home-manager): 有些配置项在官方文档中没有列出,或者文档描述不够清晰,可以直接在这份 home-manager 的源码中搜索阅读对应的源码。
|
||||
|
||||
## Home Manager vs NixOS
|
||||
|
||||
有许多的软件包或者软件配置, 既可以使用 NixOS Module 配置(`configuration.nix`),也可以使用 Home Manager 配置(`home.nix`), 这带来一个选择难题: **将软件包或者配置文件写在 NixOS Module 里还是 Homa Manager 配置里面有何区别? 该如何决策?**
|
||||
|
||||
首先看看区别, NixOS Module 中安装的软件包跟配置文件都是整个系统全局的, 全局的配置通常会被存放在 `/etc` 中, 而全局的软件也通常被链接到全局. 不论切换到哪个用户下,
|
||||
都能正常使用该软件或配置.
|
||||
|
||||
而 Home Manager 安装的所有东西, 都仅在对应的用户下可用, 切换到其他用户后这些配置跟软件就都失效了.
|
||||
|
||||
根据这种特性, 一般的推荐用法是:
|
||||
|
||||
- NixOS Module: 安装系统的核心组件, 以及几乎所有用户(包含 root 用户)都需要用到的其他软件包.
|
||||
- 比如说如果你希望某个软件包能在你切换到 root 用户时仍能正常使用, 或者使某个配置能在 root 用户下也能生效, 那就得用 NixOS Module
|
||||
来安装它.
|
||||
- Home Manager: 其他所有的配置与软件, 都建议用 Home Manager 来安装.
|
||||
|
|
Loading…
Reference in a new issue