feat: home manager vs nixos module

This commit is contained in:
Ryan Yin 2023-10-12 23:41:57 +08:00
parent 653e2c9203
commit dead7139e1
2 changed files with 88 additions and 7 deletions

View File

@ -232,8 +232,48 @@ On the other hand, everything installed through Home Manager is specific to the
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.
- NixOS modules: Install core system components and other software packages/configurations required by all users.
- 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 globally on the system, 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.
## How to use packages installed by Home Manager with privileged access?
The first thing that comes to mind is to switch to `root`, but then any packages installed by the current user through `home.nix` will be unavailable.
let's take `kubectl` as an example(it's pre-installed via `home.nix`):
```sh
# 1. kubectl is available
kubectl | head
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
......
# 2. switch user to `root`
sudo su
# 3. kubectl is no longer available
> kubectl
Error: nu::shell::external_command
× External command failed
╭─[entry #1:1:1]
1 │ kubectl
· ───┬───
· ╰── executable was not found
╰────
help: No such file or directory (os error 2)
/home/ryan/nix-config> exit
```
But it's possible to run those packages with privileged access without switching to `root`, by using `sudo`, we temporarily grant the current user privileged access to system resources:
```sh
sudo kubectl
kubectl controls the Kubernetes cluster manager.
...
```

View File

@ -236,7 +236,48 @@ nix flake new example -t github:nix-community/home-manager#nixos
根据这种特性, 一般的推荐用法是:
- NixOS Module: 安装系统的核心组件, 以及几乎所有用户(包含 root 用户)都需要用到的其他软件包.
- 比如说如果你希望某个软件包能在你切换到 root 用户时仍能正常使用, 或者使某个配置能在 root 用户下也能生效, 那就得用 NixOS Module
来安装它.
- Home Manager: 其他所有的配置与软件, 都建议用 Home Manager 来安装.
- NixOS Module: 安装系统的核心组件, 以及所有用户都需要用到的其他软件包或配置
- 比如说如果你希望某个软件包能在你切换到 root 用户时仍能正常使用, 或者使某个配置能在系统全局生效, 那就得用 NixOS Module 来安装它
- Home Manager: 其他所有的配置与软件, 都建议用 Home Manager 来安装
## 如何以特权身份使用 Home Manager 安装的软件包?
对这个问题,首先想到的一般都是直接切换到 `root` 用户, 可切换用户后,当前用户通过 `home.nix` 安装的软件包都将不可用。
让我们以 `kubectl` 为例(已通过 `home.nix` 预先安装好),来演示一下:
```sh
# 1. kubectl 当前可用
kubectl | head
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
......
# 2. 切换到 `root` 用户
sudo su
# 3. kubectl 不再可用,报错找不到它
> kubectl
Error: nu::shell::external_command
× External command failed
╭─[entry #1:1:1]
1 │ kubectl
· ───┬───
· ╰── executable was not found
╰────
help: No such file or directory (os error 2)
/home/ryan/nix-config> exit
```
但是,我们可以通过 `sudo` 命令,临时授予当前用户访问系统资源的特权,从而在不切换到 `root` 用户的情况下,使用 `home.nix` 安装的软件包:
```sh
sudo kubectl
kubectl controls the Kubernetes cluster manager.
...
```