Searching for a Package

There are several ways to search for packages in NixOS (each way may return more results or information):

nix search <package-name>, nix-env --query <package-name>, or nix-env -qa '<package-name>

You can also visit a web-based interface on NixOS’ site to search for the a package.

Installing a Package

Installing a Package via nix-env (user-wide)

To install a package just in your regular user environment, you can use nix-env -i <packange-name>.

You can also pretend to install adding the --dry-run parameter at the end of any operation of nix-env.

To see a list of installed packages that were installed via nix-env run:

nix-env --query --installed

Installing a Package via Configuration.nix (system-wide)

To add additional package to be install system-wide (this can also be done at install time), look for the following lines and add additional packages separated each by a space:

environment.systemPackages = with pkgs; [
    wget vim nano zsh file
];

Now you have to rebuild the environment. You can either run sudo nixos-rebuild to have it rebuild and not switch your current operating environment to the changes or sudo nixos-rebuild switch to have it switch automatically. When you reboot your PC, you will be able to pick previous builds, so if it broke something, you can go back in time.

Installing a Package via config.nix (user-wide)

Create the following folder and file: ~/.nixpkgs/config.nix. We can define multiple groups of packages that can be installed all at once with just one command. Put the following in to this new file:

{ pkgs }:
let
    inherit (pkgs) lib buildEnv;

in {
    allowUnfree = true; # allow non-free, non-open source packages to install

    # if you are using Chromium, this is likely something you want to enable an option for
    chromium = {
        enableWideVine = true; # enable the DRM to allow things like Netflix to work
    };

    packageOverrides = pkgs: {
        desktopDev = lib.lowPrio (buildEnv {
        name = "devEnvApps";
        paths = with pkgs; [
            vscode
            elixir
            nodejs
            git
            file
            unzip
        ];
        });
        kdeApplications = lib.lowPrio (buildEnv {
        name = "kdeApps";
        
        paths = with pkgs; [
            latte-dock
            yakuake
            kate
            kdeconnect
            okular
            ark
        ];
        });
    };
}

In order to install one of these groups of applications, you just need to install using the name = "" one, for example, to install the KDE group, nix-env -i kdeApps. The other part that is unique like the kdeApplications doesn’t matter as much, just has to be unique in this instance.

Uninstalling a Package

Uninstalling System-Wide

To uninstall a package, remove it from the pkg list in the configuration.nix file:

environment.systemPackages = with pkgs; [
    wget vim nano zsh file
];

Just like when you add a new pacakge, you will now you have to rebuild the environment. You can either run sudo nixos-rebuild to have it rebuild and not switch your current operating environment to the changes or sudo nixos-rebuild switch to have it switch automatically.

Uninstalling User-Wide

To uninstall a package, run:

nix-env --uninstall <package-name>

Enabling Flatpak

To enable flatpak add the following to your configuration.nix file (first line needed if not doing Gnome):

xdg.portal.enable = true; # only needed if you are not doing Gnome
services.flatpak.enable = true;

After adding anything into the configuration.nix file like more packages, you need to rebuild NixOS:

You can either run sudo nixos-rebuild to have it rebuild and not switch your current operating environment to the changes or sudo nixos-rebuild switch to have it switch automatically.

Once flatpak is installed, you need to add the usual flathub repo:

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

Updating NixOS

Updating System-Wide

To update NixOS, you need to update the channel (nix-channel --update), to force a rebuild of the packages defined in your configuration.nix, you would have to do use the nixos-rebuild command. You can update both the channel and rebuild things in one command, nixos-rebuild --upgrade switch. If your update broke things, you can roll back with nix-channel --rollback and rebuild again.

Updating User-Wide

To update your packages, run the same nix-channel --update as in a system-wide update and then run the following command: nix-env -u.

Switching to the Unstable NixPkgs Repository

Switching System-Wide

Do this under root or as sudo:

nix-channel --add https://nixos.org/channels/nixos-unstable nixos

As with making changes to configuration.nix, you will want to do your nixos-rebuild commands to get it current.

Switching User-Wide

Do this under your normal account and not as root or sudo:

Remove nixos channel (stable):

nix-channel --remove stable

Now any applications you install will be done from the unstable repo. Do a nix-env -u to get current. If you get a warning about a name conflict, you may have to remove the old channel:

nix-channel --add https://nixos.org/channels/nixos-unstable nixos

Then you need to re-add it as above. Only one channel can be named nixos at a time.

Install Specific Packages from Unstable

You can manually clone the github repo that contains nixos-unstable. This will allow you to install some applications that are from unstable without having to install all of your packages from unstable. To clone it:

git clone https://github.com/NixOS/nixpkgs.git ~/nixpkgs-unstable

To install a package from the cloned Unstable NixPkgs repo where ~/nixpkgs-unstable/ is the location of the cloned repo:

nix-env -iA <packagename> -f ~/nixpkgs-unstable/

Also, be sure to do a git pull on the cloned repo before doing operations so it is current.

Cleanup Old Versions of Packages

Cleanup Old Packages (user-wide)

To remove all old packages but the current one, run this:

nix-env --delete-generations old

You can delete based on different generations (like generation 10, 11, 14):

nix-env --delete-generations 10 11 14

Or you can delete generations based on how old they are (14 days in this example):

nix-env --delete-generations 14d

Then run the garbage collector to delete them:

nix-store --gc

To see what will be deleted:

nix-store --gc --print-dead

Cleanup Old Packages (all users)

To cleanup the garbage collection on all users:

nix-collect-garbage -d

Useful References

Tags:

Updated: