The problem this actually solves

On a normal distro, if a network mount goes stale … the connection dropped, the server bounced, whatever … the fix is usually sudo umount then sudo mount -a, reading straight from /etc/fstab. Simple, because fstab is a plain text file you can just point tools at.

NixOS doesn’t work that way. fileSystems entries in configuration.nix get compiled into individual systemd .mount units automatically at build time. There’s no /etc/fstab to edit or run mount -a against in the traditional sense … the mounts exist purely as generated systemd units with names like mnt-t620\x2drelationships.mount.

When one of those goes stale, the usual muscle-memory fix doesn’t apply, and I found that out the hard way the first time a share on my T620 dropped and I instinctively reached for commands that don’t do anything useful on this system.

Why not just reboot every time

I could. Rebooting always clears stale mounts. But rebooting the whole desktop because one network share got confused is a heavy hammer for a small problem, and I wanted something I could run in five seconds without closing everything I had open.

What the script actually does, step by step

step "Stopping all network mount units"
units=$(systemctl list-units --type=mount --all --no-legend | \
        awk '{print $1}' | grep -E '^mnt-(t620|nfs)')

Instead of hardcoding every mount name, it asks systemd directly for every currently-known mount unit and filters down to the ones that match my naming pattern … mnt-t620-* for the Samba shares off my T620, mnt-nfs-* for the direct QNAP NFS mounts. This means if I add or remove a share in my NixOS config later, the script doesn’t need updating … it just discovers whatever’s actually there at the time it runs.

for u in $units; do
    info "Stopping $u"
    sudo systemctl stop "$u"
done

Stop each one properly through systemd, not by force-unmounting first … letting the unit shut itself down cleanly avoids leaving anything in a half-torn-down state.

The force-clear step, and why it’s there at all

step "Force-clearing anything still lingering"
for d in /mnt/t620-* /mnt/nfs-*; do
    [[ -d "$d" ]] || continue
    if mountpoint -q "$d"; then
        warn "$d still mounted, forcing unmount"
        sudo umount -f -l "$d" 2>&1
    fi
done

This exists because I’ve genuinely seen a mount survive a clean systemctl stop and still show as mounted afterward … usually when whatever’s on the other end (the T620, in my case) was slow to respond to the disconnect. Rather than assume the stop always worked, this checks every known mountpoint directly with mountpoint -q and only force-unmounts (-f -l, force plus lazy) the ones still actually showing as mounted. Doesn’t touch anything that already came down cleanly.

Getting everything back

step "Reloading systemd and remounting everything"
sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target

remote-fs.target is the systemd target that all network filesystem mounts hang off of … restarting it tells systemd to bring every associated mount unit back up in the correct order, which is the closest NixOS equivalent to a plain mount -a on a system with a real fstab.

The verification steps, and why I didn’t just trust it worked

systemctl --failed | grep -E 'mnt-(t620|nfs)' && warn "Some mounts still failed" \
    || msg "No failed network mount units"

Checking systemctl --failed specifically for my mount pattern tells me immediately if something didn’t come back, rather than assuming success just because the script ran without an error exiting it.

for d in /mnt/t620-* /mnt/nfs-*; do
    [[ -d "$d" ]] || continue
    if ls "$d" &>/dev/null; then
        msg "$d ... accessible"
    else
        err "$d ... NOT accessible"
    fi
done

The real proof isn’t “systemd thinks the mount is active,” it’s “can I actually list files in it.” A mount unit can report as active while the underlying share is unresponsive … this last check is the one that actually matters, and it’s deliberately the very last thing the script does.

Running it

sudo ./remount-network-shares.sh

Shows current mounts, tears down anything matching the pattern, brings it all back through systemd properly, then proves each mountpoint is genuinely reachable … not just reported as mounted. Five seconds, no reboot, and I get real confirmation instead of a guess.

#!/usr/bin/env bash
# =============================================================================
#  remount-network-shares.sh
#  Tolga Erok
#  Version : 1.0
#  Date    : 11 Jul 2026
#
#  Clears stale NFS/CIFS mounts on G4-NIXOS and remounts everything cleanly.
#  On NixOS, fileSystems entries in configuration.nix generate individual
#  systemd .mount units automatically -- my script targets those units
#  directly rather than touching /etc/fstab (there isn't one to edit).
# =============================================================================

clear
set -uo pipefail

red='\e[31m'; grn='\e[32m'; yel='\e[33m'; cyn='\e[36m'; rst='\e[0m'; bld='\e[1m'

msg()  { echo -e "${grn}${bld}[  OK  ]${rst} $*"; }
info() { echo -e "${cyn}${bld}[ INFO ]${rst} $*"; }
warn() { echo -e "${yel}${bld}[ WARN ]${rst} $*"; }
err()  { echo -e "${red}${bld}[ FAIL ]${rst} $*"; }
step() { echo -e "\n${yel}${bld}  ──► $*${rst}"; }

step "Current network mounts"
mount -t cifs,nfs,nfs4

step "Stopping all network mount units"
units=$(systemctl list-units --type=mount --all --no-legend | \
        awk '{print $1}' | grep -E '^mnt-(t620|nfs)')

if [[ -z "$units" ]]; then
    warn "No matching mount units found -- check naming with: systemctl list-units --type=mount"
else
    for u in $units; do
        info "Stopping $u"
        sudo systemctl stop "$u"
    done
fi

step "Force-clearing anything still lingering"
for d in /mnt/t620-* /mnt/nfs-*; do
    [[ -d "$d" ]] || continue
    if mountpoint -q "$d"; then
        warn "$d still mounted, forcing unmount"
        sudo umount -f -l "$d" 2>&1
    fi
done

step "Reloading systemd and remounting everything"
sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target

step "Verifying"
sleep 2
mount -t cifs,nfs,nfs4
echo
systemctl --failed | grep -E 'mnt-(t620|nfs)' && warn "Some mounts still failed -- check the unit above" \
    || msg "No failed network mount units"

step "Result"
for d in /mnt/t620-* /mnt/nfs-*; do
    [[ -d "$d" ]] || continue
    if ls "$d" &>/dev/null; then
        msg "$d — accessible"
    else
        err "$d — NOT accessible"
    fi
done