LinuxCommandLibrary

nix-run

Run applications in isolated Nix environments

TLDR

Run the default application in the flake in the current directory

$ nix run
copy

Run a command whose name matches the package name from nixpkgs (if you want a different command from that package, see tldr nix shell)
$ nix run nixpkgs#[pkg]
copy

Run a command with provided arguments
$ nix run nixpkgs#[vim] -- [path/to/file]
copy

Run from a remote repository
$ nix run [remote_name]:[owner]/[repo]
copy

Run from a remote repository using a specific tag, revision or branch
$ nix run [remote_name]:[owner]/[repo]/[reference]
copy

Run from a remote repository specifying a subdirectory and a program
$ nix run "[remote_name]:[owner]/[repo]?dir=[dir_name]#[app]"
copy

Run the flake of a GitHub pull request
$ nix run github:[owner]/[repo]/pull/[number]/head
copy

SYNOPSIS

nix run [options] expression

PARAMETERS

--expr
    Evaluate and run its result as a command. This is equivalent to passing the expression directly as an argument.


    A Nix expression to evaluate. It can be a string containing Nix code or a path to a Nix file.

-f, --file
    Load a Nix expression from . Can point to a file in the local directory or to an attribute set containing default values.

--impure
    Allow access to mutable paths such as /etc, /var, and /tmp.

--option
    Set the Nix configuration setting to .

-v, --verbose
    Increase verbosity level.

--help
    Show help message.

DESCRIPTION

The nix-run command allows you to execute Nix expressions or pre-defined commands within a Nix environment. It's a powerful tool for quickly testing Nix code, running scripts, and managing dependencies without permanently modifying your system. The command facilitates isolated builds by creating a temporary Nix environment for each execution. This guarantees reproducibility and prevents interference with other parts of your system.
It supports running simple Nix expressions directly on the command line or executing commands defined in Nix files or packages. nix-run can also be used to execute commands from Git repositories, URLs, or local paths. The ability to specify input sources and environment variables makes it highly customizable. The command offers a convenient way to use software packages managed by Nix without explicitly installing them globally. It's particularly useful for development, testing, and continuous integration workflows, providing a consistent and isolated environment for running your code.

CAVEATS

The exact behavior of nix-run can vary slightly depending on the Nix version. Running impure code can compromise reproducibility. Security precautions should be considered while using --impure flag.

EXAMPLES

nix run 'pkgs.hello': Runs the 'hello' package.
nix run ./my-script.nix: Runs the script defined in 'my-script.nix'.
nix run 'with import {}; bash -c "echo Hello"': Runs a bash command within the Nix environment.

HISTORY

nix-run emerged as part of the Nix package manager, aiming to simplify the execution of Nix expressions and commands. It builds upon Nix's core principles of reproducibility and isolation, providing a convenient way to execute code in a consistent environment.

SEE ALSO

nix-build(1), nix-shell(1), nix(1)

Copied to clipboard