Adding programs to PATH: Difference between revisions
Add initial version of guide page for adding packages to PATH. Tags: Mobile edit Mobile web edit Visual edit |
m Clarify udev paragraph Tags: Mobile edit Mobile web edit Visual edit |
||
| Line 1: | Line 1: | ||
Here is a collection of methods to add a program to the <code>PATH</code> environment variable with [[Nix (package manager)]] with packages from [[Nixpkgs]]. | Here is a collection of methods to add a program to the <code>PATH</code> 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 <code>environment.systemPackages</code>. | |||
Here we are using the package <code>hello</code> as an example. Unless stated otherwise, package attribute names (with necessary prefixes like <code>nixpkgs\#</code> or <code>pkgs.</code>) can be repeated to add more packages. | Here we are using the package <code>hello</code> as an example. Unless stated otherwise, package attribute names (with necessary prefixes like <code>nixpkgs\#</code> or <code>pkgs.</code>) can be repeated to add more packages. | ||
Revision as of 11:26, 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 link directories together.
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.
nix-shell -p hello
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.
nix shell nixpkgs\#hello
You can also use packages from a local flake with the following command:
nix shell .\#my-package
You may also consider the nix run command useful. It reads the package's meta.mainProgram attribute and runs it.
nix run nixpkgs\#hello
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. See the main article for more information. Write the file below and run the command nix develop.
{
description = "Minimal development shell using flakes";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { nixpkgs, ... }: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in {
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
packages = [ pkgs.hello ];
}
};
}