Jump to content

Adding programs to PATH: Difference between revisions

From Official NixOS Wiki
Axka (talk | contribs)
m Put commands into code blocks
Tags: Mobile edit Mobile web edit Visual edit
Axka (talk | contribs)
m Put commands into code blocks
Tags: Mobile edit Mobile web edit Visual edit
Line 39: Line 39:


=== 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


<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:~]$ hello
Hello, world!
 
[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) ===

Revision as of 11:39, 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.

❄︎ /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:~]$

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 ];
    }
  };
}