Broke Nix? Cannot connect to socket at '/nix/var/nix/daemon-socket': Connection refused

Heya! I’m using Nix on top of a Debian installation. Yesterday, I tried moving my /nix folder onto a separate btrfs subvolume so that I wouldn’t be having my nix store captured in my btrfs snapshots. I just used the mv command to do this, but it appears doing that has caused nix-daemon to break. Now, whenever I run just about any nix-related command without sudo, I get an error like this:

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

What’s the best way to fix this? Am I best off just re-installing Nix, or is that not needed? Also how would I move my /nix next time I have to do something like this, so I don’t break everything?

1 Like

Did you stop the nix-daemon service and configure where the new /nix is? Make sure you also restart nix-daemon afterwards.

1 Like

It appears a sudo systemctl restart nix-daemon makes the affected Nix commands work again, but only until I reboot the system, and then it returns to being broken.

How do I configure nix-daemon to make it look at the new /nix at startup?

Is your /nix being mounted at startup? It should be in your /etc/fstab

It is
Snippet of my fstab file:

UUID=e0512aab-05f9-4b14-b7c3-4b5eb5b68dac  /  btrfs  rw,relatime,compress=no,discard=async,ssd,space_cache=v2,subvol=/@  0 0
UUID=e0512aab-05f9-4b14-b7c3-4b5eb5b68dac  /btrfsroot  btrfs  defaults,subvolid=5  0 0
UUID=e0512aab-05f9-4b14-b7c3-4b5eb5b68dac  /home  btrfs  rw,relatime,nodev,nosuid,compress=no,discard=async,ssd,space_cache=v2,subvol=/@home  0 0
UUID=e0512aab-05f9-4b14-b7c3-4b5eb5b68dac  /tmp  btrfs  rw,relatime,compress=no,discard=async,ssd,space_cache=v2,subvol=/@tmp  0 0
UUID=e0512aab-05f9-4b14-b7c3-4b5eb5b68dac  /nix  btrfs  rw,relatime,compress=no,discard=async,ssd,space_cache=v2,subvol=/@nix  0 0

Maybe the problem is that /nix is getting mounted after nix-daemon starts? Maybe I can set up a systemd mountpoint for /nix with the service for nix-daemon requiring that mountpoint? I’ll try that out and see if it works.

1 Like

Ok, I fixed the problem. Turns out that if you have /nix on a separate partition or subvolume, systemd will not be made aware of the nix-daemon service immediately, since it lives in the nix store and is symlinked to /etc/systemd/system. You have to run systemctl daemon-reload at startup to make it aware of the nix-daemon service so it can actually be loaded.

I made a mount unit for /nix (nix.mount) and a service unit to run the daemon-reload command (ensure-symlinked-units-resolve.service).

nix.mount:

[Unit]
Description=Nix directory (/nix)
Before=local-fs.target umount.target
Conflicts=umount.target
PropagatesStopTo=nix-daemon.service
ConditionPathIsDirectory=/nix
DefaultDependencies=no
RequiredBy=nix-daemon.service
RequiredBy=nix-daemon.socket

[Mount]
What=/dev/disk/by-uuid/e0512aab-05f9-4b14-b7c3-4b5eb5b68dac
Where=/nix
Type=btrfs
Options=subvol=/@nix

[Install]
WantedBy=default.target

ensure-symlinked-units-resolve.service:

[Unit]
Description=Ensure Nix related units which are symlinked resolve
After=nix.mount
Requires=nix.mount
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemctl daemon-reload
ExecStart=/usr/bin/systemctl restart --no-block nix-daemon.socket

[Install]
WantedBy=sysinit.target

I removed the fstab entry for /nix, and then ran sudo systemctl enable nix.mount and sudo systemctl enable ensure-symlinked-units-resolve.service.

Thank you! :3

4 Likes

I have also mv /nix /media/partition2 to a other (bigger) partition. Created a symlink with ln -s /media/partition2/nix /nix Now nix seems to works but not fully:

$ echo “Hello Nix” | nix run “https://flakehub.com/f/NixOS/nixpkgs/*#ponysay
error: cannot connect to socket at ‘/nix/var/nix/daemon-socket/socket’: Connection refused

Where I have to create nix.mount and ensure-symlinked-units-resolve.service in the filestructure?

sorry for such a noob question - but thats what I am :slight_smile:

If using NixOS you can use this discourse as a reference.