How to prevent a store path from being garbage-collected?

tl;dr How to I combine nix-store --add-fixed and nix-store --add-root to prevent garbage collection of manually added files?


I’ve installed Mathematica on my Nix installation which requires the manual insertion of the installer into the Nix store. To this end I am using

$ nix-store --add-fixed sha256 Mathematica_11.3.0_LINUX.sh

First of all this doesn’t work on my 2012 laptop because it runs out of memory and I use the technique outlined in the (old) NixOS wiki.

  1. Find the right store path

    $ nix-hash --type sha256 --flat --base32 Mathematica_11.3.0_LINUX.sh
    03nimsayxj18740nq1jxkjyif2rphkdhpbz3gf749j7aq44f5kqg
    $ nix-store --print-fixed-path sha256 03nimsayxj18740nq1jxkjyif2rphkdhpbz3gf749j7aq44f5kqg Mathematica_11.3.0_LINUX.sh
    /nix/store/h9pljd50ha6b4bbrwp3riabh3g4i5bkp-Mathematica_11.3.0_LINUX.sh
    
  2. Move (or copy) the file there as root

    # mount -o remount,rw /nix/store
    # mv Mathematica_11.3.0_LINUX.sh /nix/store/h9pljd50ha6b4bbrwp3riabh3g4i5bkp-Mathematica_11.3.0_LINUX.sh
    # mount -o remount,ro /nix/store
    
  3. Register the file (interactively, i.e. requires user input)

    # nix-store --register-validity --reregister
    /nix/store/h9pljd50ha6b4bbrwp3riabh3g4i5bkp-Mathematica_11.3.0_LINUX.sh <RET>
    <RET>
    0 <RET>
    <C-d>
    

This method is surely not very elegant but seems to work fine. However, because the laptop is so old, it only has a small SSD and therefore I regularly garbage collect the Nix store. Unfortunately, this will eventually delete the Mathematica installer but I need this again when there is an update for curl or whatever and the Mathematica derivation has to be rebuilt. Hence I’m keeping around a second copy of the installer outside of the Nix store, just so that I can go through the process described above again, when it is needed.

I’ve went through the manpage of nix-store and found the --add-root option to add a gcroot for a realization but I don’t know how to combine this with the above process. Can you please help me?


Just another side question: How do I change the syntax highlighting to not use Nix language highlighting, but either none or console session?

I can’t help with your setup question, though syntax highlighting in the forum is controlled by providing highlight hints to the opening fence when using fenced code blocks.

You can’t give hints when using indented code blocks.

```nix
# comment
```

```plain
No highlighting
```
1 Like

In short:

nix-store --add-root <some_path_here_for_symlink> --indirect --realise /nix/store/...

This is also how the result symlinks from nix-build are made.

1 Like