mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Merge pull request #29044 from wxy/20230321.1-⭐️⭐️-NixOS-Series-5-How-to-set-up-home-manager-on-NixOS
ATRP:20230321.1 ⭐️⭐️ nix os series 5 how to set up home manager on nix os
This commit is contained in:
commit
eb2b000a57
@ -0,0 +1,276 @@
|
||||
[#]: subject: "NixOS Series #5: How to set up home-manager on NixOS?"
|
||||
[#]: via: "https://itsfoss.com/home-manager-nixos/"
|
||||
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "wxy"
|
||||
[#]: reviewer: "wxy"
|
||||
[#]: publisher: "wxy"
|
||||
[#]: url: "https://linux.cn/article-15697-1.html"
|
||||
|
||||
NixOS 系列 #5:如何在 NixOS 上设置家庭管理员?
|
||||
======
|
||||
|
||||
在发表这篇文章之前,我解释了如何为一个单用户系统 [在 NixOS 中安装和删除软件包][1]。
|
||||
|
||||
但是,如果你正在供多个用户使用,有一个很好的方法来分别满足每个用户的需求。
|
||||
|
||||
在本指南中,我将指导你如何在 NixOS 上设置一个家庭管理员,以及如何使用它来安装软件包。
|
||||
|
||||
如果你是新读者,本系列中讨论的一些资源包括:
|
||||
|
||||
- [使用 NixOS 的原因][2]
|
||||
- [在虚拟机上安装 NixOS][3]
|
||||
- [安装 NixOS 后要做的事情][4]
|
||||
|
||||
### 在 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
|
||||
```
|
||||
|
||||
🛠️ 在安装时,可能会出现以下错误:
|
||||
|
||||
![安装家庭管理员时出现构建错误][5]
|
||||
|
||||
重新启动你的系统并再次使用安装命令,它将开始安装。
|
||||
|
||||
一旦完成,它将显示独立安装的家庭管理员的位置:
|
||||
|
||||
![家庭管理员在NixOS中的位置][6]
|
||||
|
||||
#### 将家庭管理员安装为 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 模块][7]
|
||||
|
||||
现在,跳到该行的末尾,在 `}` 前添加以下内容:
|
||||
|
||||
```
|
||||
home-manager.users.{username} = { pkgs, ... }: {
|
||||
home.packages = [ ] ;
|
||||
};
|
||||
```
|
||||
|
||||
![NixOS 配置文件中的家庭管理员模块的格式][8]
|
||||
|
||||
上面这一行是为了方便安装和删除软件包而添加的,我接下来会告诉你。
|
||||
|
||||
现在,[保存修改并退出 Nano][9] 文本编辑器。
|
||||
|
||||
接下来,重建配置,并做一个切换:
|
||||
|
||||
```
|
||||
sudo nixos-rebuild switch
|
||||
```
|
||||
|
||||
但如果你使用的是稳定版,并使用上述命令,就会出现以下错误:
|
||||
|
||||
> 🛠️ 错误: 选项 `home-manager.users.user.home.stateVersion` 被使用但没有定义:
|
||||
|
||||
![错误: 选项 `home-manager.users.user.home.stateVersion` 已被使用但未被定义。][10]
|
||||
|
||||
要解决这个问题,你必须在你的家庭管理器块中添加 `home.stateVersion`。
|
||||
|
||||
在写这篇文章时,我正在运行 22.11,所以整个家庭管理员块看起来像这样:
|
||||
|
||||
```
|
||||
home-manager.users.{username} = { pkgs, ... }: {
|
||||
home.stateVersion = "22.11";
|
||||
home.packages = [ ] ;
|
||||
};
|
||||
```
|
||||
|
||||
![如何解决 `home-manager.users.user.home.stateVersion` 选项被使用但没有定义。][11]
|
||||
|
||||
保存修改,按 `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 上使用家庭管理员安装多个软件包][12]
|
||||
|
||||
一旦你完成了添加你喜欢的软件包,保存配置文件并使用以下命令来安装软件包:
|
||||
|
||||
```
|
||||
home-manager switch
|
||||
```
|
||||
|
||||
#### 使用 NixOS 模块方式
|
||||
|
||||
首先,用以下命令打开 `configuration.nix` 文件:
|
||||
|
||||
```
|
||||
sudo nano /etc/nixos/configuration.nix
|
||||
```
|
||||
|
||||
在配置部分,我已经添加了家庭管理员块,所以剩下的就是在 `home.packages = [ ];` 里面添加软件包的名称,格式如图所示:
|
||||
|
||||
```
|
||||
home.packages = [ pkgs.package_name ] ;
|
||||
```
|
||||
|
||||
> 💡我已经在上节提到软件包名称前你可以使用 `pkgs.` 。
|
||||
|
||||
例如,如果我想 [安装 htop][13]、Firefox 和 LibreOffice,那么我将添加:
|
||||
|
||||
```
|
||||
pkgs.htop pkgs.firefox pkgs.libreoffice
|
||||
```
|
||||
|
||||
然后我的家庭管理员块会看起来像这样:
|
||||
|
||||
![作为 NixOS 模块在家庭管理员中安装多个软件包][14]
|
||||
|
||||
现在,保存修改并退出文本编辑器。
|
||||
|
||||
接下来,重建配置并使用以下命令进行切换:
|
||||
|
||||
```
|
||||
sudo nixos-rebuild switch
|
||||
```
|
||||
|
||||
这是这样!软件包将很快被安装。
|
||||
|
||||
### 这就结束了
|
||||
|
||||
我认为你应该选择独立安装,因为你不需要使用超级用户的权限。另外,如果你运行一个有多个用户的系统,为不同的用户拥有不同的配置文件是相当方便的。
|
||||
|
||||
因此,除非你想用一个文件来实现各种目的,否则我认为没有其他理由使用模块选项。
|
||||
|
||||
就这样,我结束了 NixOS 的初学者系列。我希望它能给你一个足够好的平台来熟悉这个独特的 Linux 发行版。
|
||||
|
||||
💬 你喜欢 NixOS 系列文章吗?对于 NixOS 的初学者,我们还有什么要介绍的吗?请提供你的宝贵意见。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/home-manager-nixos/
|
||||
|
||||
作者:[Sagar Sharma][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/sagar/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://linux.cn/article-15645-1.html
|
||||
[2]: https://linux.cn/article-15606-1.html
|
||||
[3]: https://linux.cn/article-15624-1.html
|
||||
[4]: https://linux.cn/article-15663-1.html
|
||||
[5]: https://itsfoss.com/content/images/2023/02/building-error-while-installing-home-manager.png
|
||||
[6]: https://itsfoss.com/content/images/2023/02/location-of-home-manager-in-NixOS.png
|
||||
[7]: https://itsfoss.com/content/images/2023/02/install-home-manager-as-NixOS-module.png
|
||||
[8]: https://itsfoss.com/content/images/2023/02/syantax-for-home-manager-module-in-NixOS-config-file.png
|
||||
[9]: https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com
|
||||
[10]: https://itsfoss.com/content/images/2023/02/The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png
|
||||
[11]: https://itsfoss.com/content/images/2023/02/how-to-solve-The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png
|
||||
[12]: https://itsfoss.com/content/images/2023/02/install-multiple-packages-using-home-manager-on-NixOS.png
|
||||
[13]: https://itsfoss.com/use-htop/
|
||||
[14]: https://itsfoss.com/content/images/2023/02/install-multiple-packages-in-home-manager-as-a-NixOS-module.png
|
||||
[0]: https://img.linux.net.cn/data/attachment/album/202304/06/110641k8v9q1152hhhh114.jpg
|
@ -1,285 +0,0 @@
|
||||
[#]: subject: "NixOS Series #5: How to set up home-manager on NixOS?"
|
||||
[#]: via: "https://itsfoss.com/home-manager-nixos/"
|
||||
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
NixOS Series #5: How to set up home-manager on NixOS?
|
||||
======
|
||||
|
||||
Before publishing this, I explained how to [install and remove packages in NixOS][1] 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:
|
||||
|
||||
- [Reasons to use nixOS][2]
|
||||
- [Installing NixOS on a virtual machine][3]
|
||||
- [Things to do after installing NixOS][4]
|
||||
|
||||
### 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][5]
|
||||
|
||||
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][6]
|
||||
|
||||
#### Installing home-manager as a NixOS module
|
||||
|
||||
⚠️
|
||||
|
||||
You will need sudo privileges if you choose to use the 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][7]
|
||||
|
||||
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][8]
|
||||
|
||||
The above line was added to facilitate installing and removing packages I will show you next.
|
||||
|
||||
Now, [save changes and exit from the nano][9] 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.][10]
|
||||
|
||||
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.][11]
|
||||
|
||||
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][12]
|
||||
|
||||
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 ];
|
||||
```
|
||||
|
||||
💡
|
||||
|
||||
I've mentioned how you can get away with using
|
||||
|
||||
```
|
||||
pkgs.
|
||||
```
|
||||
|
||||
before the package name in the above section (installing packages on the standalone home manager).
|
||||
|
||||
For example, if I want to [install htop][13], 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][14]
|
||||
|
||||
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._
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/home-manager-nixos/
|
||||
|
||||
作者:[Sagar Sharma][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/sagar/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/nixos-package-management/
|
||||
[2]: https://itsfoss.com/why-use-nixos/
|
||||
[3]: https://itsfoss.com/install-nixos-vm/
|
||||
[4]: https://itsfoss.com/things-to-do-after-installing-nixos/
|
||||
[5]: https://itsfoss.com/content/images/2023/02/building-error-while-installing-home-manager.png
|
||||
[6]: https://itsfoss.com/content/images/2023/02/location-of-home-manager-in-NixOS.png
|
||||
[7]: https://itsfoss.com/content/images/2023/02/install-home-manager-as-NixOS-module.png
|
||||
[8]: https://itsfoss.com/content/images/2023/02/syantax-for-home-manager-module-in-NixOS-config-file.png
|
||||
[9]: https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com
|
||||
[10]: https://itsfoss.com/content/images/2023/02/The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png
|
||||
[11]: https://itsfoss.com/content/images/2023/02/how-to-solve-The-option--home-manager.users.user.home.stateVersion--is-used-but-not-defined..png
|
||||
[12]: https://itsfoss.com/content/images/2023/02/install-multiple-packages-using-home-manager-on-NixOS.png
|
||||
[13]: https://itsfoss.com/use-htop/
|
||||
[14]: https://itsfoss.com/content/images/2023/02/install-multiple-packages-in-home-manager-as-a-NixOS-module.png
|
Loading…
Reference in New Issue
Block a user