Storage optimization: Difference between revisions

imported>Mth
automatically run garbage collection whenever there is not enough space left
imported>Mth
m Use syntaxhighlight lang="console"
Line 7: Line 7:
Note that if a result file still exists in the file system, all the dependencies used to build it will be kept. To see which result files prevent garbage collection, run:
Note that if a result file still exists in the file system, all the dependencies used to build it will be kept. To see which result files prevent garbage collection, run:


<syntaxhighlight lang="bash">$ nix-store --gc --print-roots
<syntaxhighlight lang="console">$ nix-store --gc --print-roots
/home/danbst/dev/test-shell/.shell.drv -> /nix/store/4diqwczyjipdqyi7aj34wfagblbhfjr9-nixops-1.4
/home/danbst/dev/test-shell/.shell.drv -> /nix/store/4diqwczyjipdqyi7aj34wfagblbhfjr9-nixops-1.4
/home/danbst/dev/test-shell/.shell.drv-2 -> /nix/store/62h3c4d6rdnlxichixqg8h9jxi8nhxk0-stdenv
/home/danbst/dev/test-shell/.shell.drv-2 -> /nix/store/62h3c4d6rdnlxichixqg8h9jxi8nhxk0-stdenv
Line 44: Line 44:
How much space do these (apparently) abandoned derivations use?
How much space do these (apparently) abandoned derivations use?


<syntaxhighlight lang="bash">$ du -sch $(nix-store -qR /root/forkstat/result /home/ec2-user/result /home/danbst/stack/new/website/server/result)
<syntaxhighlight lang="console">$ du -sch $(nix-store -qR /root/forkstat/result /home/ec2-user/result /home/danbst/stack/new/website/server/result)
...
...
3.4G    total</syntaxhighlight>
3.4G    total</syntaxhighlight>
Not all of the derivations are garbage in this case, but quite a few are:
Not all of the derivations are garbage in this case, but quite a few are:


<pre>
<syntaxhighlight lang="console">
# rm /root/forkstat/result /home/ec2-user/result /home/danbst/stack/new/website/server/result
# rm /root/forkstat/result /home/ec2-user/result /home/danbst/stack/new/website/server/result
# nix-collect-garbage -d
# nix-collect-garbage -d
...
...
690 store paths deleted, 1817.99 MiB freed
690 store paths deleted, 1817.99 MiB freed
</pre>
</syntaxhighlight>
Look for system derivations in particular. Those are created on many occasions, for example when running <code>nixos-rebuild  build-vm</code>
Look for system derivations in particular. Those are created on many occasions, for example when running <code>nixos-rebuild  build-vm</code>


Line 65: Line 65:
When you invoke <code>nix-shell</code> with
When you invoke <code>nix-shell</code> with


<syntaxhighlight lang="bash">nix-instantiate shell.nix --indirect --add-root $DIR/.nix-gc-roots/shell.drv ...</syntaxhighlight>
<syntaxhighlight lang="console">
$ nix-instantiate shell.nix --indirect --add-root $DIR/.nix-gc-roots/shell.drv ...
</syntaxhighlight>
then you'll have a persistent environment which won't be garbage collected. It is useful when you don't want to spend time waiting for redownloads every time you enter the shell.
then you'll have a persistent environment which won't be garbage collected. It is useful when you don't want to spend time waiting for redownloads every time you enter the shell.


Line 111: Line 113:
Run <code>nix-store --optimise</code> in case you really need some a bit of space. It is a lengthy operation, and after multiple-outputs PR landing, it became less useful.
Run <code>nix-store --optimise</code> in case you really need some a bit of space. It is a lengthy operation, and after multiple-outputs PR landing, it became less useful.


<pre>
<syntaxhighlight lang="console">
# nix-store --optimise
# nix-store --optimise
985.41 MiB freed by hard-linking 251942 files
985.41 MiB freed by hard-linking 251942 files
</pre>
</syntaxhighlight>


== See also ==
== See also ==


<references group="cf."/>
<references group="cf."/>