Adding programs to PATH: Difference between revisions
m Add details about symlink trees Tags: Mobile edit Mobile web edit Visual edit |
m Axka moved page Package installation to Adding programs to PATH: Better description of page contents Tags: Mobile edit Mobile web edit |
||
(No difference)
| |||
Revision as of 12:12, 8 February 2026
Here is a collection of methods to add a program to the PATH environment variable with Nix (package manager) with packages from Nixpkgs.
The NixOS method supports installing udev rules, which may be required for programs interacting with hardware. Services should be enabled and configured with NixOS options rather than by adding them manually to environment.systemPackages.
Here we are using the package hello as an example. Unless stated otherwise, package attribute names (with necessary prefixes like nixpkgs\# or pkgs.) can be repeated to add more packages.
Persistent
These methods install the packages globally (not local to a specific development shell), persist the installation between reboots and symlink paths together into a specific directory (see the NixOS options environment.systemPackages and environment.pathsToLink for details).
NixOS
Modify your Home Manager configuration to include the meaning of the following example.
{ pkgs, ...} :
{
environment.systemPackages = [ pkgs.hello ];
}
$ nixos-rebuild switch --sudo
Modify your Home Manager configuration to include the meaning of the following example.
{ pkgs, ... }:
{
home.packages = [ pkgs.hello ];
}
nix-env
- Main article: Nix profiles
nix-env -iA hello
nix-env has some pitfalls and is considered deprecated by some: https://stop-using-nix-env.privatevoid.net/nix profile (Flakes)
- Main article: Nix profiles
nix profile install nixpkgs\#hello
Temporary
These installation methods are temporary and local to the specific development shell. Only environment variables are modified; symbolic links to packages will not be created outside the Nix store. As such, applications installed with these methods will not show up in your desktop environment's application menu as their .desktop files are not in the global XDG_DATA_DIRS environment variable's paths.
Ad-hoc shell using nix-shell -p
Run the following command to create a new shell with the hello package from Nixpkgs.
[username@hostname:~]$ nix-shell -p hello
[nix-shell:~]$ hello
Hello, world!
[nix-shell:~]$
nix-shell -p and nix shell. If I remember correctly, the former uses pkgs.mkShell and supports expressions and hooks (i.e. for extra environment variables). Further information may be found in the related discussion page. Please consult the pedia article metapage for guidelines on contributing.
Ad-hoc shell using nix shell (Flakes)
Run the following command to create a new shell with the hello package from the nixpkgs flake added to the PATH environment variable.
[username@hostname:~]$ nix shell nixpkgs\#hello
[username@hostname:~]$ hello
Hello, world!
[username@hostname:~]$
You may also consider the nix run command useful. It reads the package's meta.mainProgram attribute and runs it.
[username@hostname:~]$ nix run nixpkgs\#hello
Hello, world!
[username@hostname:~]$
Declarative shell using shell.nix and nix-shell
Write the file below and run the command nix-shell.
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShellNoCC {
packages = [ pkgs.hello ];
}
Declarative shell using flake.nix and nix develop (Flakes)
- Main article: Flakes
Below is a minimal example using flakes. Write the file below and run the command nix develop.
{
description = "Minimal development shell using flakes";
# Define Nixpkgs as a flake input
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { nixpkgs, ... }: let
# Get the Nixpkgs package set for the system x86_64-linux
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in {
# Define a development shell for the system x86_64-linux
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
packages = [ pkgs.hello ];
};
};
}
Here is an example of a it working:
[username@hostname:/tmp/example]$ hello
-bash: hello: command not found
[username@hostname:/tmp/example]$ nix develop
warning: creating lock file "/tmp/example/flake.lock":
• Added input 'nixpkgs':
'github:NixOS/nixpkgs/00c21e4c93d963c50d4c0c89bfa84ed6e0694df2?narHash=sha256-AYqlWrX09%2BHvGs8zM6ebZ1pwUqjkfpnv8mewYwAo%2BiM%3D' (2026-02-04)
[username@hostname:/tmp/example]$ hello
Hello, world!
[username@hostname:/tmp/example]$