How to use the command 'nix profile' (with examples)
The ’nix profile’ command is used to install, update, and remove packages from Nix profiles. It provides a way to manage packages within a profile, allowing users to easily control the packages installed in their Nix environments.
Use case 1: Install some packages from nixpkgs into the default profile
Code:
nix profile install nixpkgs#pkg1 nixpkgs#pkg2 ...
Motivation: This use case is useful when you want to install specific packages from the nixpkgs collection into the default profile. By specifying the package names after ’nixpkgs#’, you can easily install multiple packages at once.
Explanation: The ’nix profile install’ command is used to install packages into a profile. In this use case, we are installing packages from the nixpkgs collection, which is referenced by ’nixpkgs#’. The package names are provided after the ‘#’, and multiple packages can be specified separated by space.
Example output:
installing 'pkg1' into the default profile...
installing 'pkg2' into the default profile...
Use case 2: Install a package from a flake on GitHub into a custom profile
Code:
nix profile install github:owner/repo/pkg --profile ./path/to/directory
Motivation: This use case is useful when you want to install a package from a flake hosted on GitHub into a custom profile. By specifying the GitHub repository and package name, you can easily install the desired package. Additionally, you can specify a custom profile location using the ‘–profile’ option.
Explanation: The ’nix profile install’ command is used to install packages into a profile. In this use case, we are installing a package from a GitHub repository by specifying ‘github:owner/repo/pkg’. The ‘–profile’ option is used to specify a custom profile location, which will override the default profile path.
Example output:
installing 'pkg' from 'github:owner/repo' into the custom profile at './path/to/directory'...
Use case 3: List packages currently installed in the default profile
Code:
nix profile list
Motivation: This use case is useful when you want to get a list of packages currently installed in the default profile. It allows you to check which packages are present in your environment and verify the installation status.
Explanation: The ’nix profile list’ command is used to list packages installed in a profile. When executed without any additional arguments, it lists the packages in the default profile.
Example output:
pkg1
pkg2
pkg3
Use case 4: Remove a package installed from nixpkgs from the default profile, by name
Code:
nix profile remove legacyPackages.x86_64-linux.pkg
Motivation: This use case is useful when you want to remove a specific package installed from nixpkgs in the default profile. It allows you to uninstall individual packages without affecting the rest of your environment.
Explanation: The ’nix profile remove’ command is used to remove packages from a profile. In this use case, we are removing the package ‘pkg’ from the default profile, which is located in the ’legacyPackages.x86_64-linux’ namespace.
Example output:
removing 'pkg' from the default profile...
Use case 5: Upgrade packages in the default to the latest available versions
Code:
nix profile upgrade
Motivation: This use case is useful when you want to upgrade packages in the default profile to their latest available versions. It ensures that you have the most up-to-date packages installed in your environment.
Explanation: The ’nix profile upgrade’ command is used to upgrade packages in a profile. When executed without any additional arguments, it upgrades packages in the default profile.
Example output:
upgrading packages in the default profile...
pkg1 upgraded to version 2.0
pkg2 upgraded to version 1.5
Use case 6: Rollback (cancel) the latest action on the default profile
Code:
nix profile rollback
Motivation: This use case is useful when you want to undo the latest action performed on the default profile. It allows you to revert back to the previous state of the profile and undo any changes made.
Explanation: The ’nix profile rollback’ command is used to rollback the latest action on a profile. When executed without any additional arguments, it rolls back the latest action on the default profile.
Example output:
rolling back the latest action on the default profile...
Conclusion:
The ’nix profile’ command provides a comprehensive set of functionalities for managing packages within Nix profiles. With the ability to install, update, remove, list, upgrade, and rollback packages, users have full control over their Nix environments. By following the examples provided in this article, you can easily utilize the ’nix profile’ command for various use cases and efficiently manage your packages.