profilePaths returns the paths where the Nix profile shell scripts might be. None of the paths are guaranteed to be readable or even exist.
()
| 396 | // profilePaths returns the paths where the Nix profile shell scripts might be. |
| 397 | // None of the paths are guaranteed to be readable or even exist. |
| 398 | func profilePaths() []string { |
| 399 | // os.UserHomeDir only checks $HOME, user.Current reads /etc/passwd or |
| 400 | // uses libc. This can help when running in an isolated environment |
| 401 | // where $HOME isn't set. |
| 402 | home, _ := os.UserHomeDir() |
| 403 | if home == "" { |
| 404 | if u, err := user.Current(); err == nil { |
| 405 | home = u.HomeDir |
| 406 | } |
| 407 | } |
| 408 | if home == "" { |
| 409 | // Might as well check the root home directory if we've got |
| 410 | // nothing else. |
| 411 | home = "/root" |
| 412 | } |
| 413 | xdgState := os.Getenv("XDG_STATE_HOME") |
| 414 | if xdgState == "" { |
| 415 | xdgState = filepath.Join(home, ".local/state") |
| 416 | } |
| 417 | |
| 418 | dirs := make([]string, 0, 5) |
| 419 | if nixExe, err := exec.LookPath("nix"); err == nil { |
| 420 | dirs = append(dirs, filepath.Clean(nixExe+"/../../etc/profile.d")) |
| 421 | } |
| 422 | if !slices.Contains(dirs, "/nix/var/nix/profiles/default/etc/profile.d") { |
| 423 | dirs = append(dirs, "/nix/var/nix/profiles/default/etc/profile.d") |
| 424 | } |
| 425 | dirs = append(dirs, |
| 426 | filepath.Join(home, ".nix-profile/etc/profile.d"), |
| 427 | filepath.Join(xdgState, "nix/profile/etc/profile.d"), |
| 428 | filepath.Join(xdgState, "nix/profiles/profile/etc/profile.d"), |
| 429 | ) |
| 430 | |
| 431 | // Try sourcing scripts in the following order: |
| 432 | // |
| 433 | // 1. nix-daemon.sh: because nix.sh is a no-op when $USER isn't set |
| 434 | // (this happens in containers). |
| 435 | // 2. nix-daemon.fish: same, but for fish users. |
| 436 | // 3. nix.sh, nix.fish: for old single-user installs. |
| 437 | files := make([]string, 0, len(dirs)*4) |
| 438 | for _, dir := range dirs { |
| 439 | files = append(files, filepath.Join(dir, "nix-daemon.sh")) |
| 440 | } |
| 441 | for _, dir := range dirs { |
| 442 | files = append(files, filepath.Join(dir, "nix-daemon.fish")) |
| 443 | } |
| 444 | for _, dir := range dirs { |
| 445 | files = append(files, filepath.Join(dir, "nix.sh")) |
| 446 | } |
| 447 | for _, dir := range dirs { |
| 448 | files = append(files, filepath.Join(dir, "nix.fish")) |
| 449 | } |
| 450 | return files |
| 451 | } |