Nix using store in /var, bind mount?

I have a server where I have enough space available on /var, but not on the root filesystem, and I want to install Nix on that machine. Can I just install it, then move /nix to /var/nix-store and then bind-mount /var/nix-store to /nix?

yes you can, the same way iso uses ro-store and rw-store in overlayfs

2 Likes

I tried this, and now I’m getting:

error: cannot connect to socket at '/nix/var/nix/daemon-socket/socket': Connection refused.

Did I do anything wrong? I did mv /nix /var/nix and then bind-mounted /var/nix to /nix (and restarted nix-daemon).

I was able to solve this by executing

systemctl stop nix-daemon
systemctl stop nix-daemon.socket
systemctl daemon-reload
systemctl start nix-daemon

In case anyone comes across this.

3 Likes

please mark it as a solution! :slight_smile:

I’d love to, but I don’t see a way how to…

Oh!, maybe because you don’t have enough reputation yet, or some other ‘privilege level’ stuff?

Why did this happen? I installed on ubuntu latest LTS, and I was able to do some nix-shell -p .... but after reboot started to get this error: cannot connect to socket at '/nix/var/nix/daemon-socket/socket': Connection refused.

I’ve had this happen because the systemd unit file to start the nix-daemon (/etc/systemd/system/nix-demon.{service, socket}) is a symlink that resolves into the nix store and systemd only scans for unit files once at startup. In other words, systemd does not use unit files that are not present on the root filesystem at system startup and /nix is bind mounted by systemd later.

I don’t have easy access to the system where I had this problem right now to check for sure but I believe my solution was to copy the nix-daemon service file into /etc/systemd/system so that it was no longer a symlink. I also adjusted the Exec path to be /nix/var/nix/profiles/default/bin/nix-daemon so that it will run the nix-daemon from the current system profile. I think that systemd will automatically add a dependency on the /nix mount point because of that path appearing in the Exec option.

The other idea I had (but I don’t remember if I ever tried it or not) was to have a systemd unit that runs after /nix was mounted and does a systemctl daemon-reload so that systemd could pick up the now-available nix-daemon unit files.