Merge pull request #28967 from wxy/20230313.2-️-NixOS-Series-4-Things-To-Do-After-Installing-NixOS

ATRP:published/20230313.2 ️ NixOS Series 4 Things To Do After Installing NixOS.md
This commit is contained in:
Xingyu.Wang 2023-03-26 14:32:12 +08:00 committed by GitHub
commit 572028ad4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 354 additions and 348 deletions

View File

@ -0,0 +1,354 @@
[#]: subject: "NixOS Series #4: Things To Do After Installing NixOS"
[#]: via: "https://itsfoss.com/things-to-do-after-installing-nixos/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "wxy"
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-15663-1.html"
NixOS 系列 #4:安装 NixOS 后要做的事
======
![][0]
安装之后,你会发现 NixOS 与通用的 Linux 发行版有很大的不同。
当然,作为 [高级 Linux 发行版][1]之一,大多数新用户对它并不熟悉。
如果你不知道 [为什么你应该使用 NixOS][2],并且出于好奇而尝试它,那么在继续进行之前,知道它是为谁准备的至关重要。
虽然我假设你已经安装了这个发行版,但如果你是第一次使用,我建议先 [在虚拟机上安装 NixOS][3]。
### 1、更新软件包
即使你使用最新的 ISO 进行安装,也总是会有更新的。那么为什么不从更新软件包开始呢?
要升级软件包,首先,你必须在添加的频道中检查是否有更新:
```
nix-channel --update
```
然后,使用下面的命令来安装这些更新(如果有的话):
```
sudo nixos-rebuild switch --upgrade
```
这就行了!它将处理其余的事情。
### 2、在 NixOS 中改变主机名
如果你尝试用传统的方法 [改变主机名][4](使用 `hostnamectl` 命令),会出现以下错误:
![在 NixOS 中改变主机名的错误][5]
在 NixOS 中,你可以用它的主配置文件轻松地改变主机名。使用如下命令:
```
sudo nano /etc/nixos/configuration.nix
```
在这个配置文件中,寻找以下一行:
```
networking.hostName = "nixos"
```
然后把它改成:
```
networking.hostName = "Your_Hostname"
```
例如,我把我的主机名改为 `itsFOSS`
```
networking.hostName = "itsFOSS"
```
![在 NixOS 中改变主机名][6]
现在,[保存更改并退出 nano][7] 文本编辑器。
为了使你对主机名的修改生效,执行以下命令:
```
sudo nixos-rebuild switch
```
最后,重新打开终端,主机名的变化应该反映出来。
### 3、设置 Flatpak
我知道你可能在想什么。Nix 软件包管理器已经提供了大量的软件包。那么,为什么你需要 Flatpak 呢?
安装你所需要的东西对于第一次使用的人来说可能有点费时。所以Flatpak 应该能给你带来方便。
[设置 Flatpak][8] 与你在 Ubuntu 上做的不一样。
要设置 Flatpak你必须对 `configuration.nix` 文件进行修改,可以通过以下方式访问该文件:
```
sudo nano /etc/nixos/configuration.nix
```
[在 nano 中移动到最后一行][9],在 `}` 前添加以下一行:
```
services.flatpak.enable = true
```
![在 NixOS 上设置 Flatpak][10]
`Ctrl + O` 保存更改,按回车键,按 `Ctrl + X` 退出。
接下来,使用以下命令重建并切换到新的配置文件:
```
sudo nixos-rebuild switch
```
最后,使用下面的命令将 Flathub 软件库添加到系统中:
```
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
```
想知道到底什么是 Flatpak 包吗?你可以参考我们关于它的文章:
> **[什么是 Flatpak你需要知道的关于这个通用包装系统的一切重要信息][23]**
### 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 中启用自动垃圾收集][11]
保存修改并退出 nano 文本编辑器。
为了激活垃圾收集,重建并切换到新的配置文件:
```
sudo nixos-rebuild switch
```
如果你不确定垃圾收集器是否在后台正常运行,你可以用以下命令列出活动的计时器:
```
systemctl list-timers
```
![][12]
正如你所看到的Nix 垃圾收集器正在如期运行,并显示距离下次清理还有 5 天。
### 5、安装你喜欢的软件
我是说这是我们使用电脑的唯一原因 —— “为了使用我们最喜欢的软件”,如果还不是,我们就把它变成现实!
寻找软件包的最好地方是 [Nix 软件包搜索][13],可以使用你喜欢的任何浏览器访问。
- 搜索软件包
- 选择软件包
- 点击 “nix-env” 并复制给定的 NixOS 的命令
- 执行该命令,就可以了
你可以查看我们的 [NixOS 软件包管理][14] 指南,了解所有的细节。
让我在这里给你一个简单的回顾。例如,在这里,我想安装 Librewolf所以我采用了以下方法
![搜索 NixOS 的软件包][15]
**但如果你想安装 SSH 或 Plex 等服务,上述方法就不能用了**。
为此,你得查看位于页面顶部的 “<ruby>NixOS 选项<rt>NixOS options</rt></ruby>”。
因此,假设我想安装 OpenSSH那么我必须按照给定的步骤进行
- 进入 “NixOS 选项”。
- 搜索服务的名称
- 获取服务的名称并将其粘贴到 `configuration.nix` 中,将其值改为 `true`
![搜索 NixOS 的服务][16]
```
services.openssh.enable = true
```
![在 NixOS 上启用 OpenSSH][17]
在配置文件中加入这一行后,重建服务:
```
sudo nixos-rebuild switch
```
### 6、在 NixOS 中启用自动更新(可选)
一些用户喜欢启用自动更新功能,而另一些用户则可以在他们方便的时候更新软件包。
所以这完全取决于你。
**要启用自动更新**,首先打开 `configuration.nix` 文件:
```
sudo nano /etc/nixos/configuration.nix
```
接下来,在配置文件的末尾,在 `}` 之前添加以下一行:
```
# Auto system update
system.autoUpgrade = {
enable = true
};
```
![在 NixOS 中启用自动系统更新][18]
保存修改并退出 nano。
要启用自动更新,你必须用以下方法重建并切换到该文件:
```
sudo nixos-rebuild switch
```
你也可以用下面的命令检查 NixOS 的升级定时器:
```
systemctl list-timers
```
![NixOS 的自动升级定时器][19]
正如你所看到的,`nixos-upgrade.service` 正在后台如期运行!
### 7、减少交换度
如果你正在利用交换分区,你可能想减少交换度。
交换度只不过是你想要多积极地使用交换分区(或内存)的数值,其范围从 0 到 100。
交换度越小你的系统就会越多地使用物理内存RAM而交换分区只不过是你的存储驱动器的一部分而已。
另外,存储驱动器的速度相对比内存慢,所以如果可能的话,你应该减少交换度。
要检查你的系统的默认交换度,请使用以下方法:
```
cat /proc/sys/vm/swappiness
```
![检查 Linux 系统的交换度][20]
而对于大多数 Linux 发行版,它被设置为 `60`
我建议你把这个值降低到 `10`
要做到这一点,首先,用以下命令打开配置文件:
```
sudo nano /etc/nixos/hardware-configuration.nix
```
并在 `}` 之前的行末添加以下一行:
```
boot.kernel.sysctl = { "vm.swappiness" = 10;}
```
![减少 NixOS 中的交换度][21]
保存修改并退出文本编辑器。
现在,重建配置并使用下面的方法切换到它:
```
sudo nixos-rebuild switch
```
现在,你可以再次检查交换度,它应该反映出变化:
```
cat /proc/sys/vm/swappiness
```
![减少 NixOS 中的交换度][22]
就这些了。
### 总结
如果你在第一次安装 NixOS 后马上遵循这些要点,你应该得到一个良好的用户体验。
当然,根据你的要求,还可以有一些其他的东西。但是,我认为上面提到的事情是最基本或最常见的事情。
在本系列的下一部分,我将讨论在 NixOS 上设置家庭管理器,这对有多个用户的系统应该是有帮助的。
💬 安装NixOS后你首先做什么让我知道你的想法。
--------------------------------------------------------------------------------
via: https://itsfoss.com/things-to-do-after-installing-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://itsfoss.com/advanced-linux-distros/
[2]: https://linux.cn/article-15606-1.html
[3]: https://linux.cn/article-15624-1.html
[4]: https://itsfoss.com/change-hostname-ubuntu/
[5]: https://itsfoss.com/content/images/2023/02/error-changing-hostname-in-nixos.png
[6]: https://itsfoss.com/content/images/2023/02/change-hostname-in-NixOS.png
[7]: https://linuxhandbook.com/nano-save-exit/?ref=its-foss
[8]: https://itsfoss.com/flatpak-guide/
[9]: https://linuxhandbook.com/beginning-end-file-nano/?ref=its-foss
[10]: https://itsfoss.com/content/images/2023/02/setup-flatpak-on-nixos.png
[11]: https://itsfoss.com/content/images/2023/02/enable-automatic-garbage-collection-in-NixOS.png
[12]: https://itsfoss.com/content/images/2023/02/list-active-timers-in-nixos.png
[13]: https://search.nixos.org/packages?ref=its-foss
[14]: https://linux.cn/article-15645-1.html
[15]: https://itsfoss.com/content/images/2023/02/search-packages-for-nixos.png
[16]: https://itsfoss.com/content/images/2023/02/Search-the-service-for-nixos.png
[17]: https://itsfoss.com/content/images/2023/02/enable-openssh-on-nixos.png
[18]: https://itsfoss.com/content/images/2023/02/enable-auto-system-update-in-nixos.png
[19]: https://itsfoss.com/content/images/2023/02/auto-upgrade-timer-in-nixos.png
[20]: https://itsfoss.com/content/images/2023/02/check-swapiness-of-linux-system.png
[21]: https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-nixos.png
[22]: https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-NixOS.png
[23]: https://itsfoss.com/what-is-flatpak/
[0]: https://img.linux.net.cn/data/attachment/album/202303/26/142500uzc095szl064s9xx.jpg

View File

@ -1,348 +0,0 @@
[#]: subject: "NixOS Series #4: Things To Do After Installing NixOS"
[#]: via: "https://itsfoss.com/things-to-do-after-installing-nixos/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
NixOS Series #4: Things To Do After Installing NixOS
======
After installation, you will notice that NixOS is quite different from general-purpose Linux distributions.
Of course, as one of the [advanced Linux distributions][1], it may not feel right at home to most new users.
If you do not know [why you should use NixOS][2], 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][3].
### 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][4] (using the `hostnamectl` command), it will throw the following error:
![error changing hostname in nixos][5]
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][6]
Now, [save changes and exit from the nano][7] 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.
### 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][8] 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][9] and add the following line before the `}`:
```
services.flatpak.enable = true;
```
![setup flatpak on nixos][10]
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:
### 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][11]
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
```
![][12]
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][13] 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][14] 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][15]
**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][16]
```
services.openssh.enable = true
```
![enable openssh on nixos][17]
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][18]
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][19]
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.
To check the default swapiness of your system, use the following:
```
cat /proc/sys/vm/swappiness
```
![check swapiness of linux system][20]
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][21]
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][22]
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._
--------------------------------------------------------------------------------
via: https://itsfoss.com/things-to-do-after-installing-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/advanced-linux-distros/
[2]: https://itsfoss.com/why-use-nixos/
[3]: https://itsfoss.com/install-nixos-vm/
[4]: https://itsfoss.com/change-hostname-ubuntu/
[5]: https://itsfoss.com/content/images/2023/02/error-changing-hostname-in-nixos.png
[6]: https://itsfoss.com/content/images/2023/02/change-hostname-in-NixOS.png
[7]: https://linuxhandbook.com/nano-save-exit/?ref=its-foss
[8]: https://itsfoss.com/flatpak-guide/
[9]: https://linuxhandbook.com/beginning-end-file-nano/?ref=its-foss
[10]: https://itsfoss.com/content/images/2023/02/setup-flatpak-on-nixos.png
[11]: https://itsfoss.com/content/images/2023/02/enable-automatic-garbage-collection-in-NixOS.png
[12]: https://itsfoss.com/content/images/2023/02/list-active-timers-in-nixos.png
[13]: https://search.nixos.org/packages?ref=its-foss
[14]: https://itsfoss.com/nixos-package-management/
[15]: https://itsfoss.com/content/images/2023/02/search-packages-for-nixos.png
[16]: https://itsfoss.com/content/images/2023/02/Search-the-service-for-nixos.png
[17]: https://itsfoss.com/content/images/2023/02/enable-openssh-on-nixos.png
[18]: https://itsfoss.com/content/images/2023/02/enable-auto-system-update-in-nixos.png
[19]: https://itsfoss.com/content/images/2023/02/auto-upgrade-timer-in-nixos.png
[20]: https://itsfoss.com/content/images/2023/02/check-swapiness-of-linux-system.png
[21]: https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-nixos.png
[22]: https://itsfoss.com/content/images/2023/02/reduce-swapiness-in-NixOS.png