TranslateProject/sources/tech/20230306.3 ⭐️ NixOS Series 3 Install and Remove Packages in NixOS.md

291 lines
9.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[#]: 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