-
Notifications
You must be signed in to change notification settings - Fork 9
/
nix.go
79 lines (63 loc) · 2.26 KB
/
nix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package host
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/tasks"
)
const nixosDynsym = ".dynloader"
func EnsureNixosPatched(ctx context.Context, binary string) (string, error) {
// We keep a symlink to the interpreter go/bin/go was patched with. If the link
// exists, then the interpreter exists, and no additional patching is required.
dynlink := binary + nixosDynsym
patchedGoBin := binary + ".patched"
if _, err := os.Readlink(dynlink); err == nil {
return patchedGoBin, nil
}
return tasks.Return(ctx, tasks.Action("nixos.patch-elf").Arg("path", binary), func(ctx context.Context) (string, error) {
// Assume all NixOS versions have systemd, which is true for all recent versions.
systemCtl, err := output(ctx, "which", "systemctl")
if err != nil {
return "", err
}
existingInterpreter, err := output(ctx, "nix-shell", "-p", "patchelf", "--run", fmt.Sprintf("patchelf --print-interpreter %s", systemCtl))
if err != nil {
return "", err
}
cint := strings.TrimSpace(string(existingInterpreter))
if _, err := output(ctx, "nix-shell", "-p", "patchelf", "--run", fmt.Sprintf("patchelf --set-interpreter %s --output %s %s", cint, patchedGoBin, binary)); err != nil {
return "", err
}
if err := os.Remove(dynlink); err != nil && os.IsNotExist(err) {
fmt.Fprintf(console.Errors(ctx), "Failed to remove %s: %v", dynlink, err)
}
// Remember which interpreter we used, and if it doesn't exist, re-patch.
if err := os.Symlink(cint, dynlink); err != nil {
return "", err
}
return patchedGoBin, nil
})
}
func IsNixOS() bool {
_, err := os.Stat("/etc/NIXOS")
return err == nil
}
func output(ctx context.Context, args ...string) ([]byte, error) {
c := exec.CommandContext(ctx, args[0], args[1:]...)
var b bytes.Buffer
c.Stdout = &b
c.Stderr = console.Stderr(ctx)
c.Stdin = nil
if err := c.Run(); err != nil {
return nil, fnerrors.InternalError("failed to invoke: %w", err)
}
return b.Bytes(), nil
}