@wxy
https://linux.cn/article-15645-1.html
This commit is contained in:
Xingyu Wang 2023-03-21 09:10:11 +08:00
parent 5f9526e18d
commit d811959b87
2 changed files with 301 additions and 290 deletions

View File

@ -0,0 +1,301 @@
[#]: subject: "NixOS Series #3: Install and Remove Packages in NixOS"
[#]: via: "https://itsfoss.com/nixos-package-management/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "wxy"
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-15645-1.html"
NixOS 系列 #3:在 NixOS 中安装和删除软件包
======
NixOS 中的打包系统是它最强大的地方。Nix 软件包管理器使用的语法与 `apt`、`dnf` 和其他软件包管理器大不相同。
这也是 [人们应该尝试使用 NixOS 的原因之一][1]。
在本指南中,我将分享两种在 NixOS 上安装和删除软件包的方法:
- 使用 Nix 软件包管理器
- 使用 `configuration.nix` 配置文件
> ⚠️ 使用 Nix 软件包管理器,你只能安装软件包,而不能安装 OpenSSH 或 Plex 服务器等服务。对于服务的安装,你必须使用 Nix 配置文件。
要安装任何软件包,必须知道它的确切名称,为此,我将从如何在 NixOS 中搜索软件包开始。
### 搜索软件包
要寻找软件包,你可以使用它的 [网页搜索][2]。
你可以使用如下步骤:
- 在搜索栏中输入软件包的名称
- 选择适当的软件包(从给出的描述中决定)
- 点击 “nix-env” 标签页
- 复制 NixOS 命令(第一条)
例如,如果我想要 `librewolf` 包,我将执行以下操作:
![使用 Nix 软件包管理器的网络搜索寻找软件包][3]
你也可以通过终端做同样的事情。
要使用终端搜索软件包,你可以按照给定的命令语法进行:
```
nix-env -qaP --description [软件包名称]
```
例如,在这里,我搜索了 `librewolf`
![使用终端搜索 NixOS 中的软件包][4]
你必须复制输出的第一行,因为那是你需要安装的软件包的名称。
在这里它是 `nixos.librewolf`
是的,它听起来可能没有像使用 [APT][5] 或 DNF 时软件包名字那么方便。但是,我认为这并不是大问题。
一些妥协或许会换来一些好处?
### 在 NixOS 中安装一个软件包
要安装一个软件包,你所要做的就是使用以下命令语法:
```
nix-env -iA [软件包名称]
```
而且,如果你使用网络搜索来寻找软件包,你就已经有了安装所需的确切命令。
所以,假设我想安装 `librewolf',我将使用以下命令:
```
nix-env -iA nixos.librewolf
```
如果你想进行全系统的安装(让每个用户都能使用这个包),用 `sudo` 执行安装命令:
```
sudo nix-env -iA nixos.librewolf
```
就是这样!你将很快安装好你喜欢的软件包。
### 在 NixOS 中卸载一个软件包
要删除一个软件包,你可以参考下面的命令语法:
```
nix-env --uninstall [软件包名称]
```
因此,如果我必须删除 `librewolf` 包,我必须使用以下命令:
```
nix-env --uninstall librewolf
```
如果你仔细注意,我使用了 `librewolf` 而不是 `nixos.librewolf` 来安装。
这意味着你在删除软件包时要跳过 `nixos` 部分,这使事情变得简单而快速。
### 在 NixOS 中安装服务
正如我前面提到的,你不能使用 Nix 软件包管理器来安装像 OpenSSH、Plex 服务器、[Flatpak][6] 等服务。
从搜索服务到安装过程,都与你上面看到的不同。
所以让我先说说如何 **搜索服务**
- 要搜索服务,请前往 Nix 软件包搜索 [网页][2]
- 选择 “<ruby>NixOS 选项<rt>NixOS options</rt></ruby>”(页面顶部菜单行的第三个选项)
- 输入你要找的服务的名称
- 复制服务的名称
例如,在这里,我正在搜索 OpenSSH 服务。
![搜索 NixOS 中的 OpenSSH 服务][7]
一旦你找到了这个名字,用下面的命令打开 `configuration.nix` 文件:
```
sudo nano /etc/nixos/configuration.nix
```
并在行末添加服务的名称(在 `}` 之前),如下:
```
[service_name] = true;
```
由于 **我想启用 OpenSSH**,我将添加以下内容:
```
services.openssh.enable = true;
```
![在 NixOS 上启用 OpenSSH][8]
一旦你在配置文件中添加了服务,[保存修改并退出 Nano][9] 文本编辑器。
要启用该服务,请重建配置文件,并使用以下命令切换到所做的更改:
```
sudo nixos-rebuild switch
```
这就行了,你已经启用了该服务。
### 从 NixOS 卸载服务
要卸载一个服务,你所要做的就是在 `configuration.nix` 文件中删除或注释该服务的一行。
因此,首先,用以下命令打开配置文件:
```
sudo nano /etc/nixos/configuration.nix
```
寻找服务,并删除这一行或用 `#` 注释掉:
![从 NixOS 删除服务][10]
通过添加注释 `#`,我忽略了 OpenSSH 服务的加载,因为我不再需要它在我的系统上。
保存修改并退出文本编辑器。
最后,重建配置文件并进行切换:
```
sudo nixos-rebuild switch
```
### 使用 Nix 配置文件安装软件包
配置文件可以让你 **方便地一次性管理软件包**
要使用 Nix 配置文件安装软件包,你必须在配置文件中输入软件包的名称、重建,然后切换到配置文件,就可以了。
首先,打开 `configuration.nix` 文件。
```
sudo nano /etc/nixos/configuration.nix
```
如果你想 **为一个特定的登录用户安装软件包**,将软件包的名称添加到用户的配置文件中。
用户配置文件看起来像这样:
```
users.users.sagar = {
isNormalUser = true;
description = "Sagar";
extraGroups = [ "networkmanager" "wheel" ];
packages = with pkgs; [
firefox
];
};
```
当然,它将显示你的用户名而不是 `sagar`
你应该使用如下语法来添加软件包的名称:
```
packages = with pkgs; [
软件包名称
];
```
所以我们假设我也想安装 `Thunderbird`,那么我将添加它的名字,如下所示:
![使用 Nix 配置文件在 NixOS 中安装一个包][11]
你必须在方括号内添加所有的软件包名称,不要用逗号。它必须像截图中描述的那样一个软件一个新的行。
但是如果你想在整个系统中安装这个包,那么你必须在 `environment.systemPackages` 下添加包的名字,比如:
```
environment.systemPackages = with pkgs; [
软件包名称
];
```
![使用 Nix 配置文件在 NixOS 中全系统安装软件包][12]
一旦你完成了在系统配置文件或用户配置文件,甚至两者中添加所需软件包的名称,你将需要按照同样的命令来完成安装:
```
sudo nixos-rebuild switch
```
这样就可以了!
### 使用 Nix 配置文件删除软件包
要删除软件包,你所要做的就是按照给定的简单步骤进行:
- 打开 Nix 配置文件
- 删除或注释掉软件包的名称
- 重新构建配置并进行切换
所以,让我们从第一步开始(打开配置文件):
```
sudo nano /etc/nixos/configuration.nix
```
接下来,注释掉用户配置文件或系统配置文件中的包的名称:
![在 NixOS 上使用 Nix 配置文件删除包][13]
保存更改并退出配置文件。
最后,重建配置文件,并做一个切换来删除包:
```
sudo nixos-rebuild switch
```
这是这样!
> 📋 目前,还没有官方的 GUI 工具来帮助你安装/删除软件包。你可能会发现一些由社区开发的项目,如 [nix-gui][14] 和 [nix42b][15],但它们不再被维护或仅仅处于早期开发阶段。
### 接下来...
我希望你喜欢阅读 NixOS 系列,就像我写它一样。
在下一篇中,我将强调一些在你安装 NixOS 后需要马上做的重要事情。
如果你认为我遗漏了什么或有其他建议,请在评论中告诉我。
--------------------------------------------------------------------------------
via: https://itsfoss.com/nixos-package-management/
作者:[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-15606-1.html
[2]: https://search.nixos.org/packages?ref=its-foss
[3]: https://itsfoss.com/content/images/2023/02/search-packages-for-nixos-1.png
[4]: https://itsfoss.com/content/images/2023/02/search-packages-in-nixos.png
[5]: https://itsfoss.com/apt-command-guide/
[6]: https://itsfoss.com/what-is-flatpak/
[7]: https://itsfoss.com/content/images/2023/02/Search-the-service-for-nixos-1.png
[8]: https://itsfoss.com/content/images/2023/02/enable-openssh-on-NixOS.png
[9]: https://linuxhandbook.com/nano-save-exit/?ref=its-foss
[10]: https://itsfoss.com/content/images/2023/02/remove-service-from-NixOS.png
[11]: https://itsfoss.com/content/images/2023/02/install-a-package-in-NixOS-using-the-Nix-config-file.png
[12]: https://itsfoss.com/content/images/2023/02/install-package-systemwide-in-NixOS-using-Nix-configuration-file-1.png
[13]: https://itsfoss.com/content/images/2023/02/remove-package-using-nix-config-file-on-NixOS.png
[14]: https://github.com/nix-gui/nix-gui?ref=its-foss
[15]: https://gitlab.com/juliendehos/nix42b?ref=its-foss
[0]: https://img.linux.net.cn/data/attachment/album/202303/21/090913cnq91ob2hn9b2qbh.jpg

View File

@ -1,290 +0,0 @@
[#]: subject: "NixOS Series #3: Install and Remove Packages in NixOS"
[#]: via: "https://itsfoss.com/nixos-package-management/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
NixOS Series #3: Install and Remove Packages in NixOS
======
The packaging system in NixOS is its strongest point. The Nix package manager uses a vastly different syntax than apt, dnf and other package managers.
It is also [one of the reasons why one should try using NixOS][1].
In this guide, I will share two ways to install and remove packages on NixOS:
- **Using the Nix package manager**
- **Using `configuration.nix` config file**
> ⚠️ Using the Nix package manager, you can only install packages but not services like SSH or Plex server. For the installation of services, you'd have to use a Nix configuration file.
To install any package, it is necessary to know its exact name, and for that purpose, I will start with how you can search for packages in NixOS.
### Search packages
To look for packages, you can use its [web search][2] using your preferred browser.
You can utilize its web search using the given steps:
- **Enter the name of the package in the search bar**
- **Select the appropriate package (decide from the given description)**
- **Click on `nix-env` option**
- **And copy the command for `NixOS` (first one)**
For example, if I want `librewolf` package, I will perform the following:
![searching for package using nix package manager web search][3]
You can do the same through the **terminal**.
To search packages using the terminal, you can follow the given command syntax:
```
nix-env -qaP --description [package_name]
```
For example, here, I searched for the `librewolf`:
![search packages in nixos using terminal][4]
You will have to copy the first line of the output as that is the name for the package you need to install.
For me, it was `nixos.librewolf`.
Yes, **it may not sound as convenient as the package names** when using [APT][5] or DNF. But, it is not too bad, I think.
Some compromises for some benefits, I guess?
### Install a package in NixOS
To install a package, all you have to do is use the following command syntax:
```
nix-env -iA [package_name]
```
And if you use the web search to look for the package, you will already have the exact command you need for the installation.
So let's say I want to install `librewolf`, so I will be using the following command:
```
nix-env -iA nixos.librewolf
```
And if you want to perform a system-wide install (make this package available for every user), execute the installation command with `sudo`:
```
sudo nix-env -iA nixos.librewolf
```
That's it! You will have your favorite package installed in no time.
### Uninstall a Package in NixOS
To remove a package, you can refer to the given command syntax:
```
nix-env --uninstall [package_name]
```
So if I have to remove the `librewolf` package, I have to use the following command:
```
nix-env --uninstall librewolf
```
If you notice closely, I have used `librewolf` instead of `nixos.librewolf` what I used for the installation.
This means you will have to skip the `nixos` part during removal of the package, which makes things easy and quick.
### Install services in NixOS
As I mentioned earlier, you can not use the nix package manager to install services like OpenSSH, Plex server, [Flatpak][6], etc.
From searching for the service to the installation process, it differs from what you saw above.
So let me start with how you can **search for a service**:
- **To search for the service, head [over to the web page][2] for the Nix package search.**
- **Select `NixOS options` (3rd option in the top-menu row of the page).**
- **Enter the name of the service you are looking for.**
- **Copy the name of the service.**
For example, here, I'm searching for OpenSSH service:
![search for openssh service in NixOS][7]
Once you have the name, open the `configuration.nix` file using the following command:
```
sudo nano /etc/nixos/configuration.nix
```
And add the name of the service at the end of the line (before `}`) in the following manner:
```
[service_name] = true;
```
As **I want to enable OpenSSH**, I will be adding the following:
```
services.openssh.enable = true;
```
![enable openssh on NixOS][8]
Once you are done adding the service to the config file, [save the changes and exit from the nano][9] text editor.
To enable the service, rebuild the config file and switch to the changes using the following command:
```
sudo nixos-rebuild switch
```
That's it! You have the service enabled.
### Uninstall services from NixOS
To uninstall a service, all you have to do is remove or comment out the line for that service from `configuration.nix` file.
So first, open the config file using the following command:
```
sudo nano /etc/nixos/configuration.nix
```
Look for the service and remove the line or comment it out with `#`:
![remove service from NixOS][10]
With the added comment #, I am ignoring the OpenSSH service to load up as I no longer want it on my system.
Once done, **save the change and exit from the text editor.**
And finally, rebuild the config file and make the switch:
```
sudo nixos-rebuild switch
```
### Install packages using Nix config file
The configuration file lets you **easily manage packages in one go**.
To install a package using the Nix config file, you have to enter the package's name in the config file, rebuild, and switch to the config file, and that's it.
First, open the `configuration.nix` file:
```
sudo nano /etc/nixos/configuration.nix
```
If you want to **install a package for a specific logged-in user,** add the package's name to the user's profile.
The user profile looks like this:
```
users.users.sagar = {
isNormalUser = true;
description = "Sagar";
extraGroups = [ "networkmanager" "wheel" ];
packages = with pkgs; [
firefox
];
};
```
Sure, it will show your username instead of `sagar`.
And you are supposed to add the name of the package using the syntax  `packages = with pkgs; [package_name];`
So let's suppose I want to install `Thunderbird` as well, then I will add its name as shown below:
![install a package in NixOS using the Nix config file][11]
You must add **all the package names inside the square bracket** without commas. It has to be on a new line as the screenshot describes.
But **if you want to install this package system-wide**, then you will have to add the package name under **environment.systemPackages** like:
`environment.systemPackages = with pkgs; [package_name]`;
![install package systemwide in NixOS using Nix configuration file][12]
Once you are done adding the name of the required package in the system profile or user profile, or even both, you will have to follow the same command to complete the installation:
```
sudo nixos-rebuild switch
```
And you have it!
### Remove packages using the Nix config file
To remove the package, all you have to do is follow the given simple steps:
- **Open the Nix config file**
- **Remove or comment out the name of the package**
- **Rebuild the config and make a switch**
So let's start with the first step (opening the config file):
```
sudo nano /etc/nixos/configuration.nix
```
Next, comment out the name of the packet from the user profile or system profile:
![remove package using nix config file on NixOS][13]
Save changes and exit from the config file.
And finally, rebuild the config and make a switch to remove the package:
```
sudo nixos-rebuild switch
```
That's it!
> 📋 Currently, there are no official GUI tools to help you with installing/removing packages. You might find some projects like [nix-gui][14] and [nix42b][15] developed by the community, but they are no longer maintained or simply in their early stages of development.
### Next Up...
I hope you enjoy reading the NixOS series as much as I do writing it.
In the next part, I will highlight some **important things you need to do right after installing NixOS**.
_💬 If you think I'm missing out on something or have any other suggestions, please let me know in the comments._
--------------------------------------------------------------------------------
via: https://itsfoss.com/nixos-package-management/
作者:[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/why-use-nixos/
[2]: https://search.nixos.org/packages?ref=its-foss
[3]: https://itsfoss.com/content/images/2023/02/search-packages-for-nixos-1.png
[4]: https://itsfoss.com/content/images/2023/02/search-packages-in-nixos.png
[5]: https://itsfoss.com/apt-command-guide/
[6]: https://itsfoss.com/what-is-flatpak/
[7]: https://itsfoss.com/content/images/2023/02/Search-the-service-for-nixos-1.png
[8]: https://itsfoss.com/content/images/2023/02/enable-openssh-on-NixOS.png
[9]: https://linuxhandbook.com/nano-save-exit/?ref=its-foss
[10]: https://itsfoss.com/content/images/2023/02/remove-service-from-NixOS.png
[11]: https://itsfoss.com/content/images/2023/02/install-a-package-in-NixOS-using-the-Nix-config-file.png
[12]: https://itsfoss.com/content/images/2023/02/install-package-systemwide-in-NixOS-using-Nix-configuration-file-1.png
[13]: https://itsfoss.com/content/images/2023/02/remove-package-using-nix-config-file-on-NixOS.png
[14]: https://github.com/nix-gui/nix-gui?ref=its-foss
[15]: https://gitlab.com/juliendehos/nix42b?ref=its-foss