nix-edit
Edit Nix expressions
TLDR
Open the source of the Nix expression of a package from nixpkgs in your $EDITOR
Dump the source of a package to stdout
SYNOPSIS
nix edit [GLOBAL-OPTIONS] [EDIT-OPTIONS] [ATTRIBUTE-PATH]
nix edit [GLOBAL-OPTIONS] [EDIT-OPTIONS] --file PATH
nix edit [GLOBAL-OPTIONS] [EDIT-OPTIONS] --expr EXPR
PARAMETERS
ATTRIBUTE-PATH
Specifies the Nix expression to edit using an attribute path, e.g., nixpkgs#hello or .#myPackage within a flake. This is the most common way to target a specific package or derivation.
--file PATH
Directly opens the specified Nix file PATH in the editor. This is useful when you already know the exact file you want to edit.
--expr EXPR
Evaluates the provided Nix expression string EXPR and opens its source definition. This allows for editing expressions that might not correspond directly to a file.
--subpath PATH
When opening a derivation's source, this option allows specifying a particular subpath within the derivation's source tree to open, instead of the primary Nix expression file.
--offline
Operate in offline mode, preventing network access for fetching Nixpkgs or flake inputs.
--refresh
Force a refresh of flake inputs and cached data, ensuring you're working with the latest versions.
--option NAME VALUE
Set a specific Nix configuration option, overriding default or system-wide settings for this command invocation (e.g., --option experimental-features 'nix-commands flakes').
--show-trace
Show a detailed stack trace on Nix evaluation errors, which is invaluable for debugging issues in Nix expressions.
--verbose
Increase the verbosity of output messages, providing more detailed information about the command's execution.
--quiet
Suppress most output messages, showing only critical errors. Useful for scripting or when minimal output is desired.
DESCRIPTION
nix edit is a powerful subcommand of the Nix command-line tool designed to streamline the process of inspecting and temporarily modifying Nix expressions. It allows users to quickly open the Nix source code that defines a specific Nix store path, a derivation, or an attribute from a flake. This capability is immensely useful for understanding how a package is built, debugging configuration issues, or performing rapid, non-permanent modifications to a package definition without needing to manually locate the relevant Nix expression file within the Nixpkgs source tree or a custom flake. The command intelligently determines the correct Nix file and opens it in the user's preferred text editor, as defined by the EDITOR or VISUAL environment variables.
CAVEATS
nix edit requires the EDITOR or VISUAL environment variable to be set to a valid editor program (e.g., nvim, code, nano). If neither is set, it might default to a basic editor like vi or fail.
Editing expressions opened by nix edit creates a temporary file for modifications. These changes are not permanent and do not alter the package definition in your Nix store or cache. To apply changes, you typically need to incorporate them into your own flake, overlay, or Nixpkgs fork and then rebuild or re-instantiate the derivation.
<I>ENVIRONMENT VARIABLES</I>
The behavior of nix edit is highly dependent on the EDITOR or VISUAL environment variables. These variables tell Nix which text editor to launch. For optimal user experience, it is highly recommended to set one of these variables in your shell configuration (e.g., export EDITOR=nvim or export VISUAL=code --wait).
<I>TEMPORARY NATURE OF EDITS</I>
It is crucial to understand that when nix edit opens a Nix expression, it typically copies the relevant file to a temporary location for editing. Any changes you make and save are applied only to this temporary copy. This command is primarily for quick inspection, exploration, and experimental modification. To make changes persistent and globally visible to the Nix system, you must integrate them into a proper Nix project structure (like a flake or an overlay) and then build or apply those changes.
HISTORY
The nix edit command is a key component of the 'new' Nix command-line interface, introduced as part of the broader effort to unify the Nix CLI and integrate Nix flakes. It aims to provide a more intuitive and user-friendly experience compared to older, more fragmented Nix commands. Its development reflects the ongoing evolution of the Nix project towards more declarative and reproducible system management, focusing on improved developer ergonomics.