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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
package builder

import (
	"fmt"
	"sort"
)

// seccompFilterForHost returns the compiled seccomp BPF program for the host, or
// (nil, nil) on platforms without seccomp support.  Defined per-OS.

// BuildSpec is the JSON-friendly description of a build to run.  It is the
// user-facing input (e.g. from --build-json) that gets translated into a
// BuildRequest suitable for Spawn.
//
// Example for a trivial dep-free build:
//
//	{
//	  "builder": "/bin/busybox",
//	  "args":    ["busybox", "sh", "-c", "echo hello > \"$out\""],
//	  "env":     {"out": "/out/result", "PATH": "/bin"},
//	  "system":  "x86_64-linux",
//	  "sandboxPaths": [
//	    {"source": "/tmp/busybox-dir", "target": "/bin"},
//	    {"source": "/tmp/out-dir",     "target": "/out"}
//	  ]
//	}
type BuildSpec struct {
	// Builder is the path to the executable inside the sandbox.
	Builder string `json:"builder"`
	// Args is argv, including argv[0].
	Args []string `json:"args"`
	// Env is the environment as a map; converted to KEY=VALUE strings.
	Env map[string]string `json:"env"`
	// System is the derivation platform string (e.g. "x86_64-linux").
	System string `json:"system"`
	// SandboxPaths are additional host→sandbox bind mounts layered on top of
	// the standard dev/proc/etc synthesised by the launcher.
	SandboxPaths []BoundPath `json:"sandboxPaths"`
	// PrivateNetwork adds CLONE_NEWNET (only loopback is available).
	PrivateNetwork bool `json:"privateNetwork"`
	// StoreDir is the nix store directory (default: /nix/store).
	StoreDir string `json:"storeDir"`
	// NoSeccomp disables the seccomp syscall filter.  Lix disables seccomp when
	// libseccomp is unavailable or on user request; the namespace/chroot
	// isolation still applies.
	NoSeccomp bool `json:"noSeccomp"`
}

// ToRequest converts a BuildSpec into a BuildRequest ready for Spawn.
//
// chrootRoot is a host directory that becomes the sandbox root (must exist;
// caller is responsible for creating it via SetupChrootDir).
// buildDir is the working directory inside the sandbox (e.g. "/build").
func (s BuildSpec) ToRequest(chrootRoot, buildDir string) (*BuildRequest, error) {
	if s.Builder == "" {
		return nil, fmt.Errorf("BuildSpec.Builder must be set")
	}
	if chrootRoot == "" {
		return nil, fmt.Errorf("chrootRoot must be set")
	}

	storeDir := s.StoreDir
	if storeDir == "" {
		storeDir = "/nix/store"
	}

	// Convert env map to sorted KEY=VALUE slice for determinism.
	env := make([]string, 0, len(s.Env))
	for k, v := range s.Env {
		env = append(env, k+"="+v)
	}
	sort.Strings(env)

	sandbox := &Sandbox{
		Paths:           s.SandboxPaths,
		PrivateNetwork:  s.PrivateNetwork,
		ChrootRootDir:   chrootRoot,
		StoreDir:        storeDir,
		SandboxShmFlags: "size=50%",
		// uid/gid 0 inside the user namespace.  lix uses 1000/100 here, but
		// that requires the setns capability dance which is impossible in pure
		// Go (CGO_ENABLED=0); see the rationale in spawn_linux.go.  uid 0 inside
		// the namespace still maps to the unprivileged host euid.
		Uid: 0,
		Gid: 0,
	}

	lin := &LinuxPlatform{
		Platform: s.System,
		Sandbox:  sandbox,
	}

	// Compile the seccomp syscall filter unless disabled.  Arch selection keys
	// on the host's native system (which compat ABIs to filter), mirroring
	// compileSyscallFilter's use of nativeSystem in lix.
	if !s.NoSeccomp {
		filter, err := seccompFilterForHost()
		if err != nil {
			return nil, fmt.Errorf("compiling seccomp filter: %w", err)
		}
		lin.SeccompFilters = filter
	}

	return &BuildRequest{
		Builder:     s.Builder,
		Args:        s.Args,
		Environment: env,
		WorkingDir:  buildDir,
		Linux:       lin,
	}, nil
}