-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.go
75 lines (68 loc) · 1.74 KB
/
build.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(buildCmd)
}
var buildCmd = &cobra.Command{
Use: `build`,
Short: `Builds systems for deployment`,
Long: `Build will build NixOS systems locally for deployment.`,
RunE: runBuild,
}
func runBuild(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
systems, err := inv.matchSystems(args...)
if err != nil {
return err
}
err = inv.build(ctx, systems...)
if err != nil {
return err
}
results := make(map[string]string, len(systems))
for _, system := range systems {
results[system] = inv.Systems[system].Result
}
return json.NewEncoder(os.Stdout).Encode(inv)
}
// build builds systems for a specific target, such as "system" for a NixOS system or "vhd" for a disk image.
func (inv *Inventory) build(ctx context.Context, systems ...string) error {
for _, system := range systems {
err := inv.Systems[system].build(ctx, system)
if err != nil {
return fmt.Errorf(`%w while building %q`, err, system)
}
}
return nil
}
func (cfg *System) build(ctx context.Context, system string) error {
if cfg.Result != `` {
return nil // already built.
}
inform(ctx, `building %q`, system)
link := filepath.Join(tmp, `system-`+system)
args := []string{`build`, `--out-link`, link, `--include`, `deployment=` + deploymentPath}
paths := inv.systemPaths(system)
for _, path := range paths {
args = append(args, `--include`, path)
}
args = append(args, `--argstr`, `name`, system)
args = append(args, `(import <hive/build.nix>)`)
_, err := execNix(ctx, args...)
if err != nil {
return err
}
result, err := os.Readlink(link)
if err != nil {
return err
}
cfg.Result = result
return err
}