Skip to content

Commit

Permalink
unify build development
Browse files Browse the repository at this point in the history
  • Loading branch information
Equanox committed Feb 7, 2024
1 parent 173b3bd commit c927ab4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bob/bobfile/bobfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Bobfile struct {

// Shell specifies a shell.nix file as usually used by nix-shell.
// This is mutualy exclusive with Dependencies.
Shell string `yaml:"shell"`
ShellDotNix string `yaml:"shell"`

// Nixpkgs specifies an optional nixpkgs source.
Nixpkgs string `yaml:"nixpkgs"`
Expand Down
68 changes: 39 additions & 29 deletions bob/nix-builder/nix_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package nixbuilder
import (
"fmt"

"github.com/benchkram/bob/pkg/boblog"
"github.com/benchkram/bob/pkg/envutil"
"github.com/benchkram/bob/pkg/file"
"github.com/benchkram/bob/pkg/filehash"
"github.com/benchkram/errz"

"github.com/benchkram/bob/bob/bobfile"
Expand Down Expand Up @@ -91,28 +88,32 @@ func (n *NB) BuildNixDependencies(ag *bobfile.Bobfile, buildTasksInPipeline, run
return usererror.Wrap(fmt.Errorf("nix is not installed on your system. Get it from %s", nix.DownloadURl()))
}

if ag.Shell != "" {
boblog.Log.Info(fmt.Sprintf("using shell file %s, ignoring dependencies", ag.Shell))
// hash shell file
if !file.Exists(ag.Shell) {
return usererror.Wrap(fmt.Errorf("shell file %s does not exist", ag.Shell))
}

nixShellEnv, err := nix.NixShell(ag.Shell)
errz.Fatal(err)

hash, err := filehash.Hash(ag.Shell)
errz.Fatal(err)

n.envStore[envutil.Hash(hash)] = nixShellEnv
for _, name := range buildTasksInPipeline {
t := ag.BTasks[name]
t.SetEnvID(envutil.Hash(hash))
ag.BTasks[name] = t
}
// TODO: run tasks
return nil
var shellDotNix *string
if ag.ShellDotNix != "" {
shellDotNix = &ag.ShellDotNix
}
// if ag.Shell != "" {
// boblog.Log.Info(fmt.Sprintf("using shell file %s, ignoring dependencies", ag.Shell))
// // hash shell file
// if !file.Exists(ag.Shell) {
// return usererror.Wrap(fmt.Errorf("shell file %s does not exist", ag.Shell))
// }

// nixShellEnv, err := nix.NixShell(ag.Shell)
// errz.Fatal(err)

// hash, err := filehash.Hash(ag.Shell)
// errz.Fatal(err)

// n.envStore[envutil.Hash(hash)] = nixShellEnv
// for _, name := range buildTasksInPipeline {
// t := ag.BTasks[name]
// t.SetEnvID(envutil.Hash(hash))
// ag.BTasks[name] = t
// }
// // TODO: run tasks
// return nil
// }

// Resolve nix storePaths from dependencies
// and rewrite the affected tasks.
Expand All @@ -130,7 +131,9 @@ func (n *NB) BuildNixDependencies(ag *bobfile.Bobfile, buildTasksInPipeline, run
errz.Fatal(err)

if _, ok := n.envStore[envutil.Hash(hash)]; !ok {
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs)
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs,
BuildEnvironmentArgs{ShellDotNix: shellDotNix},
)
errz.Fatal(err)
n.envStore[envutil.Hash(hash)] = nixShellEnv
}
Expand All @@ -157,7 +160,9 @@ func (n *NB) BuildNixDependencies(ag *bobfile.Bobfile, buildTasksInPipeline, run
errz.Fatal(err)

if _, ok := environmentCache[hash]; !ok {
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs)
nixShellEnv, err := n.BuildEnvironment(deps, ag.Nixpkgs,
BuildEnvironmentArgs{ShellDotNix: shellDotNix},
)
errz.Fatal(err)
environmentCache[hash] = nixShellEnv
}
Expand All @@ -174,12 +179,17 @@ func (n *NB) BuildDependencies(deps []nix.Dependency) error {
return nix.BuildDependencies(deps, n.cache)
}

type BuildEnvironmentArgs struct {
ShellDotNix *string
}

// BuildEnvironment builds the environment with all nix deps
func (n *NB) BuildEnvironment(deps []nix.Dependency, nixpkgs string) (_ []string, err error) {
func (n *NB) BuildEnvironment(deps []nix.Dependency, nixpkgs string, args BuildEnvironmentArgs) (_ []string, err error) {
return nix.BuildEnvironment(deps, nixpkgs,
nix.BuildEnvironmentArgs{
Cache: n.cache,
ShellCache: n.shellCache,
Cache: n.cache,
ShellCache: n.shellCache,
ShellDotNix: args.ShellDotNix,
},
)
}
Expand Down
9 changes: 8 additions & 1 deletion cli/cmd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"

"github.com/benchkram/bob/bob"
nixbuilder "github.com/benchkram/bob/bob/nix-builder"
"github.com/benchkram/bob/pkg/boblog"
"github.com/benchkram/bob/pkg/filehash"
"github.com/benchkram/bob/pkg/usererror"
Expand Down Expand Up @@ -77,7 +78,13 @@ func runEnv(taskname string) {
}
task = bobfile.BTasks[taskname]

taskEnv, err := b.Nix().BuildEnvironment(task.Dependencies(), task.Nixpkgs())
var shellDotNix *string
if bobfile.ShellDotNix != "" {
shellDotNix = &bobfile.ShellDotNix
}
taskEnv, err := b.Nix().BuildEnvironment(task.Dependencies(), task.Nixpkgs(),
nixbuilder.BuildEnvironmentArgs{ShellDotNix: shellDotNix},
)
errz.Fatal(err)

for _, e := range taskEnv {
Expand Down
17 changes: 13 additions & 4 deletions pkg/nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,19 @@ type BuildEnvironmentArgs struct {
// List of dependencies imported from the
// shell.nix file. Bob doesn't parse the imports
// therefore it's required to pass all dependencies.
//
// TODO: let the hases be computes from the outside
// as BuilEnvironment is called for each task.
// TODO:
// TODO:
// TODO:
NixShellImports []string
}

// BuildEnvironment is running nix-shell for a list of dependencies and fetch its whole environment
//
// nix-shell --pure --keep NIX_SSL_CERT_FILE --keep SSL_CERT_FILE -p --command 'env' -E nixExpressionFromDeps
// nix-shell --pure --keep NIX_SSL_CERT_FILE --keep SSL_CERT_FILE -p --command 'env' --expr 'with import <nixpkgs> { }; [pkg1, pkg2]'
// nix-shell --pure --keep NIX_SSL_CERT_FILE --keep SSL_CERT_FILE -p --command 'env' shell.nix
//
// nix shell can be started with empty list of packages so this method works with empty deps as well
func BuildEnvironment(deps []Dependency, nixpkgs string, args BuildEnvironmentArgs) (_ []string, err error) {
Expand All @@ -252,18 +259,20 @@ func BuildEnvironment(deps []Dependency, nixpkgs string, args BuildEnvironmentAr
err = BuildDependencies(deps, cache)
errz.Fatal(err)

expression := nixExpression(deps, nixpkgs)

var arguments []string
for _, envKey := range global.EnvWhitelist {
if _, exists := os.LookupEnv(envKey); exists {
arguments = append(arguments, []string{"--keep", envKey}...)
}
}
arguments = append(arguments, []string{"--command", "env"}...)
arguments = append(arguments, []string{"--expr", expression}...)

// if shellDotNix is set, use it as the shell.nix file (must be at cmd's end)
// otherwise use the expression containing the packages.
if shellDotNix != nil {
arguments = append(arguments, *shellDotNix)
} else {
arguments = append(arguments, []string{"--expr", nixExpression(deps, nixpkgs)}...)
}

cmd := exec.Command("nix-shell", "--pure")
Expand Down

0 comments on commit c927ab4

Please sign in to comment.