Skip to content

Commit 4139672

Browse files
committed
lib: create instantiateDRvPath to separate eval steps from build steps
1 parent 313fbe4 commit 4139672

2 files changed

Lines changed: 43 additions & 21 deletions

File tree

src/lib.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,11 @@ type JobSpec struct {
2121
User string `json:"user"` // ssh user
2222
}
2323

24-
func buildClosure(flake string, spec JobSpec, builder string) (string, error) {
25-
expr := fmt.Sprintf("%s#%sConfigurations.%s.config.system.build.toplevel", flake, spec.Type, spec.Output)
26-
drvExpr := expr + ".drvPath"
27-
28-
// Step 1: Evaluate the .drv path locally
29-
data, err := runJSON("nix", "--extra-experimental-features", "nix-command flakes", "eval", "--raw", drvExpr)
30-
if err != nil {
31-
return "", fmt.Errorf("failed to evaluate drvPath for %s: %w", spec.Output, err)
32-
}
33-
drvPath := strings.TrimSpace(string(data))
34-
35-
// Step 2: Copy the .drv closure to the remote builder (if needed)
36-
if builder != "localhost" {
37-
if _, err := run("nix", "--extra-experimental-features", "nix-command flakes", "copy", "--to", "ssh-ng://"+builder, drvPath); err != nil {
38-
return "", fmt.Errorf("failed to copy .drv to %s: %w", builder, err)
39-
}
40-
}
41-
42-
// Step 3: Build the closure from the drv remotely or locally
24+
func buildClosure(spec JobSpec, drvPath string, builder string) (string, error) {
4325
var buildOut []byte
26+
var err error
27+
28+
// Build the closure locally or on the remote builder
4429
if builder != "localhost" {
4530
buildOut, err = runJSON("nix", "--extra-experimental-features", "nix-command flakes", "build", "--no-link", "--json", "--store", "ssh-ng://"+builder, drvPath+"^*")
4631
} else {
@@ -50,7 +35,7 @@ func buildClosure(flake string, spec JobSpec, builder string) (string, error) {
5035
return "", fmt.Errorf("failed to build %s on %s: %w", spec.Output, builder, err)
5136
}
5237

53-
// Step 4: Parse the output path
38+
// Parse the output path
5439
var results []BuildResult
5540
if err := json.Unmarshal(buildOut, &results); err != nil {
5641
return "", fmt.Errorf("invalid build JSON for %s: %w\nRaw: %s", spec.Output, err, string(buildOut))
@@ -113,6 +98,28 @@ func info(format string, args ...any) {
11398
fmt.Printf("[nynx] "+format+"\n", args...)
11499
}
115100

101+
func instantiateDrvPath(flake string, spec JobSpec, builder string) (string, error) {
102+
expr := fmt.Sprintf("%s#%sConfigurations.%s.config.system.build.toplevel", flake, spec.Type, spec.Output)
103+
drvExpr := expr + ".drvPath"
104+
105+
// Evaluate the .drv path locally
106+
data, err := runJSON("nix", "--extra-experimental-features", "nix-command flakes", "eval", "--raw", drvExpr)
107+
if err != nil {
108+
return "", fmt.Errorf("failed to evaluate drvPath for %s: %w", spec.Output, err)
109+
}
110+
111+
drvPath := strings.TrimSpace(string(data))
112+
113+
// Copy the .drv to the remote builder (if needed)
114+
if builder != "localhost" {
115+
if _, err := run("nix", "--extra-experimental-features", "nix-command flakes", "copy", "--to", "ssh-ng://"+builder, drvPath); err != nil {
116+
return "", fmt.Errorf("failed to copy .drv to %s: %w", builder, err)
117+
}
118+
}
119+
120+
return drvPath, nil
121+
}
122+
116123
func loadDeploymentSpec(cfg string) (map[string]JobSpec, error) {
117124
// Nix -> JSON
118125
data, err := runJSON("nix", "eval", "--json", "-f", cfg)

src/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,28 @@ func main() {
111111
verboseInfo(verbose, "✔ Operations validated.")
112112
}
113113

114+
verboseInfo(verbose, "Instantiating drvPath(s) for %d job(s)...", len(jobs))
115+
116+
drvPaths := make(map[string]string, len(jobs))
117+
for name, spec := range jobs {
118+
verboseInfo(verbose, "Instantiating drvPath for %s#%s...", flake, spec.Output)
119+
120+
drv, err := instantiateDrvPath(flake, spec, buildHost)
121+
if err != nil {
122+
fatal("Failed to instantiate drvPath for job '%s': %v", name, err)
123+
}
124+
drvPaths[name] = drv
125+
126+
info("✔ Instantiated drvPath for %s#%s.", flake, spec.Output)
127+
}
128+
114129
verboseInfo(verbose, "Building closures for %d job(s)...", len(jobs))
115130

116131
outs := make(map[string]string, len(jobs))
117132
for name, spec := range jobs {
118133
verboseInfo(verbose, "Building %s#%s...", flake, spec.Output)
119134

120-
out, err := buildClosure(flake, spec, buildHost)
135+
out, err := buildClosure(spec, drvPaths[name], buildHost)
121136
if err != nil {
122137
fatal("Failure building closures: %v", err)
123138
}

0 commit comments

Comments
 (0)