Storage optimization: Difference between revisions

imported>Ianthehenry
m try to make some of the language a little more clear
imported>Ianthehenry
larger tweaks to the section on pinning
Line 94: Line 94:
=== Pinning ===
=== Pinning ===


When you invoke <code>nix-shell</code> with:
Running the following command:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ nix-instantiate shell.nix --indirect --add-root $DIR/.nix-gc-roots/shell.drv ...
$ nix-instantiate shell.nix --indirect --add-root ./.nix-gc-roots/shell.drv ...
</syntaxhighlight>
</syntaxhighlight>


Then you'll have a persistent environment which won't be garbage collected as long as you have configured <code>keep-outputs = true</code>. This is useful when you don't want to spend time waiting for redownloads every time you enter the shell.
Will create a persistent snapshot of your <code>shell.nix</code> dependencies, which then won't be garbage collected, as long as you have configured <code>keep-outputs = true</code> (and haven't changed the default of <code>keep-derivations = true</code>). This is useful if your project has a dependency with no substitutes available, or you don't want to spend time waiting to re-download your dependencies every time you enter the shell.


A little problem exists though. GC roots are numbered sequentially, so if you change <code>shell.nix</code> to contain '''fewer''' derivations, such that the name of the last GC root starts with <code>shell.drv-7</code>, then <code>shell.drv-{8,9,10,11,12}*</code> will be dangling and unused. To overcome this problem you should remove GC roots dir periodically (or just before running <code>nix-shell</code>).
You need to re-run that <code>nix-instantiate</code> command any time your <code>shell.nix</code> changes.


Obviously, you should remove the GC roots directory for projects you don't plan to work on.
And there is a subtle gotcha if your <code>shell.nix</code> happens to evaluate to more than one derivation: <code>nix-instantiate</code> will number each derivation sequentially, so if you change your <code>shell.nix</code> to contain ''fewer'' derivations, such that (for example) the name of the last GC root starts with <code>shell.drv-7</code>, then <code>shell.drv-{8,9,10,11,12...}</code> will be dangling and unused.
 
The easiest way to get around this is to delete the <code>./.nix-gc-roots</code> directory periodically (i.e., any time you re-run the <code>nix-instantiate</code> command).
 
Don't forget to periodically check your GC roots, and remove any that you no longer need.


=== Automation ===
=== Automation ===