NixOS 系列 #4：安装 NixOS 后要做的事
NixOS
![](/data/attachment/album/202303/26/142500uzc095szl064s9xx.jpg)


安装之后，你会发现 NixOS 与通用的 Linux 发行版有很大的不同。


当然，作为 [高级 Linux 发行版](https://itsfoss.com/advanced-linux-distros/)之一，大多数新用户对它并不熟悉。


如果你不知道 [为什么你应该使用 NixOS](/article-15606-1.html)，并且出于好奇而尝试它，那么在继续进行之前，知道它是为谁准备的至关重要。


虽然我假设你已经安装了这个发行版，但如果你是第一次使用，我建议先 [在虚拟机上安装 NixOS](/article-15624-1.html)。


### 1、更新软件包


即使你使用最新的 ISO 进行安装，也总是会有更新的。那么为什么不从更新软件包开始呢？


要升级软件包，首先，你必须在添加的频道中检查是否有更新：



```
nix-channel --update

```

然后，使用下面的命令来安装这些更新（如果有的话）：



```
sudo nixos-rebuild switch --upgrade

```

这就行了！它将处理其余的事情。


### 2、在 NixOS 中改变主机名


如果你尝试用传统的方法 [改变主机名](https://itsfoss.com/change-hostname-ubuntu/)（使用 `hostnamectl` 命令），会出现以下错误：


![在 NixOS 中改变主机名的错误](/data/attachment/album/202303/26/142514uqjyy5wgdiyeqmpd.png)


在 NixOS 中，你可以用它的主配置文件轻松地改变主机名。使用如下命令：



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

```

在这个配置文件中，寻找以下一行：



```
networking.hostName = "nixos"；

```

然后把它改成：



```
networking.hostName = "Your_Hostname"；

```

例如，我把我的主机名改为 `itsFOSS`：



```
networking.hostName = "itsFOSS"；

```

![在 NixOS 中改变主机名](/data/attachment/album/202303/26/142514up8e3hhthhj48xie.png)


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


为了使你对主机名的修改生效，执行以下命令：



```
sudo nixos-rebuild switch

```

最后，重新打开终端，主机名的变化应该反映出来。


### 3、设置 Flatpak


我知道你可能在想什么。Nix 软件包管理器已经提供了大量的软件包。那么，为什么你需要 Flatpak 呢？


安装你所需要的东西对于第一次使用的人来说可能有点费时。所以，Flatpak 应该能给你带来方便。


[设置 Flatpak](https://itsfoss.com/flatpak-guide/) 与你在 Ubuntu 上做的不一样。


要设置 Flatpak，你必须对 `configuration.nix` 文件进行修改，可以通过以下方式访问该文件：



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

```

[在 nano 中移动到最后一行](https://linuxhandbook.com/beginning-end-file-nano/?ref=its-foss)，在 `}` 前添加以下一行：



```
services.flatpak.enable = true；

```

![在 NixOS 上设置 Flatpak](/data/attachment/album/202303/26/142515phcz4z4hg94qdedh.png)


按 `Ctrl + O` 保存更改，按回车键，按 `Ctrl + X` 退出。


接下来，使用以下命令重建并切换到新的配置文件：



```
sudo nixos-rebuild switch

```

最后，使用下面的命令将 Flathub 软件库添加到系统中：



```
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

```

想知道到底什么是 Flatpak 包吗？你可以参考我们关于它的文章：



> 
> **[什么是 Flatpak？你需要知道的关于这个通用包装系统的一切重要信息](https://itsfoss.com/what-is-flatpak/)**
> 
> 
> 


### 4、启用垃圾收集


NixOS 以其不可改变性而闻名，这是有充分理由的。


每当你升级一个软件包时，旧的软件包不会被删除。只是指向旧包的符号链接将被指向最新的版本。


而这样做，你的系统中会积累下不必要的垃圾。


但是，删除每一个旧世代将违背 NixOS 的初衷。


所以，在这种情况下，你可以将你的系统配置为每周删除垃圾包。


要做到这一点，首先，打开 NixOS 配置文件：



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

```

并在配置文件末尾的 `}` 前添加以下一行：



```
# Automatic Garbage Collection
nix.gc = {
                automatic = true;
                dates = "weekly";
                options = "--delete-older-than 7d";
        };

```

![在 NixOS 中启用自动垃圾收集](/data/attachment/album/202303/26/142515yyutut2yffttyrtt.png)


保存修改并退出 nano 文本编辑器。


为了激活垃圾收集，重建并切换到新的配置文件：



```
sudo nixos-rebuild switch

```

如果你不确定垃圾收集器是否在后台正常运行，你可以用以下命令列出活动的计时器：



```
systemctl list-timers

```

![](/data/attachment/album/202303/26/142515a95e5j4gqem09z54.png)


正如你所看到的，Nix 垃圾收集器正在如期运行，并显示距离下次清理还有 5 天。


### 5、安装你喜欢的软件


我是说这是我们使用电脑的唯一原因 —— “为了使用我们最喜欢的软件”，如果还不是，我们就把它变成现实！


寻找软件包的最好地方是 [Nix 软件包搜索](https://search.nixos.org/packages?ref=its-foss)，可以使用你喜欢的任何浏览器访问。


* 搜索软件包
* 选择软件包
* 点击 “nix-env” 并复制给定的 NixOS 的命令
* 执行该命令，就可以了


你可以查看我们的 [NixOS 软件包管理](/article-15645-1.html) 指南，了解所有的细节。


让我在这里给你一个简单的回顾。例如，在这里，我想安装 Librewolf，所以我采用了以下方法：


![搜索 NixOS 的软件包](/data/attachment/album/202303/26/142516mjbmzqg9vbv8bj30.png)


**但如果你想安装 SSH 或 Plex 等服务，上述方法就不能用了**。


为此，你得查看位于页面顶部的 “<ruby> NixOS 选项 <rt>  NixOS options </rt></ruby>”。


因此，假设我想安装 OpenSSH，那么我必须按照给定的步骤进行：


* 进入 “NixOS 选项”。
* 搜索服务的名称
* 获取服务的名称并将其粘贴到 `configuration.nix` 中，将其值改为 `true`。


![搜索 NixOS 的服务](/data/attachment/album/202303/26/142516e3zf5h5l1p7pswz5.png)



```
services.openssh.enable = true

```

![在 NixOS 上启用 OpenSSH](/data/attachment/album/202303/26/142517fkwj76bpa70zl6zk.png)


在配置文件中加入这一行后，重建服务：



```
sudo nixos-rebuild switch

```

### 6、在 NixOS 中启用自动更新（可选）


一些用户喜欢启用自动更新功能，而另一些用户则可以在他们方便的时候更新软件包。


所以这完全取决于你。


**要启用自动更新**，首先打开 `configuration.nix` 文件：



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

```

接下来，在配置文件的末尾，在 `}` 之前添加以下一行：



```
# Auto system update
system.autoUpgrade = {
      enable = true；
};

```

![在 NixOS 中启用自动系统更新](/data/attachment/album/202303/26/142517jzh759lkxskhev25.png)


保存修改并退出 nano。


要启用自动更新，你必须用以下方法重建并切换到该文件：



```
sudo nixos-rebuild switch

```

你也可以用下面的命令检查 NixOS 的升级定时器：



```
systemctl list-timers

```

![NixOS 的自动升级定时器](/data/attachment/album/202303/26/142517zfsi9lqxiiiiz6zx.png)


正如你所看到的，`nixos-upgrade.service` 正在后台如期运行！


### 7、减少交换度


如果你正在利用交换分区，你可能想减少交换度。


交换度只不过是你想要多积极地使用交换分区（或内存）的数值，其范围从 0 到 100。


交换度越小，你的系统就会越多地使用物理内存（RAM），而交换分区只不过是你的存储驱动器的一部分而已。


另外，存储驱动器的速度相对比内存慢，所以如果可能的话，你应该减少交换度。


要检查你的系统的默认交换度，请使用以下方法：



```
cat /proc/sys/vm/swappiness

```

![检查 Linux 系统的交换度](/data/attachment/album/202303/26/142517uxxeb23x3gx9b8tt.png)


而对于大多数 Linux 发行版，它被设置为 `60`。


我建议你把这个值降低到 `10`。


要做到这一点，首先，用以下命令打开配置文件：



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

```

并在 `}` 之前的行末添加以下一行：



```
boot.kernel.sysctl = { "vm.swappiness" = 10;}；

```

![减少 NixOS 中的交换度](/data/attachment/album/202303/26/142518zq1oxfkl67111qq6.png)


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


现在，重建配置并使用下面的方法切换到它：



```
sudo nixos-rebuild switch

```

现在，你可以再次检查交换度，它应该反映出变化：



```
cat /proc/sys/vm/swappiness

```

![减少 NixOS 中的交换度](/data/attachment/album/202303/26/142519jvox0d0d4x0ek00x.png)


就这些了。


### 总结


如果你在第一次安装 NixOS 后马上遵循这些要点，你应该得到一个良好的用户体验。


当然，根据你的要求，还可以有一些其他的东西。但是，我认为上面提到的事情是最基本或最常见的事情。


在本系列的下一部分，我将讨论在 NixOS 上设置家庭管理器，这对有多个用户的系统应该是有帮助的。


? 安装NixOS后，你首先做什么？让我知道你的想法。




---


via: <https://itsfoss.com/things-to-do-after-installing-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
# Things to do After Installing NixOS

What do you do right after installing NixOS? Clueless? We got your back.

After installation, you will notice that NixOS is quite different from general-purpose Linux distributions.

Of course, as one of the [advanced Linux distributions](https://itsfoss.com/advanced-linux-distros/), it may not feel right at home to most new users.

If you do not know [why you should use NixOS](https://itsfoss.com/why-use-nixos/), and trying it out of curiosity, it is vital to know who it is for before proceeding.

While I assume you installed the distro already, if it is your first time, I suggest [installing NixOS on a virtual machine](https://itsfoss.com/install-nixos-vm/).

## 1. Update packages

Updates would always be there even if you used the latest ISO for the installation. So why not start by updating the packages?

To upgrade packages, first, you will have to check for updates in added channels:

`nix-channel --update`


And then, use the following command to install those updates (if any):

`sudo nixos-rebuild switch --upgrade`


That's it! It will take care of the rest.

## 2. Change hostname in NixOS

If you try the traditional way of [changing the hostname](https://itsfoss.com/change-hostname-ubuntu/) (using the `hostnamectl`

command), it will throw the following error:

![error changing hostname in nixos](https://itsfoss.com/content/images/2023/02/error-changing-hostname-in-nixos.png)

With NixOS, you can change the hostname easily using its main config file, which you can access using the following command:

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


In this config file, look for the following line:

`networking.hostName = "nixos";`


And change it to:

`networking.hostName = "Your_Hostname";`


For example, I changed my hostname to `itsFOSS`

:

`networking.hostName = "itsFOSS";`


![change hostname in NixOS](https://itsfoss.com/content/images/2023/02/change-hostname-in-NixOS.png)

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

To take effect from the change you made to hostname, execute the following command:

`sudo nixos-rebuild switch`


And finally, reopen the terminal, and the change in hostname should reflect. You may have to reboot in recent versions of NixOS.

**Suggested Read 📖**

[Vim vs Nano: What Should You Choose?Vim and Nano are two popular terminal text editors. How are they different? What’s best for you? Let us find out.](https://itsfoss.com/vim-vs-nano/)![](https://itsfoss.com/content/images/wordpress/2022/01/vim-vs-nano.png)


![](https://itsfoss.com/content/images/wordpress/2022/01/vim-vs-nano.png)

## 3. Setup Flatpak

I know what you might be thinking. The Nix package manager already offers a plethora of packages. So, why do you need Flatpak?

Installing what you need could be a bit time-consuming for first-time users. So, Flatpak should make things convenient for you.

[Setting up Flatpak](https://itsfoss.com/flatpak-guide/) is not the same as you do on Ubuntu.

To setup Flatpak, you will have to make changes to the `configuration.nix`

file, which can be accessed using the following:

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


[Go to the end of the line in nano](https://linuxhandbook.com/beginning-end-file-nano/) and add the following line before the `}`

:

`services.flatpak.enable = true;`


![setup flatpak on nixos](https://itsfoss.com/content/images/2023/02/setup-flatpak-on-nixos.png)

Save changes by pressing `Ctrl + O`

, hit enter and exit by `Ctrl + X`

.

Next, rebuild and switch to the new config file using the following command:

`sudo nixos-rebuild switch`


And finally, add the Flathub repository to the system using the following command:

`flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo`


Want to know what exactly is a Flatpak package? You can refer to our article on it:

[What is Flatpak? Everything Important You Need to Know About This Universal Packaging SystemWhat are Flatpak packages? Why is it called a universal packaging system, what problem does it solve and how does it work? Learn about Flatpak.](https://itsfoss.com/what-is-flatpak/)![](https://itsfoss.com/content/images/wordpress/2020/10/what-is-flatpak.png)


![](https://itsfoss.com/content/images/wordpress/2020/10/what-is-flatpak.png)

## 4. Enable garbage collection

NixOS is known for being immutable, and there is a strong reason why.

Whenever you upgrade a package, the old package won't be removed. Just the symlinks of the old package will be given to the latest version.

And doing that, you will collect unnecessary trash from your system.

But removing every old generation will falsify the purpose of NixOS.

So, in that case, you can configure your system to remove garbage packages weekly.

To do that, first, open the nix configuration file:

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


And add the following line at the end of the config file before `}`

:

```
# Automatic Garbage Collection
nix.gc = {
automatic = true;
dates = "weekly";
options = "--delete-older-than 7d";
};
```


![enable automatic garbage collection in NixOS](https://itsfoss.com/content/images/2023/02/enable-automatic-garbage-collection-in-NixOS.png)

Save changes and exit from the nano text editor.

To activate the garbage collection, rebuild and switch to the new config file:

`sudo nixos-rebuild switch`


If you are not sure whether the garbage collector is running fine in the background, you can list active timers using the following command:

`systemctl list-timers`


![](https://itsfoss.com/content/images/2023/02/list-active-timers-in-nixos.png)

And as you can see, the Nix garbage collector is running as expected and shows 5 days left for the next cleanup.

## 5. Install your favorite software

I mean this is the only reason why we use computers. "To use our favorite software," and if there's none, we make it happen!

The best place to look for packages is the [Nix package search](https://search.nixos.org/packages) which can be accessed using any of your preferred browsers.

- Search package
- Select the package
- Click on
`nix-env`

and copy the given command for`NixOS`

- Execute that command, and that's it

You can check our [NixOS package management](https://itsfoss.com/nixos-package-management/) guide to get all the details.

Let me give you a quick recap here. For example, here, I want to install Librewolf, so I went with the following:

![search packages for nixos](https://itsfoss.com/content/images/2023/02/search-packages-for-nixos.png)

**But if you want to install services like SSH or plex, the above method won't work**.

For that, you will have to look into `NixOS options`

situated at the top of the page.

So let's say I want to install OpenSSH, so I have to follow the given steps:

- Go to
`NixOS options`

- Search the name of the service
- Get the name of the service and paste it to the
`configuration.nix`

by changing its value to`true`


![Search the service for nixos](https://itsfoss.com/content/images/2023/02/Search-the-service-for-nixos.png)

`services.openssh.enable = true`


![enable openssh on nixos](https://itsfoss.com/content/images/2023/02/enable-openssh-on-nixos.png)

After adding the line to the config file, rebuild the service:

`sudo nixos-rebuild switch`


## 6. Enable auto-update in NixOS (optional)

Some users prefer to have auto-updates enabled, whereas others can update packages at their convenience.

So it is all up to you.

**To enable auto-update**, first open the `configuration.nix`

file:

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


Next, add the following line at the end of the config file before `}`

:

```
# Auto system update
system.autoUpgrade = {
enable = true;
};
```


![enable auto system update in nixos](https://itsfoss.com/content/images/2023/02/enable-auto-system-update-in-nixos.png)

Save changes and exit from the nano.

To enable the auto-update, you will have to rebuild and switch to that file using the following:

`sudo nixos-rebuild switch`


You can also check the NixOS upgrade timer using the following command:

`systemctl list-timers`


![auto upgrade timer in nixos](https://itsfoss.com/content/images/2023/02/auto-upgrade-timer-in-nixos.png)

And as you can see, the `nixos-upgrade.service`

is running in the background as intended!

## 7. Reduce swapiness

If you are utilizing the swap partition, you may want to reduce the swapiness value.

Swapiness is nothing but the value of how aggressively you want to use the swap partition (or memory), which ranges from 0 to 100.

The lesser the swapiness, the more your system will use the physical memory (RAM), whereas a swap partition is nothing but a bit of part of your storage drive.

Also, storage drives are relatively slower than RAM, so you should reduce the swapiness if possible.

**Suggested Read 📖**

[How Much Swap Should You Use in Linux?How much should be the swap size? Should the swap be double of the RAM size or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Perhaps these are the most common asked questions about choosing](https://itsfoss.com/swap-size/)![](https://itsfoss.com/content/images/wordpress/2018/01/choose-swap-size.jpg)


![](https://itsfoss.com/content/images/wordpress/2018/01/choose-swap-size.jpg)

To check the default swapiness of your system, use the following:

`cat /proc/sys/vm/swappiness`


![check swapiness of linux system](https://itsfoss.com/content/images/2023/02/check-swapiness-of-linux-system.png)

And for most Linux distributions, it is set to `60`

.

I would recommend you lower this value to `10`

.

To do that, first, open the configuration file using the following command:

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


And add the following line at the end of the line before `}`

:

`boot.kernel.sysctl = { "vm.swappiness" = 10;};`


![reduce swapiness in nixos](https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-nixos.png)

Save changes and exit from the text editor.

Now, rebuild the config and switch to it using the following:

`sudo nixos-rebuild switch`


And now, you can check the swapiness again and it should reflect the change:

`cat /proc/sys/vm/swappiness`


![reduce swapiness in NixOS](https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-NixOS.png)

That's it!

## Wrapping Up

If you follow these points right after installing NixOS for the first time, you should get a good user experience.

Sure, there can be a few other things depending on your requirements. But, I think the above-mentioned things are the most essential or common things to do.

For the next part of this series, I shall discuss setting up the home manager on NixOS, which should be helpful for a system with multiple users.

💬 *What do you first do after installing NixOS? Let me know your thoughts.*