NixOS 系列 #5：如何在 NixOS 上设置主目录管理器
NixOS
![](/data/attachment/album/202304/06/110641k8v9q1152hhhh114.jpg)


在发表这篇文章之前，我解释了如何为一个单用户系统 [在 NixOS 中安装和删除软件包](/article-15645-1.html)。


但是，如果你正在供多个用户使用，有一个很好的方法来分别满足每个用户的需求。


在本指南中，我将指导你如何在 NixOS 上设置一个<ruby> 主目录管理器 <rp>  （ </rp> <rt>  Home Manager </rt> <rp>  ） </rp></ruby>，以及如何使用它来安装软件包。


如果你是新读者，本系列中讨论的一些资源包括：


* [使用 NixOS 的原因](/article-15606-1.html)
* [在虚拟机上安装 NixOS](/article-15624-1.html)
* [安装 NixOS 后要做的事情](/article-15663-1.html)


### 在 NixOS 上设置主目录管理器


在本指南中，我将指导你通过 2 种方式来设置主目录管理器：


* 独立的主目录管理器（使用单独的配置文件）
* 作为一个 NixOS 模块（在 `configuration.nix` 文件中使用它）


那么，让我们从独立方式开始。


#### 独立安装的主目录管理器


如果你使用的是 NixOS 的稳定频道，你可以使用以下命令来配置主目录管理器：



```
nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.11.tar.gz home-manager

```

在编写本指南时，稳定版是 `22.11`。


而 **如果你在一个不稳定的频道上**，请使用以下命令：



```
nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager

```

无论你使用稳定版还是不稳定版，下面的步骤都是一样的。


一旦完成，更新频道：



```
nix-channel --update

```

最后，使用下面的命令来安装主目录管理器：



```
nix-shell '<home-manager>' -A install

```

?️ 在安装时，可能会出现以下错误：


![安装主目录管理器时出现构建错误](/data/attachment/album/202304/06/110643if01f44x191v499u.png)


重新启动你的系统并再次使用安装命令，它将开始安装。


一旦完成，它将显示独立安装的主目录管理器的位置：


![主目录管理器在NixOS中的位置](/data/attachment/album/202304/06/110644kg7j9zeuivn9bq06.png)


#### 将主目录管理器安装为 NixOS 模块



> 
> ⚠️ 如果你选择将主目录管理器作为 NixOS 模块使用，你将需要 sudo 权限。
> 
> 
> 


如果你在一个稳定的频道上（在写本文的时候，是 `22.11`），你可以使用下面的命令来添加主目录管理器的稳定频道：



```
sudo nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.11.tar.gz home-manager

```

而 **如果你使用的是不稳定通道或主通道**，则使用以下命令：



```
sudo nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager

```

一旦你使用上面的任何一条命令完成了添加频道的工作，就用下面的方法更新频道：



```
sudo nix-channel --update

```

接下来，用以下方法打开 `configuration.nix` 文件：



```
sudo nano /etc/nixos/configuration.nix

```

并在 `imports []` 中添加以下一行：



```
<home-manager/nixos>

```

![将主目录管理器安装为 NixOS 模块](/data/attachment/album/202304/06/110644oczkc2r5ko224zcc.png)


现在，跳到该行的末尾，在 `}` 前添加以下内容：



```
home-manager.users.{username} = { pkgs, ... }: {
  home.packages = [ ] ;
  };

```

![NixOS 配置文件中的主目录管理器模块的格式](/data/attachment/album/202304/06/110644p8s1e9mz9391e9cz.png)


上面这一行是为了方便安装和删除软件包而添加的，我接下来会告诉你。


现在，[保存修改并退出 Nano](https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com) 文本编辑器。


接下来，重建配置，并做一个切换：



```
sudo nixos-rebuild switch

```

但如果你使用的是稳定版，并使用上述命令，就会出现以下错误：



> 
> ?️ 错误： 选项 `home-manager.users.user.home.stateVersion` 被使用但没有定义：
> 
> 
> 


![错误： 选项  已被使用但未被定义。](/data/attachment/album/202304/06/110645o7q7mx0gq200al00.png)


要解决这个问题，你必须在你的主目录管理器块中添加 `home.stateVersion`。


在写这篇文章时，我正在运行 22.11，所以整个主目录管理器块看起来像这样：



```
home-manager.users.{username} = { pkgs, ... }: {
  home.stateVersion = "22.11";  
  home.packages = [ ] ;
  };

```

![如何解决  选项被使用但没有定义。](/data/attachment/album/202304/06/110645m9bm99b74z5eum48.png)


保存修改，按 `Ctrl+O`，按回车键和 `Ctrl+X` 退出 Nano 文本编辑器。


现在，试着重建配置并再次进行切换，应该可以解决问题。


### 如何在 NixOS 上使用主目录管理器安装软件包


现在你已经安装了主目录管理器，如何用它来安装软件包：


#### 使用独立安装的主目录管理器


首先，用下面的方法打开配置文件：



```
nano /home/$USER/.config/nixpkgs/home.nix

```

跳到行末，在 `}` 前添加以下代码块：



```
home.packages = [];

```

现在，你所要做的就是在这两个大括号之间写上软件包的名称。


例如，如果我想安装 `htop`，我将得输入以下内容：



```
home.packages = [pkgs.htop];

```

是的，你通常要在软件包的名称后面加上 `pkgs.`。


但是，如果你想在每次安装新包时不使用 `pkgs.`，可以改变代码块的语法，如图所示：



```
home.packages = with pkgs; [];

```

而现在，你不再需要在每次安装时使用 `pkgs.`：



```
home.packages = with pkgs; [htop];

```

例如，在这里，我想安装 `htop`、`firefox`和 `libreoffice`，所以我的 `home` 块会看起来像这样：


![在 NixOS 上使用主目录管理器安装多个软件包](/data/attachment/album/202304/06/110645d3exfdszmrkzkrym.png)


一旦你完成了添加你喜欢的软件包，保存配置文件并使用以下命令来安装软件包：



```
home-manager switch

```

#### 使用 NixOS 模块方式


首先，用以下命令打开 `configuration.nix` 文件：



```
sudo nano /etc/nixos/configuration.nix

```

在配置部分，我已经添加了主目录管理器块，所以剩下的就是在 `home.packages = [ ];` 里面添加软件包的名称，格式如图所示：



```
home.packages = [ pkgs.package_name ] ;

```


> 
> ?我已经在上节提到软件包名称前你可以使用 `pkgs.` 。
> 
> 
> 


例如，如果我想 [安装 htop](https://itsfoss.com/use-htop/)、Firefox 和 LibreOffice，那么我将添加：



```
pkgs.htop pkgs.firefox pkgs.libreoffice

```

然后我的主目录管理器块会看起来像这样：


![作为 NixOS 模块在主目录管理器中安装多个软件包](/data/attachment/album/202304/06/110645kuczbvmfweevf28c.png)


现在，保存修改并退出文本编辑器。


接下来，重建配置并使用以下命令进行切换：



```
sudo nixos-rebuild switch

```

这是这样！软件包将很快被安装。


### 这就结束了


我认为你应该选择独立安装，因为你不需要使用超级用户的权限。另外，如果你运行一个有多个用户的系统，为不同的用户拥有不同的配置文件是相当方便的。


因此，除非你想用一个文件来实现各种目的，否则我认为没有其他理由使用模块选项。


就这样，我结束了 NixOS 的初学者系列。我希望它能给你一个足够好的平台来熟悉这个独特的 Linux 发行版。


? 你喜欢 NixOS 系列文章吗？对于 NixOS 的初学者，我们还有什么要介绍的吗？请提供你的宝贵意见。




---


via: <https://itsfoss.com/home-manager-nixos/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Set up home-manager on NixOS

Here's how you can easily set up the home-manager to install/remove packages on NixOS.

Before publishing this, I explained how to [install and remove packages in NixOS](https://itsfoss.com/nixos-package-management/) for a single-user system.

But if you are running multiple users, there is an excellent way to cater needs of every user separately.

And in this guide, I will walk you through how you can set up a home manager on NixOS and how it can be used to install packages.

If you are new here, some resources discussed in this series include:

## Setup home-manager on NixOS

In this guide, I will walk you through 2 ways to set up a home manager:

- Standalone home manager (uses separate config file)
- As a nix module (using it inside
`configuration.nix`

file)

So let's start with the standalone option.

### Standalone installation of home-manager

If you are using a stable channel of NixOS, you can use the following command to configure the home manager:

`nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.11.tar.gz home-manager`


While writing this guide, the stable release is `22.11`

.

And **if you are on an unstable channel**, use the following:

`nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager`


The following steps will remain the same whether you use stable or unstable.

Once done, update the channels:

`nix-channel --update`


And finally, use the following command to install the home manager:

`nix-shell '<home-manager>' -A install`


🛠️ While installing, it may throw the following error:

![building error while installing home manager](https://itsfoss.com/content/images/2023/02/building-error-while-installing-home-manager.png)

Reboot your system and use the installation command again, and it will start the installation.

Once done, it will show the location of the standalone installation of the home manager:

![location of home manager in NixOS](https://itsfoss.com/content/images/2023/02/location-of-home-manager-in-NixOS.png)

### Installing home-manager as a NixOS module

If you are on a stable channel (while writing, it is 22.11), you can use the following command to add the stable channel of the home manager:

`sudo nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.11.tar.gz home-manager`


And **if you are using unstable or the master channel**, use the following:

`sudo nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager`


Once you are done adding a channel by using any of one command shown above, update the channel using the following:

`sudo nix-channel --update`


Next, open the `configuration.nix`

file using:

`sudo nano /etc/nixos/configuration.nix`


And add the following line inside the `imports []`

:

`<home-manager/nixos>`


![install home-manager as NixOS module](https://itsfoss.com/content/images/2023/02/install-home-manager-as-NixOS-module.png)

Now, jump to the end of the line and add the following before `}`

:

```
home-manager.users.{username} = { pkgs, ... }: {
home.packages = [ ];
};
```


![syantax for home-manager module in NixOS config file](https://itsfoss.com/content/images/2023/02/syantax-for-home-manager-module-in-NixOS-config-file.png)

The above line was added to facilitate installing and removing packages I will show you next.

Now, [save changes and exit from the nano](https://linuxhandbook.com/nano-save-exit/) text editor.

Next, rebuild the config and make a switch:

`sudo nixos-rebuild switch`


But if you are using stable release and use the above command, it will throw the error saying :

🛠️ **error: The option `home-manager.users.user.home.stateVersion' is used but not defined:**

![error: The option `home-manager.users.user.home.stateVersion' is used but not defined.](https://itsfoss.com/content/images/2023/02/The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png)

To solve this issue, you will have to add the `home.stateVersion`

in your home manager block.

While writing, I'm running 22.11, so the whole home manager block would look like this:

```
home-manager.users.{username} = { pkgs, ... }: {
home.stateVersion = "22.11";
home.packages = [ ];
};
```


![how to solve The option `home-manager.users.user.home.stateVersion' is used but not defined.](https://itsfoss.com/content/images/2023/02/how-to-solve-The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png)

Save changes and exit from the nano text editor by pressing `Ctrl + O`

, hitting enter and `Ctrl + X`

.

Now, try to rebuild the config and make the switch again, and that should solve the issue.

## How to install packages using home-manager on NixOS

Now that you have home-manager installed, how to install packages with it:

### Using a standalone install of Home-manager

First, open the configuration file by using the following:

`nano /home/$USER/.config/nixpkgs/home.nix`


Jump to the end of the line and add the following code block before `}`

:

`home.packages = [];`


Now, all you have to do is write the package's name between those two braces.

For example, if I want to install** htop**, I will have to enter the following:

`home.packages = [pkgs.htop];`


Yes, you will have to usually append the name of the package with `pkgs.`


But if you want to get away with using `pkgs.`

using every time you install a new package, change the syntax of the code block as shown:

`home.packages = with pkgs; [];`


And now, you are no longer required to use `pkgs.`

for every installation:

`home.packages = with pkgs; [htop];`


For example, here, I wanted to install **htop, firefox, and LibreOffice** so my home block would look like this:

![install multiple packages using home-manager on NixOS](https://itsfoss.com/content/images/2023/02/install-multiple-packages-using-home-manager-on-NixOS.png)

Once you are done adding your favorite packages, save the config file and use the following command to install packages:

`home-manager switch`


### Using the NixOS module

First, open the `configuration.nix`

file using the following command:

`sudo nano /etc/nixos/configuration.nix`


In the configuration part, I've already added the home manager block, so all it is left is to add the name of the package inside `home.packages = [ ];`

in the shown format:

`home.packages = [ pkgs.package_name ];`


`pkgs.`

before the package name in the above section (installing packages on the standalone home manager).For example, if I want to [install htop](https://itsfoss.com/use-htop/), Firefox, and LibreOffice, then I will add:

`pkgs.htop pkgs.firefox pkgs.libreoffice`


And my home manager block would look like this:

![install multiple packages in home-manager as a NixOS module](https://itsfoss.com/content/images/2023/02/install-multiple-packages-in-home-manager-as-a-NixOS-module.png)

Now, save changes and exit from the text editor.

Next, rebuild the config and make a switch using the following command:

`sudo nixos-rebuild switch`


That's it! The packages will be installed in no time.

## 'Tis the end

I think you should go with the standalone installation, as you are not required to use the superuser privileges. Also, having separate config files for separate users is quite convenient if you run a system with multiple users.

So unless you want one file for every purpose, I see no other reason to use the module option.

With this, I conclude the NixOS beginner series. I hope it gets you a good enough platform to get familiar with this unique Linux distribution.

💬 *How did you like the NixOS series? Is there something else we should cover for NixOS beginners? Please provide your valuable feedback.*