Skip to content

Commit dfe0791

Browse files
authored
clean-up after nix-shell implementation (#256)
* remove default packages. nix-shell already has them * no need to whitelist env from host to keep. nix-shell does that * move env intitialization in BuildNixDependencies; ignore buildCommandPath for hash * fix target syntax * remove storePaths from Task, Run, execctl.Cmd * fix lint; remove unused variable * no need to has system env. it already exists in t.env which is hashed. ex system=x86_64-linux * fix store path is displayed on bob build * use custom nix shell expression to build both packages and files * remove unused funcs and params * fix bob build shows no output after nix-collect-garbage -d * fix env not loaded for build tasks of runs * add back nic cache assertion * improve nix install dependencies output * display progress with . on nix build * remove DependenciesToStorePathMap * remove unused StorePath * format with padding based on package name * tick every 5s * add . at start to not be empty for very fast builds * implement build progress struct * add back XDG_CACHE_HOME for whitelist env
1 parent e1b73dc commit dfe0791

File tree

26 files changed

+257
-246
lines changed

26 files changed

+257
-246
lines changed

bob/bob.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"runtime"
77

8-
"github.com/benchkram/bob/bob/global"
98
"github.com/benchkram/bob/pkg/auth"
109
"github.com/benchkram/bob/pkg/dockermobyutil"
1110
"github.com/benchkram/bob/pkg/usererror"
@@ -92,7 +91,6 @@ func newBob(opts ...Option) *B {
9291
}
9392
opt(b)
9493
}
95-
b.keepWhitelistEnv()
9694

9795
return b
9896
}
@@ -221,13 +219,3 @@ func (b *B) read() (err error) {
221219

222220
return nil
223221
}
224-
225-
// keepWhitelistEnv will keep whitelisted env variables
226-
// from local host
227-
func (b *B) keepWhitelistEnv() {
228-
for _, envKey := range global.EnvWhitelist {
229-
if value, exists := os.LookupEnv(envKey); exists {
230-
b.env = append(b.env, envKey+"="+value)
231-
}
232-
}
233-
}

bob/bobfile/bobfile.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ func initializeDependencies(dir string, taskDependencies []string, bobfile *Bobf
209209
})
210210
}
211211

212-
taskDeps = append(taskDeps, nix.DefaultPackages(bobfile.Nixpkgs)...)
213-
214212
return nix.UniqueDeps(taskDeps)
215213
}
216214

bob/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (b *B) Build(ctx context.Context, taskName string) (err error) {
3737
playbook.WithLocalStore(b.local),
3838
playbook.WithPushEnabled(b.enablePush),
3939
playbook.WithPullEnabled(b.enablePull),
40+
playbook.WitNixCache(b.Nix().cache),
4041
)
4142
errz.Fatal(err)
4243

bob/global/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package global
22

3-
// EnvWhitelist keep the whitelisted env variables when hermetic mode is on
4-
var EnvWhitelist = []string{"HOME", "XDG_CACHE_HOME"}
3+
// EnvWhitelist keep the whitelisted env variables for nix-shell
4+
var EnvWhitelist = []string{"XDG_CACHE_HOME", "NIX_SSL_CERT_FILE", "SSL_CERT_FILE"}

bob/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (b B) Install() (err error) {
3535
fmt.Println()
3636

3737
if len(allDeps) > 0 {
38-
_, err := b.nix.BuildDependencies(allDeps)
38+
err = b.nix.BuildDependencies(allDeps)
3939
if err != nil {
4040
return err
4141
}

bob/nix_builder.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bob
33
import (
44
"fmt"
55

6+
"github.com/benchkram/bob/pkg/envutil"
67
"github.com/benchkram/errz"
78

89
"github.com/benchkram/bob/bob/bobfile"
@@ -62,48 +63,38 @@ func (n *NixBuilder) BuildNixDependencies(ag *bobfile.Bobfile, buildTasksInPipel
6263
return usererror.Wrap(fmt.Errorf("nix is not installed on your system. Get it from %s", nix.DownloadURl()))
6364
}
6465

65-
nixDependencies, err := ag.BTasks.CollectNixDependenciesForTasks(buildTasksInPipeline)
66-
errz.Fatal(err)
67-
68-
runTasksDependencies, err := ag.RTasks.CollectNixDependenciesForTasks(runTasksInPipeline)
69-
errz.Fatal(err)
70-
nixDependencies = append(nixDependencies, runTasksDependencies...)
71-
72-
depStorePathMapping, err := nix.BuildDependencies(
73-
nix.UniqueDeps(append(nix.DefaultPackages(ag.Nixpkgs), nixDependencies...)),
74-
n.cache,
75-
)
76-
errz.Fatal(err)
77-
7866
// Resolve nix storePaths from dependencies
7967
// and rewrite the affected tasks.
8068
for _, name := range buildTasksInPipeline {
8169
t := ag.BTasks[name]
8270

8371
// construct used dependencies for this task
84-
deps := nix.DefaultPackages(ag.Nixpkgs)
72+
var deps []nix.Dependency
8573
deps = append(deps, t.Dependencies()...)
8674
deps = nix.UniqueDeps(deps)
8775

88-
storePaths, err := nix.DependenciesToStorePaths(deps, depStorePathMapping)
76+
t.SetNixpkgs(ag.Nixpkgs)
77+
78+
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs)
8979
errz.Fatal(err)
80+
t.SetEnv(envutil.Merge(nixShellEnv, t.Env()))
9081

91-
t.SetStorePaths(storePaths)
9282
ag.BTasks[name] = t
9383
}
9484

9585
for _, name := range runTasksInPipeline {
9686
t := ag.RTasks[name]
9787

9888
// construct used dependencies for this task
99-
deps := nix.DefaultPackages(ag.Nixpkgs)
89+
var deps []nix.Dependency
10090
deps = append(deps, t.Dependencies()...)
10191
deps = nix.UniqueDeps(deps)
10292

103-
storePaths, err := nix.DependenciesToStorePaths(deps, depStorePathMapping)
104-
errz.Fatal(err)
93+
t.SetNixpkgs(ag.Nixpkgs)
10594

106-
t.SetStorePaths(storePaths)
95+
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs)
96+
errz.Fatal(err)
97+
t.SetEnv(envutil.Merge(nixShellEnv, t.Env()))
10798

10899
ag.RTasks[name] = t
109100
}
@@ -112,6 +103,11 @@ func (n *NixBuilder) BuildNixDependencies(ag *bobfile.Bobfile, buildTasksInPipel
112103
}
113104

114105
// BuildDependencies builds the list of all nix deps
115-
func (n *NixBuilder) BuildDependencies(deps []nix.Dependency) (nix.DependenciesToStorePathMap, error) {
106+
func (n *NixBuilder) BuildDependencies(deps []nix.Dependency) error {
116107
return nix.BuildDependencies(deps, n.cache)
117108
}
109+
110+
// BuildEnvironment builds the environment with all nix deps
111+
func (n *NixBuilder) BuildEnvironment(deps []nix.Dependency, nixpkgs string) (_ []string, err error) {
112+
return nix.BuildEnvironment(deps, nixpkgs, n.cache)
113+
}

bob/playbook/build_internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (p *Playbook) build(ctx context.Context, task *bobtask.Task) (err error) {
119119
err = task.Clean()
120120
errz.Fatal(err)
121121

122-
err = task.Run(ctx, p.namePad)
122+
err = task.Run(ctx, p.namePad, p.nixCache)
123123
if err != nil {
124124
taskSuccessFul = false
125125
taskErr = err

bob/playbook/options.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package playbook
22

3-
import "github.com/benchkram/bob/pkg/store"
3+
import (
4+
"github.com/benchkram/bob/pkg/nix"
5+
"github.com/benchkram/bob/pkg/store"
6+
)
47

58
type Option func(p *Playbook)
69

@@ -45,3 +48,9 @@ func WithLocalStore(s store.Store) Option {
4548
p.localStore = s
4649
}
4750
}
51+
52+
func WitNixCache(c *nix.Cache) Option {
53+
return func(p *Playbook) {
54+
p.nixCache = c
55+
}
56+
}

bob/playbook/playbook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/benchkram/bob/bobtask/buildinfo"
1414
"github.com/benchkram/bob/bobtask/hash"
1515
"github.com/benchkram/bob/pkg/boberror"
16+
"github.com/benchkram/bob/pkg/nix"
1617
"github.com/benchkram/bob/pkg/store"
1718
"github.com/benchkram/bob/pkg/usererror"
1819
"github.com/benchkram/errz"
@@ -74,6 +75,9 @@ type Playbook struct {
7475

7576
// enablePull allows pulling artifacts from remote store
7677
enablePull bool
78+
79+
// nixCache stores the Nix store paths
80+
nixCache *nix.Cache
7781
}
7882

7983
func New(root string, opts ...Option) *Playbook {

bob/playbook/summary.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package playbook
22

33
import (
44
"fmt"
5-
"time"
65

76
"github.com/benchkram/bob/bobtask"
87
"github.com/benchkram/bob/pkg/boblog"
8+
"github.com/benchkram/bob/pkg/format"
99
"github.com/logrusorgru/aurora"
1010
)
1111

@@ -15,7 +15,7 @@ func (p *Playbook) summary(processedTasks []*bobtask.Task) {
1515
boblog.Log.V(1).Info("")
1616
boblog.Log.V(1).Info(aurora.Bold("● ● ● ●").BrightGreen().String())
1717

18-
t := fmt.Sprintf("Ran %d tasks in %s", len(processedTasks), displayDuration(p.ExecutionTime()))
18+
t := fmt.Sprintf("Ran %d tasks in %s", len(processedTasks), format.DisplayDuration(p.ExecutionTime()))
1919

2020
boblog.Log.V(1).Info(aurora.Bold(t).BrightGreen().String())
2121
for _, t := range processedTasks {
@@ -28,21 +28,11 @@ func (p *Playbook) summary(processedTasks []*bobtask.Task) {
2828
execTime := ""
2929
status := stat.State()
3030
if status != StateNoRebuildRequired {
31-
execTime = fmt.Sprintf("\t(%s)", displayDuration(stat.ExecutionTime()))
31+
execTime = fmt.Sprintf("\t(%s)", format.DisplayDuration(stat.ExecutionTime()))
3232
}
3333

3434
taskName := t.Name()
3535
boblog.Log.V(1).Info(fmt.Sprintf(" %-*s\t%s%s", p.namePad, taskName, status.Summary(), execTime))
3636
}
3737
boblog.Log.V(1).Info("")
3838
}
39-
40-
func displayDuration(d time.Duration) string {
41-
if d.Minutes() > 1 {
42-
return fmt.Sprintf("%.1fm", float64(d)/float64(time.Minute))
43-
}
44-
if d.Seconds() > 1 {
45-
return fmt.Sprintf("%.1fs", float64(d)/float64(time.Second))
46-
}
47-
return fmt.Sprintf("%.1fms", float64(d)/float64(time.Millisecond)+0.1) // add .1ms so that it never returns 0.0ms
48-
}

bobrun/run.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ type Run struct {
4545
// in the order which they need to be added to PATH
4646
dependencies []nix.Dependency
4747

48-
// storePaths contain /nix/store/* paths
49-
// in the order which they need to be added to PATH
50-
storePaths []string
48+
// URL of nixpkgs used. If empty, will use local <nixpkgs> channel
49+
nixpkgs string
5150

5251
dir string
5352

@@ -66,6 +65,10 @@ func (r *Run) SetEnv(env []string) {
6665
r.env = env
6766
}
6867

68+
func (r *Run) SetNixpkgs(nixpkgs string) {
69+
r.nixpkgs = nixpkgs
70+
}
71+
6972
func (r *Run) Env() []string {
7073
return r.env
7174
}
@@ -89,10 +92,6 @@ func (r *Run) SetDependencies(dependencies []nix.Dependency) {
8992
r.dependencies = dependencies
9093
}
9194

92-
func (r *Run) SetStorePaths(storePaths []string) {
93-
r.storePaths = storePaths
94-
}
95-
9695
func (r *Run) UnmarshalYAML(value *yaml.Node) (err error) {
9796
defer errz.Recover(&err)
9897

@@ -142,7 +141,6 @@ func (r *Run) Command(ctx context.Context) (rc ctl.Command, err error) {
142141
r.name,
143142
r.Path,
144143
execctl.WithEnv(r.Env()),
145-
execctl.WithStorePaths(r.storePaths),
146144
)
147145
errz.Fatal(err)
148146
case RunTypeCompose:

bobrun/with_init.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"sync"
1111
"time"
1212

13-
"github.com/benchkram/bob/pkg/envutil"
14-
"github.com/benchkram/bob/pkg/nix"
1513
"github.com/benchkram/errz"
1614
"github.com/logrusorgru/aurora"
1715
"mvdan.cc/sh/expand"
@@ -231,11 +229,6 @@ func (rw *WithInit) shexec(ctx context.Context, cmds []string) (err error) {
231229
errz.Fatal(err)
232230

233231
env := rw.run.Env()
234-
if len(rw.run.storePaths) > 0 {
235-
nixShellEnv, err := nix.BuildEnvironment(rw.run.dependencies)
236-
errz.Fatal(err)
237-
env = envutil.Merge(nixShellEnv, env)
238-
}
239232
pr, pw, err := os.Pipe()
240233
errz.Fatal(err)
241234

bobtask/hash_in.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import (
66
"errors"
77
"fmt"
88
"os"
9-
"runtime"
109
"sort"
1110
"strings"
1211

13-
"github.com/benchkram/bob/bob/global"
1412
"github.com/benchkram/bob/pkg/boblog"
1513
"github.com/benchkram/bob/pkg/sliceutil"
1614
"gopkg.in/yaml.v2"
@@ -67,26 +65,14 @@ func (t *Task) computeInputHash() (taskHash hash.In, err error) {
6765
}
6866

6967
// Hash the environment
70-
env := filterOutWhitelistEnv(t.env)
68+
env := filterEnvOfIgnores(t.env)
7169
sort.Strings(env)
7270
environment := strings.Join(env, ",")
7371
err = h.AddBytes(bytes.NewBufferString(environment))
7472
if err != nil {
7573
return taskHash, fmt.Errorf("failed to write description hash: %w", err)
7674
}
7775

78-
// Hash store paths
79-
err = h.AddBytes(bytes.NewBufferString(strings.Join(t.storePaths, "")))
80-
if err != nil {
81-
return taskHash, fmt.Errorf("failed to write store paths hash: %w", err)
82-
}
83-
84-
// Hash system env
85-
err = h.AddBytes(bytes.NewBufferString(strings.Join([]string{runtime.GOOS, runtime.GOARCH}, "-")))
86-
if err != nil {
87-
return taskHash, fmt.Errorf("failed to write system env hash: %w", err)
88-
}
89-
9076
hashIn := hash.In(hex.EncodeToString(h.Sum()))
9177

9278
// store hash for reuse
@@ -97,11 +83,13 @@ func (t *Task) computeInputHash() (taskHash hash.In, err error) {
9783
return hashIn, nil
9884
}
9985

100-
func filterOutWhitelistEnv(env []string) []string {
86+
func filterEnvOfIgnores(env []string) []string {
87+
ignore := []string{"buildCommandPath"}
88+
10189
var result []string
10290
for _, v := range env {
10391
pair := strings.SplitN(v, "=", 2)
104-
if sliceutil.Contains(global.EnvWhitelist, pair[0]) {
92+
if sliceutil.Contains(ignore, pair[0]) {
10593
continue
10694
}
10795
result = append(result, v)

bobtask/run.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import (
1919
"github.com/benchkram/errz"
2020
)
2121

22-
func (t *Task) Run(ctx context.Context, namePad int) (err error) {
22+
func (t *Task) Run(ctx context.Context, namePad int, nixCache *nix.Cache) (err error) {
2323
defer errz.Recover(&err)
2424

25-
env := t.Env()
26-
if len(t.storePaths) > 0 {
27-
nixShellEnv, err := nix.BuildEnvironment(t.dependencies)
25+
if len(t.Env()) == 0 {
26+
nixShellEnv, err := nix.BuildEnvironment(t.dependencies, t.nixpkgs, nixCache)
2827
errz.Fatal(err)
29-
env = envutil.Merge(nixShellEnv, env)
28+
t.SetEnv(envutil.Merge(nixShellEnv, t.env))
3029
}
3130

3231
for _, run := range t.cmds {
@@ -61,7 +60,7 @@ func (t *Task) Run(ctx context.Context, namePad int) (err error) {
6160
r, err := interp.New(
6261
interp.Params("-e"),
6362
interp.Dir(t.dir),
64-
interp.Env(expand.ListEnviron(env...)),
63+
interp.Env(expand.ListEnviron(t.Env()...)),
6564
interp.StdIO(os.Stdin, pw, pw),
6665
)
6766

bobtask/task.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ type Task struct {
9292
// in the order which they need to be added to PATH
9393
dependencies []nix.Dependency
9494

95-
// storePaths contain /nix/store/* paths
96-
// in the order which they need to be added to PATH
97-
storePaths []string
95+
// URL of nixpkgs used. If empty, will use local <nixpkgs> channel
96+
nixpkgs string
9897
}
9998

10099
type TargetEntry interface{}

0 commit comments

Comments
 (0)