Jump to content

Adding programs to PATH: Difference between revisions

From Official NixOS Wiki
Axka (talk | contribs)
m Clarify flake devshell section
Tags: Mobile edit Mobile web edit Visual edit
Axka (talk | contribs)
Explain PATH
Tags: Mobile edit Mobile web edit Visual edit
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Tutorial]]
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 <code>PATH</code> environment variable (often referred to as "the PATH") is an environment variable that tells programs where to find other programs. When you run a command <code>hello</code> in a bash shell, it basically searches for an executable named <code>hello</code> in every directory listed in <code>PATH</code>.


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>.
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>.
Line 7: Line 11:
== Persistent ==
== Persistent ==


These methods install the packages globally (not local to a specific development shell), persist the installation between reboots and link directories together.
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 {{Nixos:option|environment.systemPackages}} and {{Nixos:option|environment.pathsToLink}} for details).


=== NixOS ===
=== NixOS ===
Line 39: Line 43:


=== Ad-hoc shell using <code>nix-shell -p</code> ===
=== Ad-hoc shell using <code>nix-shell -p</code> ===
Run the following command to create a new shell with the <code>hello</code> package from Nixpkgs.
Run the following command to create a new shell with the <code>hello</code> package from Nixpkgs.<syntaxhighlight lang="shell-session">
[username@hostname:~]$ nix-shell -p hello
 
[nix-shell:~]$ hello
Hello, world!


<code>nix-shell -p hello</code>{{Expand|reason=Compare <code>nix-shell -p</code> and <code>nix shell</code>. If I remember correctly, the former uses <code>pkgs.mkShell</code> and supports expressions and hooks (i.e. for extra environment variables).}}
[nix-shell:~]$
</syntaxhighlight>{{Expand|reason=Compare <code>nix-shell -p</code> and <code>nix shell</code>. If I remember correctly, the former uses <code>pkgs.mkShell</code> and supports expressions and hooks (i.e. for extra environment variables).}}


=== Ad-hoc shell using <code>nix shell</code> (Flakes) ===
=== Ad-hoc shell using <code>nix shell</code> (Flakes) ===


Run the following command to create a new shell with the <code>hello</code> package from the <code>nixpkgs</code> [[Flakes|flake]] added to the <code>PATH</code> environment variable.
Run the following command to create a new shell with the <code>hello</code> package from the <code>nixpkgs</code> [[Flakes|flake]] added to the <code>PATH</code> environment variable.<syntaxhighlight lang="shell-session">
[username@hostname:~]$ nix shell nixpkgs\#hello


<code>nix shell nixpkgs\#hello</code>
[username@hostname:~]$ hello
Hello, world!


You can also use packages from a local flake with the following command:
[username@hostname:~]$
</syntaxhighlight>You may also consider the <code>nix run</code> command useful. It reads the package's <code>meta.mainProgram</code> attribute and runs it.<syntaxhighlight lang="shell-session">
[username@hostname:~]$ nix run nixpkgs\#hello
Hello, world!


<code>nix shell .\#my-package</code>
[username@hostname:~]$
 
</syntaxhighlight>
You may also consider the <code>nix run</code> command useful. It reads the package's <code>meta.mainProgram</code> attribute and runs it.
 
<code>nix run nixpkgs\#hello</code>


=== Declarative shell using <code>shell.nix</code> and <code>nix-shell</code> ===
=== Declarative shell using <code>shell.nix</code> and <code>nix-shell</code> ===
Line 78: Line 89:
     devShells.x86_64-linux.default = pkgs.mkShellNoCC {
     devShells.x86_64-linux.default = pkgs.mkShellNoCC {
       packages = [ pkgs.hello ];
       packages = [ pkgs.hello ];
     }
     };
   };
   };
}|name=flake.nix|lang=nix}}
}|name=flake.nix|lang=nix}}Here is an example of a it working:<syntaxhighlight lang="shell-session">
[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]$
</syntaxhighlight>

Latest revision as of 13:10, 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 PATH environment variable (often referred to as "the PATH") is an environment variable that tells programs where to find other programs. When you run a command hello in a bash shell, it basically searches for an executable named hello in every directory listed in PATH.

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.

❄︎ /etc/nixos/configuration.nix
{ pkgs, ...} :
{
  environment.systemPackages = [ pkgs.hello ];
}
🟆︎
Tip: In order to affect your NixOS system by your nix-language-specific changes you must first evaluate it:
$ nixos-rebuild switch --sudo


Modify your Home Manager configuration to include the meaning of the following example.

❄︎ ~/.config/home-manager/home.nix
{ pkgs, ... }:
{
  home.packages = [ pkgs.hello ];
}

nix-env

Main article: Nix profiles

nix-env -iA hello

⚠︎
Warning: 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:~]$
☶︎
This article or section needs to be expanded. Compare 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.

❄︎ shell.nix
{ 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.

❄︎ flake.nix
{
  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]$