Skip to content

Fix issue 2601 (dont fail on empty /nix folder) #2605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func Exists(path string) bool {
return err == nil
}

func IsDirEmpty(path string) bool {
entries, err := os.ReadDir(path)
if err != nil {
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning false on error is a bit confusing (is it not empty if it doesn't exist?)

IsDirEmpty could return the error as well and dirExistsAndIsNotEmpty handles the error and returns a single bool

}
return len(entries) == 0
}

// FileContains checks if a given file at 'path' contains the 'substring'
func FileContains(path, substring string) (bool, error) {
data, err := os.ReadFile(path)
Expand Down
7 changes: 4 additions & 3 deletions internal/nix/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func BinaryInstalled() bool {
return cmdutil.Exists("nix")
}

func dirExists() bool {
return fileutil.Exists("/nix")
func dirExistsAndIsNotEmpty() bool {
dir := "/nix"
return fileutil.Exists(dir) && !fileutil.IsDirEmpty(dir)
}

var ensured = false
Expand Down Expand Up @@ -57,7 +58,7 @@ func EnsureNixInstalled(ctx context.Context, writer io.Writer, withDaemonFunc fu
if BinaryInstalled() {
return nil
}
if dirExists() {
if dirExistsAndIsNotEmpty() {
if _, err = SourceProfile(); err != nil {
return err
} else if BinaryInstalled() {
Expand Down