LinuxCommandLibrary

nix3-edit

Edit Nix packages and configurations

TLDR

Open the source of the Nix expression of a package from nixpkgs in your $EDITOR

$ nix edit [nixpkgs#pkg]
copy

Dump the source of a package to stdout
$ EDITOR=cat nix edit [nixpkgs#pkg]
copy

SYNOPSIS

nix edit [common-options] {<flake-ref> | <flake-ref>#<attrpath>}
nix edit [common-options] --expr <nix-expression>
nix edit [common-options] --file <path>

PARAMETERS

--expr <nix-expression>
    Allows editing of a Nix expression provided directly as a string, rather than from a file or flake attribute. The string is written to a temporary file for editing.

--file <path>
    Specifies a local file to be edited instead of a flake attribute. This is useful for editing arbitrary Nix files.

--apply
    After the editor exits, the modified expression is evaluated and applied. This typically means the associated derivation will be rebuilt or the expression re-evaluated based on the changes.

--json
    If specified, the output (especially when combined with --apply for evaluation results) is formatted as JSON. Its direct utility for merely editing is limited.

--color <when>
    Control whether Nix uses colored output. <when> can be auto (default), always, or never.

--log-format <format>
    Set the format of log output for operations like evaluation or building. <format> can be raw, internal-json, bar, bar-with-logs, or json.

--quiet
    Decrease verbosity of the output. Suppresses informational messages.

--verbose
    Increase verbosity of the output. Shows more detailed logs and progress.

--help
    Show help message for the nix edit command, detailing its usage and options.

DESCRIPTION

nix edit is a convenience command in the Nix 3.x CLI that allows users to easily inspect and modify Nix expressions. It takes a flake URI or an attribute path within a flake, locates the corresponding Nix expression, and opens it in the user's default editor (defined by the $EDITOR environment variable).

This is particularly useful for debugging, temporarily patching derivations, or quickly experimenting with changes to a package's definition without manually navigating the Nix store or source tree. After editing, the --apply option can be used to re-evaluate and apply the changes.

CAVEATS

Changes made using nix edit are temporary and only applied locally within the Nix store. They are not saved to the original source location of a package or flake unless you explicitly copy the edited content back or commit it.

Using --apply can trigger significant re-evaluation and potentially rebuilding processes, which might consume considerable time and resources depending on the complexity and size of the modified expression. Ensure your $EDITOR environment variable is configured correctly, otherwise the command may fail or open a default editor you are not familiar with.

ENVIRONMENT VARIABLE

The $EDITOR environment variable dictates which text editor nix edit will launch. If this variable is not set, Nix typically falls back to common system editors like vi or nano, or it may produce an error. Setting it to your preferred editor (e.g., EDITOR=nvim) ensures a consistent editing experience.

USAGE CONTEXT

This command is invaluable for rapid iteration and debugging in Nix development. It allows developers to quickly prototype changes to a package's build instructions, apply temporary patches to a derivation, or explore the structure of a Nix expression without needing to modify the original source tree. The combination with --apply provides immediate feedback on changes.

HISTORY

nix edit was introduced as part of the Nix 3.0 CLI re-design, which aimed to unify and simplify the user interface of the Nix package manager. It provides a more user-friendly way to interact with Nix expressions, streamlining a process that previously required more manual steps like locating source files in the Nix store.

SEE ALSO

nix(1), nix build(1), nix develop(1), nix shell(1), nix repl(1), env(1), vim(1), emacs(1), nano(1)

Copied to clipboard