在 Ubuntu 等非 Nix 操作系统上安装和使用 Nix 包管理器
NixOS Nix
![](/data/attachment/album/202310/22/082116ket5ed87padptmbw.jpg)



> 
> Nix 软件包管理器可以安装在任何 Linux 发行版上。具体方法如下。
> 
> 
> 


[人们喜欢使用不可变的 NixOS](/article-15606-1.html) 的原因之一是它的 Nix 包管理器。


它有超过 80,000 个软件包，这可能与 Debian 软件包的数量相差比较大，但仍然令人印象深刻。


好处是你不必仅仅为了包管理器而 [安装 NixOS](/article-15624-1.html)。与 [Homebrew](/article-14065-1.html) 和 Rust 的 [Cargo 包管理器](/article-13938-1.html) 一样，你可以在当前发行版中使用 Nix 包管理器。


为什么要这么做？因为有时，你可能会发现仅以 Nix 打包格式提供的新应用。这种情况很少见，但有可能。


在本教程中，我将引导你完成以下内容：


* 安装 Nix 包管理器
* 搜索并安装包
* 尝试/测试软件包而不安装它们
* 更新包
* 删除包


### 在其他 Linux 发行版上安装 Nix 包管理器


Nix 包管理器有两种安装方式：全局安装和本地安装。



> 
> ? 全局安装意味着系统上的每个可用用户都可以访问 nix 包管理器，而本地安装仅适用于当前用户。[Nix 官方文档](https://nixos.org/learn) 建议你使用全局安装。
> 
> 
> 


#### 全局安装


如果你想全局安装 Nix 包管理器，那么，你需要执行以下命令：



```
sh <(curl -L https://nixos.org/nix/install) --daemon

```

执行上述命令后，需要输入 `y` 键并按回车键：


![](/data/attachment/album/202310/22/082143da0fndc0ego334d7.png)


完成后，关闭当前终端，因为它不会在当前终端会话上运行。


#### 本地安装


如果你更喜欢本地安装并且不想每次都使用 `sudo`，则执行以下命令：



```
sh <(curl -L https://nixos.org/nix/install) --no-daemon

```

输入 `y` 并在要求确认时按回车键。


完成后，关闭当前终端会话并启动一个新终端会话以使用 Nix 包管理器。


### 搜索并安装包


安装 Nix 包管理器后，下一步是搜索包。


首先，[访问 Nix 搜索的官方页面](https://search.nixos.org/packages) 并输入你要安装的软件包的名称。


从给定的描述中，你可以找到所需的软件包，然后选择 `nix-env` 进行永久安装。


在这里，你可以添加一些额外的关键字以获得更好的搜索结果。例如，在这里，我搜索 “Firefox browser”：


![](/data/attachment/album/202310/22/082144vouku3as3qdgyy5o.png)


我上面提到的最后一步（“复制命令”）什么也不做，只是为你提供了一个用于安装的命令。


现在，你所要做的就是在终端中执行该命令。


就我而言，它给了我以下命令来安装 Firefox：



```
nix-env -iA nixpkgs.firefox

```

完成后，你可以使用以下命令列出已安装的软件包：



```
nix-env -q

```

![](/data/attachment/album/202310/22/082144tcomo9e5bqa4rn6o.png)


### 使用包而不安装它们


到目前为止，这是 Nix 包管理器的最佳功能，因为你可以使用/测试包甚至不用安装它！


为此，你可以使用 Nix Shell，它允许你将交互式 Shell 与指定的包一起使用，关闭后，你将无法再访问该包。


很酷，对吧？


要使用 nix-shell 访问你喜欢的软件包，请使用以下命令语法：



```
nix-shell -p <package_name>

```

例如，我想使用一次 `neofetch`，所以我使用了以下命令：



```
nix-shell -p neofetch

```

![](/data/attachment/album/202310/22/082144ivyevv9unexcuxc1.png)


要退出 Shell，你所要做的就是执行 `exit` 命令：



```
exit

```

### 使用 Nix 包管理器更新包


使用 Nix 包管理器更新包非常简单。


要更新软件包，首先，你需要使用以下命令更新频道：



```
nix-channel --update

```

接下来，你可以通过试运行更新命令来列出过时的软件包：



```
nix-env --upgrade --dry-run

```

![](/data/attachment/album/202310/22/082146xg5gllqoigbgtuqx.png)


就我而言，Firefox 浏览器已经过时，需要更新！


要更新单个包，请使用以下命令：



```
nix-env -u <Package_name>

```

如果你想一次更新所有软件包，请使用以下命令：



```
nix-env -u

```

### 使用 Nix 包管理器删除包


要删除软件包，你只需按以下方式执行 `nix-env` 命令即可：



```
nix-env --uninstall [package_name]

```

例如，如果我想删除 Firefox 浏览器，那么，我将使用以下命令：



```
nix-env --uninstall firefox

```

![](/data/attachment/album/202310/22/082146e5n7nkp1l5tnl55p.png)


### 通过 NixOS 释放其全部潜力


如果你喜欢 Nix 包管理器背后的想法，请相信我，你一定会喜欢 [NixOS](https://nixos.org/)。


仅使用一个配置文件来复制整个系统就足以说服我切换到 NixOS，但还有更多原因：


我喜欢 NixOS。以至于我写了整个系列，这样你就不必阅读文档（基础知识）：



> 
> **[NixOS 系列](/article-15606-1.html)**
> 
> 
> 


我希望你能像我一样喜欢使用它。


*（题图：MJ/da586165-eadb-4ed7-9a0b-9c92903344d5）*




---


via: <https://itsfoss.com/ubuntu-install-nix-package-manager/>


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


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


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

One of the reasons [why people like using the immutable NixOS](https://itsfoss.com/why-use-nixos/) is its Nix package manager.

It has over 80,000 packages, which might not be close to the number of Debian packages but pretty impressive nonetheless.

The good thing is that you don't have to [install NixOS](https://itsfoss.com/install-nixos-vm/) just for the package manager. Like [Homebrew](https://itsfoss.com/homebrew-linux/) and Rust's [Cargo package manager](https://itsfoss.com/install-rust-cargo-ubuntu-linux/), you can use Nix package manager in your present distribution.

Why would you do that? Because sometimes, you may find a new application that is only available in Nix packaging format. It's rare but possible.

In this tutorial, I will walk you through the following:

- Installing Nix package manager
- Searching and installing packages
- Try/test packages without installing them
- Update packages
- Removing packages

## Install Nix package manager on other Linux distros

There are two ways to install the Nix package manager: global install and local install.

[official Nix documentation](https://nixos.org/learn)recommends you use the global install.

#### For global installation:

If you want to install the Nix package manager globally, then, you need to execute the following command:

`sh <(curl -L https://nixos.org/nix/install) --daemon`


Once you execute the above command, you need to enter the `y`

key and press the `Enter`

key:

![](https://itsfoss.com/content/images/2023/10/Install-nix-package-manager-globally.png)

Once done, close the current terminal as it won't work on the current terminal session.

#### For local installation:

If you prefer the local installation and don't want to use sudo every time, then execute the following command:

`sh <(curl -L https://nixos.org/nix/install) --no-daemon`


Enter `y`

and press the `Enter`

key when asked for the confirmation.

Once done, close the current terminal session and start a new one to use the Nix package manager.

## Search and install packages

Once you have installed the Nix package manager, the next step is to search for packages.

First, [visit the official page of Nix search](https://search.nixos.org/packages) and type the name of the package that you want to install.

From the given description, you can find the package which you were looking for and then, choose the `nix-env`

to go with the permanent installation.

Here, you can add some extra keywords to have better search results. For example, here, I searched for "Firefox browser":

![Search packages to install using the Nix package manager](https://itsfoss.com/content/images/2023/10/Search-packages-to-install-using-the-Nix-package-manager.png)

The last step that I mentioned above (copy command) does nothing but gives you a command that you execute for the installation.

Now, all you have to do is execute that command in your terminal.

In my case, it gave me the following command to install Firefox:

`nix-env -iA nixpkgs.firefox`


Once done, you can list the installed packages using the following command:

`nix-env -q`


![List installed packages using the nix package manager](https://itsfoss.com/content/images/2023/10/List-installed-packages-using-the-nix-package-manager.png)

## Use packages without installing them

By far this is the best feature of the Nix package manager as you get to use/test packages that are not even installed!

For this, you can use the nix shell which lets you use an interactive shell with the specified package, and once closed, you can no longer access that package.

Cool, right?

To use the nix-shell to access your favorite package, use the following command syntax:

`nix-shell -p <package_name>`


For example, I wanted to use neofetch once, so I used the following:

`nix-shell -p neofetch`


![Use packages wihout installing them using the nix package manager](https://itsfoss.com/content/images/2023/10/Use-packages-wihout-installing-them-using-the-nix-package-manager.png)

To exit from the shell, all you have to do is execute the `exit`

command:

`exit`


## Update packages using the Nix package manager

Updating packages using the Nix package manager is pretty easy.

To update packages, first, you need to update channels using the following command:

`nix-channel --update`


Next, you can list the outdated packages by dry-running the update command:

`nix-env --upgrade --dry-run`


![List outdated packages using the nix package manager](https://itsfoss.com/content/images/2023/10/List-outdated-packages-using-the-nix-package-manager.png)

In my case, the Firefox browser was outdated and needs to be updated!

To update a single package, use the following command:

`nix-env -u <Package_name>`


And if you want to update all the packages at once, then use the following:

`nix-env -u`


## Remove packages using the Nix package manager

To remove packages, all you have to do is execute the `nix-env`

command in the following manner:

`nix-env --uninstall [package_name]`


For example, if I want to remove the Firefox browser, then, I will use the following command:

`nix-env --uninstall firefox`


![Remove packages using the nix package manager](https://itsfoss.com/content/images/2023/10/Remove-packages-using-the-nix-package-manager-1.png)

## Unlock its full potential with NixOS

If you liked the idea behind the Nix package manager, trust me, you're going to love the [NixOS](https://nixos.org/).

Reproducing the entire system just by using one config file was enough to convince me to make a switch to NixOS but there are more reasons:

[6 Reasons Why You Should Consider Using NixOS LinuxNixOS is an exciting distribution. Let’s take a look why you might want to give it a try.](https://itsfoss.com/why-use-nixos/)![](https://itsfoss.com/content/images/2023/02/nix-series-1.png)


![](https://itsfoss.com/content/images/2023/02/nix-series-1.png)

I love NixOS. So much so that I wrote a whole series so you don't have to go through documentation (for basics):

[Getting Started With NixOS LinuxA tutorial series to help you get familiar with the immutable NixOS Linux distribution.](https://itsfoss.com/tag/nix-os/)![](https://itsfoss.com/content/images/2023/03/nix-os-tag.png)


![](https://itsfoss.com/content/images/2023/03/nix-os-tag.png)

I hope you will enjoy using it as much as I do.