How to Use the Command 'nix store' (with Examples)
The nix store
command is an essential tool for manipulating the Nix store, which holds the binaries and dependencies necessary for building and running software in the Nix package manager system. This command allows users to efficiently manage space, ensure consistency, and inspect or compare store paths. Below are some practical use cases illustrating its application.
Use Case 1: Collect Garbage, i.e., Remove Unused Paths to Reduce Space Usage
Code:
nix store gc
Motivation:
Over time, the Nix store can accumulate a significant amount of unused packages and dependencies, occupying valuable disk space. By performing garbage collection, users can reclaim this space, ensuring a leaner, more efficient filesystem. This is particularly useful in environments where storage capacity is a constraint or in CI pipelines where minimizing unnecessary data is crucial.
Explanation:
nix
: This invokes the Nix command-line tool.store
: Refers to the subcommand that provides operations related to the store.gc
: Stands for “garbage collect,” which is the process of cleaning up the store by removing paths no longer referenced by any active user environment or derivation.
Example Output:
deleting unused links...
deleting inaccessible paths...
deleted a total of 545.91 MiB
Use Case 2: Hard-Link Identical Files Together to Reduce Space Usage
Code:
nix store optimise
Motivation:
Files within the Nix store can sometimes be duplicated, consuming more space than necessary. Hard-linking identical files is a space-saving optimization technique that helps prevent redundant storage of the same data, thereby conserving disk space and enhancing store efficiency.
Explanation:
nix
: Initiates the Nix command-line tool.store
: Specifies operations pertaining to the store.optimise
: Instructs Nix to hard-link identical files within the store, combining them so they take up less space while maintaining access through different paths.
Example Output:
optimising store...
fixed: /nix/store/abc123-package1.drv
fixed: /nix/store/def456-package2.drv
Use Case 3: Delete a Specific Store Path (Must Be Unused)
Code:
nix store delete /nix/store/abc123-package
Motivation:
Sometimes, specific packages or paths may be rendered obsolete or unused and need to be explicitly removed from the store to free up space or for administrative reasons. This command is crucial when you want to precisely control the contents of the Nix store by removing unneeded or redundant paths.
Explanation:
nix
: Calls the Nix command-line interface.store
: Focuses on store-related functionalities.delete
: This argument specifies the action of removing a specific path./nix/store/abc123-package
: A placeholder for the precise path within the Nix store that you want to delete. Note that the store path must be unused by any current system or user environments to be eligible for deletion.
Example Output:
deleting '/nix/store/abc123-package'
Use Case 4: List of Contents of the Store Path, on a Remote Store
Code:
nix store --store https://cache.nixos.org ls /nix/store/abc123-package
Motivation:
There may be a need to inspect or verify the contents of a particular store path, especially when dealing with remote stores or caches. This is helpful in situations where you want to ensure that a remote cache contains the necessary files or to explore the structure of a specific package remotely.
Explanation:
nix
: The command-line interface for Nix.store
: Handles operations regarding the store.--store https://cache.nixos.org
: Specifies that the operation should be conducted on a remote store, located at the given URL, instead of the default local store.ls
: Stands for “list,” showing the contents of the designated store path./nix/store/abc123-package
: The store path whose contents you wish to list.
Example Output:
bin/
bin/executable1
share/
share/doc/
share/doc/doc1.txt
Use Case 5: Show the Differences in Versions Between Two Store Paths, with Their Respective Dependencies
Code:
nix store diff-closures /nix/store/abc123-package /nix/store/def456-package
Motivation:
When working with different versions of a package, it is often critical to understand what has changed, including dependencies. This can aid in debugging, ensuring compatibility, or validating migrations and updates. By comparing closures, which include a package and its dependencies, one can clearly see additions, removals, or updates.
Explanation:
nix
: This is the command-line interface for the Nix system.store
: The subcommand concerning store-related actions.diff-closures
: Short for “difference closures,” this subcommand compares the closures (dependencies) of two store paths./nix/store/abc123-package
: The first store path to compare./nix/store/def456-package
: The second store path for the comparison.
Example Output:
Path differences:
+ /nix/store/ghi789-newdependency
- /nix/store/jkl012-olddependency
Updated packages:
/nix/store/abc123-package -> /nix/store/def456-package
Conclusion
By leveraging the versatile nix store
command, users can effectively manage the Nix store’s contents, ensuring optimal space usage, maintaining system integrity, and gaining insights into package and dependency differences. Whether optimizing, cleaning up, or analyzing packages, nix store
provides powerful capabilities essential for any Nix-managed environment.