Nix Cookbook: Difference between revisions
imported>Hmenke writeScriptBin -> writeShellScriptBin |
imported>Nix add section on managing storage |
||
Line 1: | Line 1: | ||
{{Note| 1=This is a [[:Category:Cookbook|Cookbook]] page. It serves as a collection of topical, often question and answer style, information. Information should be well categorized for easy reference. }} | |||
== Managing storage == | |||
=== Reclaim space on Nix install? === | |||
==== Remove old generations ==== | |||
When you make changes to your system, Nix creates a new system [[Generation]]. All of the changes to the system since the previous generation are stored there. Old generations can add up and will not be removed automatically by default. You can see your generations with: | |||
<syntaxHighlight lang=shell> | |||
$ nix-env --list-generations | |||
</syntaxHighlight> | |||
To remove all older than | |||
==== Garbage collection ==== | |||
As you work with your system (installs, uninstalls, upgrades), files in the [[Nix store]] are not automatically removed, even when no longer needed. Nix instead has a garbage collector which must be run periodically (you could set up, e.g., a cron to do this). | |||
<syntaxHighlight lang=shell> | |||
$ nix-collect-garbage | |||
</syntaxHighlight> | |||
This is safe so long as everything you need is listed in an existing ''generation'' or garbage collector root ('''gcroot'''). | |||
If you are sure you only need your current generation, this will delete all old generations and then do garbage collection: | |||
<syntaxHighlight lang=shell> | |||
$ nix-collect-garbage -d | |||
</syntaxHighlight> | |||
On NixOS, you can enable a service to automatically do daily garbage collection: | |||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | |||
nix.gc.automatic = true; | |||
</nowiki>}} | |||
==== Deduplication ==== | |||
You may wind up with duplicate files in your Nix store. Data deduplication is a resource intense process, so is not done automatically. Often you can save about 25-35% of your store space by optimizing the store though. This will perform a deduplication process on your Nix store (hard link duplicates together): | |||
<syntaxHighlight lang=shell> | |||
$ nix optimise-store | |||
</syntaxHighlight> | |||
You can set the <code>nix.conf</code> option below to set this to happen periodically: | |||
{{file|/etc/nix/nix.conf|shell|<nowiki> | |||
auto-optimise-store = true | |||
</nowiki>}} | |||
For NixOS, the equivalent option is: | |||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | |||
nix.autoOptimiseStore = true; | |||
</nowiki>}} | |||
==== Nix manual references ==== | |||
* [https://nixos.org/manual/nix/stable/#idm140737322626416 Introduction - Garbage Collection] | |||
* [https://nixos.org/manual/nix/stable/#chap-quick-start Quick Start] | |||
* [https://nixos.org/manual/nix/#sec-garbage-collection Garbage Collection Chapter] | |||
* [https://nixos.org/manual/nix/stable/#ssec-gc-roots Garbage Collector Roots] | |||
* [https://nixos.org/manual/nix/stable/#operation-optimise Optimize store] | |||
* [https://nixos.org/manual/nixos/#sec-nix-gc NixOS manual - Cleaning Nix store] | |||
== Environment Tasks == | == Environment Tasks == |