From d60a8ee8b0d66ff75a32dedec13ef4c81c13e3f1 Mon Sep 17 00:00:00 2001 From: Matthew Kenigsberg Date: Mon, 9 Jun 2025 16:46:20 -0600 Subject: [PATCH] Improve database lock permission error context Add helpful context when opening the Nix database lock fails due to permission errors. Instead of just showing "Permission denied", now provides guidance about possible causes: - Running as non-root in a single-user Nix installation - Nix daemon may have crashed --- src/libstore/local-store.cc | 11 ++++++++++- tests/functional/read-only-store.sh | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 76fadba86..da266fa7e 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -221,7 +221,16 @@ LocalStore::LocalStore(ref config) schema upgrade is in progress. */ if (!config->readOnly) { Path globalLockPath = dbDir + "/big-lock"; - globalLock = openLockFile(globalLockPath.c_str(), true); + try { + globalLock = openLockFile(globalLockPath.c_str(), true); + } catch (SysError & e) { + if (e.errNo == EACCES || e.errNo == EPERM) { + e.addTrace({}, + "This command may have been run as non-root in a single-user Nix installation,\n" + "or the Nix daemon may have crashed."); + } + throw; + } } if (!config->readOnly && !lockFile(globalLock.get(), ltRead, false)) { diff --git a/tests/functional/read-only-store.sh b/tests/functional/read-only-store.sh index f6b6eaf32..ea96bba41 100755 --- a/tests/functional/read-only-store.sh +++ b/tests/functional/read-only-store.sh @@ -42,7 +42,8 @@ chmod -R -w $TEST_ROOT/var # Make sure we fail on add operations on the read-only store # This is only for adding files that are not *already* in the store -expectStderr 1 nix-store --add eval.nix | grepQuiet "error: opening lock file '$(readlink -e $TEST_ROOT)/var/nix/db/big-lock'" +# Should show enhanced error message with helpful context +expectStderr 1 nix-store --add eval.nix | grepQuiet "This command may have been run as non-root in a single-user Nix installation" expectStderr 1 nix-store --store local?read-only=true --add eval.nix | grepQuiet "Permission denied" # Test the same operations from before should again succeed