Skip to content

patchpkg: lookPath helper to find nix binaries #2247

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
Sep 3, 2024
Merged
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
22 changes: 20 additions & 2 deletions internal/patchpkg/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func (d *DerivationBuilder) Build(ctx context.Context, pkgStorePath string) erro
}
}

bash := filepath.Join(os.Getenv("bash"), "bin/bash")
cmd := exec.CommandContext(ctx, bash, "-s")
cmd := exec.CommandContext(ctx, lookPath("bash"), "-s")
cmd.Stdin = bytes.NewReader(glibcPatchScript)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -152,5 +151,24 @@ func allFiles(fsys fs.FS, root string) iter.Seq2[string, fs.DirEntry] {
}
}

// lookPath is like [exec.lookPath], but first checks if there's an environment
// variable with the name prog. If there is, it returns $prog/bin/prog instead
// of consulting PATH.
//
// For example, lookPath would be able to find bash and patchelf in the
// following derivation:
//
// derivation {
// inherit (nixpkgs.legacyPackages.x86_64-linux) bash patchelf;
// builder = devbox;
// }
func lookPath(prog string) string {
pkgPath := os.Getenv(prog)
if pkgPath == "" {
return prog
}
return filepath.Join(pkgPath, "bin", prog)
}

func isExecutable(mode fs.FileMode) bool { return mode&0o111 != 0 }
func isSymlink(mode fs.FileMode) bool { return mode&fs.ModeSymlink != 0 }