|
| 1 | +// patchpkg patches packages to fix common linker errors. |
| 2 | +package patchpkg |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + _ "embed" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "io/fs" |
| 11 | + "iter" |
| 12 | + "log/slog" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed glibc-patch.bash |
| 19 | +var glibcPatchScript []byte |
| 20 | + |
| 21 | +// DerivationBuilder patches an existing package. |
| 22 | +type DerivationBuilder struct { |
| 23 | + // Out is the output directory that will contain the built derivation. |
| 24 | + // If empty it defaults to $out, which is typically set by Nix. |
| 25 | + Out string |
| 26 | +} |
| 27 | + |
| 28 | +// NewDerivationBuilder initializes a new DerivationBuilder from the current |
| 29 | +// Nix build environment. |
| 30 | +func NewDerivationBuilder() (*DerivationBuilder, error) { |
| 31 | + d := &DerivationBuilder{} |
| 32 | + if err := d.init(); err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + return d, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (d *DerivationBuilder) init() error { |
| 39 | + if d.Out == "" { |
| 40 | + d.Out = os.Getenv("out") |
| 41 | + if d.Out == "" { |
| 42 | + return fmt.Errorf("patchpkg: $out is empty (is this being run from a nix build?)") |
| 43 | + } |
| 44 | + } |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +// Build applies patches to a package store path and puts the result in the |
| 49 | +// d.Out directory. |
| 50 | +func (d *DerivationBuilder) Build(ctx context.Context, pkgStorePath string) error { |
| 51 | + slog.DebugContext(ctx, "starting build of patched package", "pkg", pkgStorePath, "out", d.Out) |
| 52 | + |
| 53 | + var err error |
| 54 | + pkgFS := os.DirFS(pkgStorePath) |
| 55 | + for path, entry := range allFiles(pkgFS, ".") { |
| 56 | + switch { |
| 57 | + case entry.IsDir(): |
| 58 | + err = d.copyDir(path) |
| 59 | + case isSymlink(entry.Type()): |
| 60 | + err = d.copySymlink(pkgStorePath, path) |
| 61 | + default: |
| 62 | + err = d.copyFile(pkgFS, path) |
| 63 | + } |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + bash := filepath.Join(os.Getenv("bash"), "bin/bash") |
| 71 | + cmd := exec.CommandContext(ctx, bash, "-s") |
| 72 | + cmd.Stdin = bytes.NewReader(glibcPatchScript) |
| 73 | + cmd.Stdout = os.Stdout |
| 74 | + cmd.Stderr = os.Stderr |
| 75 | + return cmd.Run() |
| 76 | +} |
| 77 | + |
| 78 | +func (d *DerivationBuilder) copyDir(path string) error { |
| 79 | + osPath, err := filepath.Localize(path) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + return os.Mkdir(filepath.Join(d.Out, osPath), 0o777) |
| 84 | +} |
| 85 | + |
| 86 | +func (d *DerivationBuilder) copyFile(pkgFS fs.FS, path string) error { |
| 87 | + src, err := pkgFS.Open(path) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + defer src.Close() |
| 92 | + |
| 93 | + stat, err := src.Stat() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + // We only need to copy the executable permissions of a file. |
| 99 | + // Nix ends up making everything in the store read-only after |
| 100 | + // the build is done. |
| 101 | + perm := fs.FileMode(0o666) |
| 102 | + if isExecutable(stat.Mode()) { |
| 103 | + perm = fs.FileMode(0o777) |
| 104 | + } |
| 105 | + |
| 106 | + osPath, err := filepath.Localize(path) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + dstPath := filepath.Join(d.Out, osPath) |
| 111 | + |
| 112 | + dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, perm) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + defer dst.Close() |
| 117 | + |
| 118 | + _, err = io.Copy(dst, src) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + return dst.Close() |
| 123 | +} |
| 124 | + |
| 125 | +func (d *DerivationBuilder) copySymlink(pkgStorePath, path string) error { |
| 126 | + // The fs package doesn't support symlinks, so we need to convert the |
| 127 | + // path back to an OS path to see what it points to. |
| 128 | + osPath, err := filepath.Localize(path) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + target, err := os.Readlink(filepath.Join(pkgStorePath, osPath)) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + // TODO(gcurtis): translate absolute symlink targets to relative paths. |
| 137 | + return os.Symlink(target, filepath.Join(d.Out, osPath)) |
| 138 | +} |
| 139 | + |
| 140 | +// RegularFiles iterates over all files in fsys starting at root. It silently |
| 141 | +// ignores errors. |
| 142 | +func allFiles(fsys fs.FS, root string) iter.Seq2[string, fs.DirEntry] { |
| 143 | + return func(yield func(string, fs.DirEntry) bool) { |
| 144 | + _ = fs.WalkDir(fsys, root, func(path string, d fs.DirEntry, err error) error { |
| 145 | + if err == nil { |
| 146 | + if !yield(path, d) { |
| 147 | + return filepath.SkipAll |
| 148 | + } |
| 149 | + } |
| 150 | + return nil |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func isExecutable(mode fs.FileMode) bool { return mode&0o111 != 0 } |
| 156 | +func isSymlink(mode fs.FileMode) bool { return mode&fs.ModeSymlink != 0 } |
0 commit comments