resolvePath resolves the path to the Nix executable. It returns n.Path if it is non-empty and a valid executable. Otherwise it searches for a nix executable in $PATH and common installation directories.
()
| 71 | // is non-empty and a valid executable. Otherwise it searches for a nix |
| 72 | // executable in $PATH and common installation directories. |
| 73 | func (n *Nix) resolvePath() (string, error) { |
| 74 | if n.Path != "" { |
| 75 | return exec.LookPath(n.Path) // verify it's an executable. |
| 76 | } |
| 77 | |
| 78 | // Re-use the cached path if we've already found Nix before. |
| 79 | cached := n.lookPath.Load() |
| 80 | if cached != nil && *cached != "" { |
| 81 | return *cached, nil |
| 82 | } |
| 83 | |
| 84 | _, _ = SourceProfile() |
| 85 | path, pathErr := exec.LookPath("nix") |
| 86 | if pathErr == nil { |
| 87 | n.lookPath.Store(&path) |
| 88 | return path, nil |
| 89 | } |
| 90 | |
| 91 | try := []string{ |
| 92 | "/nix/var/nix/profiles/default/bin/nix", |
| 93 | "/run/current-system/sw/bin", |
| 94 | } |
| 95 | for _, path := range try { |
| 96 | stat, err := os.Stat(path) |
| 97 | if err == nil { |
| 98 | // Is it executable and not a directory? |
| 99 | m := stat.Mode() |
| 100 | if !m.IsDir() && m.Perm()&0o111 != 0 { |
| 101 | n.lookPath.Store(&path) |
| 102 | return path, nil |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return "", pathErr |
| 107 | } |
| 108 | |
| 109 | func (n *Nix) logger() *slog.Logger { |
| 110 | if n.Logger == nil { |
no test coverage detected