SourceProfile adds environment variables from the Nix profile shell scripts to the current process's environment. This ensures that PATH contains the nix bin directory and that NIX_PROFILES and NIX_SSL_CERT_FILE are set. For properly configured Nix installations, the user's login shell handles sour
()
| 317 | // For properly configured Nix installations, the user's login shell handles |
| 318 | // sourcing the profile and SourceProfile has no effect. |
| 319 | func SourceProfile() (sourced bool, err error) { |
| 320 | if profileSourced() { |
| 321 | return false, nil |
| 322 | } |
| 323 | sourceProfileMutex.Lock() |
| 324 | defer sourceProfileMutex.Unlock() |
| 325 | |
| 326 | if profileSourced() { |
| 327 | return false, nil |
| 328 | } |
| 329 | |
| 330 | shell := os.Getenv("SHELL") |
| 331 | if shell == "" { |
| 332 | shell = "sh" |
| 333 | } |
| 334 | shell, _ = exec.LookPath("sh") |
| 335 | if shell == "" { |
| 336 | shell = "/bin/sh" |
| 337 | } |
| 338 | |
| 339 | trySource := func(path string) error { |
| 340 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 341 | defer cancel() |
| 342 | |
| 343 | wantEnv := map[string]bool{ |
| 344 | "NIX_PROFILES": true, |
| 345 | "NIX_SSL_CERT_FILE": true, |
| 346 | "PATH": true, |
| 347 | "XDG_DATA_DIRS": true, |
| 348 | "__ETC_PROFILE_NIX_SOURCED": true, |
| 349 | } |
| 350 | script := fmt.Sprintf(". \"%s\"\n", path) |
| 351 | for name := range wantEnv { |
| 352 | script += fmt.Sprintf("echo %s=\"$%[1]s\"\n", name) |
| 353 | } |
| 354 | |
| 355 | cmd := exec.CommandContext(ctx, shell, "-e", "-c", script) |
| 356 | stdout, err := cmd.Output() |
| 357 | if err != nil { |
| 358 | return err |
| 359 | } |
| 360 | for _, line := range strings.Split(string(stdout), "\n") { |
| 361 | name, value, ok := strings.Cut(line, "=") |
| 362 | if !ok { |
| 363 | continue |
| 364 | } |
| 365 | if wantEnv[name] { |
| 366 | err = os.Setenv(name, value) |
| 367 | if err != nil { |
| 368 | return err |
| 369 | } |
| 370 | delete(wantEnv, name) |
| 371 | } |
| 372 | } |
| 373 | return nil |
| 374 | } |
| 375 | |
| 376 | for _, path := range profilePaths() { |
no test coverage detected