Adding programs to PATH: Difference between revisions

Axka (talk | contribs)
m Clarify udev paragraph
Tags: Mobile edit Mobile web edit Visual edit
Axka (talk | contribs)
Explain PATH
Tags: Mobile edit Mobile web edit Visual edit
 
(8 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 65: Line 76:
=== Declarative shell using <code>flake.nix</code> and <code>nix develop</code> (Flakes) ===
=== Declarative shell using <code>flake.nix</code> and <code>nix develop</code> (Flakes) ===
{{Main|Flakes}}
{{Main|Flakes}}
Below is a minimal example using flakes. See the main article for more information. Write the file below and run the command <code>nix develop</code>.{{File|3={
Below is a minimal example using flakes. Write the file below and run the command <code>nix develop</code>.{{File|3={
   description = "Minimal development shell using flakes";
   description = "Minimal development shell using flakes";


  # Define Nixpkgs as a flake input
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
   inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";


outputs = { nixpkgs, ... }: let
outputs = { nixpkgs, ... }: let
    # Get the Nixpkgs package set for the system x86_64-linux
     pkgs = nixpkgs.legacyPackages.x86_64-linux;
     pkgs = nixpkgs.legacyPackages.x86_64-linux;
   in {
   in {
    # Define a development shell for the system x86_64-linux
     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>