11

After some time, I am starting to run out of disk space in my development machine (only 128GB). For this reason, I have decided to move local cache stores (.npm, .m2, .ivy2, etc.) to an external drive.

I switched recently to Nix for Haskell development after experiencing the well known "cabal hell". I haven't found a proper way to change the Nix store location, though.

Is it possible?

jarandaf
  • 4,297
  • 6
  • 38
  • 67

5 Answers5

11

After a little bit of github archaeology, I found nixos commit f8cd904:

https://github.com/NixOS/nix/commit/f8cd904e05b95c5a3ca7cf570c0503a25a2095ca

This works around a behavior (bug?) in gcc 3.3.3. The same commit message recommends working around the issue with bind mounts:

On Linux, bind mounts can be used instead of symlink for this purpose (e.g., `mount -o bind /data/nix/store /nix/store').

Nick
  • 111
  • 1
  • 3
  • Links may be outdated someday. – jogo Jan 23 '16 at 17:09
  • 1
    Nix does support symlinked store somewhat and there is a flag for that - NIX_IGNORE_SYMLINK_STORE. I had used symlinked /nix (with the flag) without any problems until I tried to install an R package which wanted to compile c++ code. At that point ld-wrapper.sh failed to recognize the path in the store after the symlink was replaced with actual path. So the bind mounts do solve this problem. See https://github.com/NixOS/nixpkgs/issues/16298. – Alex Vorobiev Jun 21 '16 at 02:48
  • I'd choose `mount -o bind` because it also fixes the annoying auto-complete for sym link directories (type "/ni" and tab, completes to "/nix", not "/nix/", then I have to type slash). – saeedgnu May 18 '18 at 16:43
  • @saeedgnu `this I have to type slash` -- or press tab again. :-) But yes, annoying. – clacke Dec 15 '18 at 04:00
  • Good solution, but `mount: /home/cadoiz/testbla: must be superuser to use mount.` And you have to create the directory/mountpoint first. – Cadoiz Nov 07 '22 at 08:10
8

In case someone is still searching for this question: the Nix manual explains that you can use the symlink at your own risk (possible failure building from source) https://nixos.org/nix/manual/#sec-common-env It is enough to set the following environment variable

export NIX_IGNORE_SYMLINK_STORE=1

edit:

tl,dr;

setting this environment variable will let you use a symlink to /nix; this may be convenient if

  • you don't want to mount something on your root directory
  • you still want to use precompiled pakages

be warned: this is not portable

details:

from the documentation

Normally, the Nix store directory (typically /nix/store) is not allowed to contain any symlink components. This is to prevent “impure” builds. Builders sometimes “canonicalise” paths by resolving all symlink components. Thus, builds on different machines (with /nix/store resolving to different locations) could yield different results. This is generally not a problem, except when builds are deployed to machines where /nix/store resolves differently. If you are sure that you’re not going to do that, you can set NIX_IGNORE_SYMLINK_STORE to 1.

So it is mostly a decision related to how some makefile work. If you don't plan to port your configuration elsewhere, it just works.

marco
  • 163
  • 2
  • 7
  • 1
    Care to explain this in greater detail for future proofing in case the link goes dead? – Ethan Field Sep 01 '17 at 10:05
  • Yes; I'll try to add some detail, please let me know if it is enough – marco Sep 11 '17 at 08:19
  • Perfect, just remember for future answers that links for information/answers are dependant on website on the end of the link still being alive in a few years time. There are far too many answers with links to good documentation from 5/6 years ago on this site, which are useless now because the site doesn't exist anymore. – Ethan Field Sep 11 '17 at 08:30
7

As of Nix 2.0 there are three options that allow a user to use an alternative nix store, all three of which I assume use user namespaces:

https://nixos.wiki/wiki/Nix_Installation_Guide#Installing_without_root_permissions

  • nix-user-chroot creates an environment with a relocated /nix.
  • proot creates a user-managed chroot-like.
  • If you have Nix installed, a user can choose to use an alternative store with nix run --store /path/to/store.
clacke
  • 7,688
  • 6
  • 46
  • 48
2

building from source with specifying --with-store-dir=path

note that this would only define the store folder (/nix/store) to another location but not the whole /nix folder

the documentation also says

Warning: It is best not to change the Nix store from its default, since doing so makes it impossible to use pre-built binaries from the standard Nixpkgs channels — that is, all packages will need to be built from source.

ref: http://nixos.org/nix/manual/#sec-building-source

hash
  • 244
  • 2
  • 3
1

Yes, but you won't be able to use the binary packages (everything will be compiled from scratch). To bootstrap, see https://nixos.org/wiki/How_to_install_nix_in_home_%28on_another_distribution%29

A better idea might be to make /nix be a symlink to a directory on your external drive. Have you considered this? As @jarandaf mentions below, /nix cannot be a symlink.

Jim Garrison
  • 4,199
  • 4
  • 25
  • 39
  • Could you explain why is the binary cache then lost? – jarandaf Mar 27 '15 at 08:52
  • Most programs when built hard-code the `PREFIX` they were built for into the program. The standard nix binaries look in `/nix/store` for everything. The same thing is true for other distributions, where you cannot (in general) take a binary package and install it in an arbitrary directory. Using a symlink to your bigger drive, though, would allow you to keep everything in `/nix/store` as far as the programs are concerned but yet actually *store* it somewhere else. – Jim Garrison Mar 27 '15 at 12:58
  • 2
    For further reference, creating a symlink in `/nix` isn't an option: `error: the path ‘/nix’ is a symlink; this is not allowed for the Nix store and its parent directories`. – jarandaf Mar 27 '15 at 14:11
  • 3
    @jarandaf that's pretty absurd (not your comment, but Nix's decision to check whether it's a symlink or not). Why do they care at all? – Jcl Mar 27 '15 at 18:06
  • 2
    @Jcl no idea, that's something I would be interested to know, definitely. Probably `nix`'s intensive usage of symlinks might be a reason. – jarandaf Mar 27 '15 at 18:12
  • Might be worth asking on nix-dev (or searching the archives) if nobody here can answer; I'd be curious too. – Jim Garrison Mar 27 '15 at 22:00
  • It's possible that an intensional nix store may make this irrelevant. – CMCDragonkai Aug 10 '17 at 06:45
  • The Nix people probably want `realpath` of Nix store paths to have predictable, reproducible results. – David Grayson Sep 20 '17 at 22:27