Skip to content

Commit 57c922e

Browse files
authored
[nixpkgs version] generate nix files containing the nixpkgs version from config (#258)
## Summary This PR will parameterize the `shell.nix`, `development.nix` and `runtime.nix` files to accept a `NixpkgsInfo` struct comprising of `url` and `sha256` fields. To pass the `NixPkgsInfo` struct to these templates, we need to add them to the `ShellPlan` and `BuildPlan` structs. I don't like this, but it seemed the most straightforward approach. ## How was it tested? with the hardcoded commit hash ``` ➜ DEVBOX_FEATURE_NIXPKG_VERSION=1 DEVBOX_DEBUG=0 devbox shell -- node --version Installing nix packages. This may take a while...done. Starting a devbox shell... v18.7.0 ``` From https://status.nixos.org, I got the most recent commit for the `nixos-22.05` channel: `b3a8f7ed267e0a7ed100eb7d716c9137ff120fe3`. I set this in the devbox.json file. ``` > DEVBOX_FEATURE_NIXPKG_VERSION=1 DEVBOX_DEBUG=0 devbox shell -- node --version Installing nix packages. This may take a while...done. Starting a devbox shell... v18.9.1 ``` This matches the version seen in https://search.nixos.org ``` > DEVBOX_FEATURE_NIXPKG_VERSION=1 devbox build && docker run devbox .... // omitted Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them the NODE_MAJOR_VERSION is 18 ```
1 parent 4042151 commit 57c922e

File tree

8 files changed

+81
-17
lines changed

8 files changed

+81
-17
lines changed

boxcli/plan.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ func runPlanCmd(_ *cobra.Command, args []string, flags planCmdFlags) error {
4747
enc.SetIndent("", " ")
4848
enc.SetEscapeHTML(false)
4949

50-
shellPlan := box.ShellPlan()
50+
shellPlan, err := box.ShellPlan()
51+
if err != nil {
52+
return err
53+
}
54+
5155
err = enc.Encode(shellPlan)
5256
if err != nil {
5357
return errors.WithStack(err)

devbox.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,18 @@ func (d *Devbox) Build(flags *docker.BuildFlags) error {
158158

159159
// Plan creates a plan of the actions that devbox will take to generate its
160160
// shell environment.
161-
func (d *Devbox) ShellPlan() *plansdk.ShellPlan {
161+
func (d *Devbox) ShellPlan() (*plansdk.ShellPlan, error) {
162162
userDefinedPkgs := d.cfg.Packages
163163
shellPlan := planner.GetShellPlan(d.srcDir, userDefinedPkgs)
164164
shellPlan.DevPackages = userDefinedPkgs
165165

166-
return shellPlan
166+
if nixpkgsInfo, err := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit); err != nil {
167+
return nil, err
168+
} else {
169+
shellPlan.NixpkgsInfo = nixpkgsInfo
170+
}
171+
172+
return shellPlan, nil
167173
}
168174

169175
// Plan creates a plan of the actions that devbox will take to generate its
@@ -174,7 +180,17 @@ func (d *Devbox) BuildPlan() (*plansdk.BuildPlan, error) {
174180
if err != nil {
175181
return nil, err
176182
}
177-
return plansdk.MergeUserBuildPlan(userPlan, buildPlan)
183+
plan, err := plansdk.MergeUserBuildPlan(userPlan, buildPlan)
184+
if err != nil {
185+
return nil, err
186+
}
187+
188+
if nixpkgsInfo, err := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit); err != nil {
189+
return nil, err
190+
} else {
191+
plan.NixpkgsInfo = nixpkgsInfo
192+
}
193+
return plan, nil
178194
}
179195

180196
// Generate creates the directory of Nix files and the Dockerfile that define
@@ -195,7 +211,10 @@ func (d *Devbox) Shell() error {
195211
if err := d.ensurePackagesAreInstalled(install); err != nil {
196212
return err
197213
}
198-
plan := d.ShellPlan()
214+
plan, err := d.ShellPlan()
215+
if err != nil {
216+
return err
217+
}
199218
profileDir, err := d.profileDir()
200219
if err != nil {
201220
return err
@@ -293,7 +312,11 @@ func (d *Devbox) convertToBuildPlan() *plansdk.BuildPlan {
293312
}
294313

295314
func (d *Devbox) generateShellFiles() error {
296-
return generateForShell(d.srcDir, d.ShellPlan())
315+
plan, err := d.ShellPlan()
316+
if err != nil {
317+
return err
318+
}
319+
return generateForShell(d.srcDir, plan)
297320
}
298321

299322
func (d *Devbox) generateBuildFiles() error {

devbox_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func testShell(t *testing.T, testPath string) {
5151
box.srcDir, err = filepath.Rel(currentDir, box.srcDir)
5252
assert.NoErrorf(err, "expect to construct relative path from %s relative to base %s", box.srcDir, currentDir)
5353

54-
shellPlan := box.ShellPlan()
54+
shellPlan, err := box.ShellPlan()
5555
assert.NoError(err, "devbox shell plan should not fail")
5656

5757
err = box.generateShellFiles()

planner/plansdk/plansdk.go

+32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/imdario/mergo"
1212
"github.com/pkg/errors"
13+
"go.jetpack.io/devbox/boxcli/featureflag"
1314
"go.jetpack.io/devbox/pkgslice"
1415
)
1516

@@ -27,6 +28,7 @@ type PlanError struct {
2728

2829
// Plan tells devbox how to start shell projects.
2930
type ShellPlan struct {
31+
NixpkgsInfo *NixpkgsInfo
3032
// Set by devbox.json
3133
DevPackages []string `cue:"[...string]" json:"dev_packages,omitempty"`
3234
// Init hook on shell start. Currently, Nginx and python pip planners need it for shell.
@@ -42,6 +44,8 @@ type ShellPlan struct {
4244

4345
// Plan tells devbox how to start build projects.
4446
type BuildPlan struct {
47+
NixpkgsInfo *NixpkgsInfo
48+
4549
NixOverlays []string `cue:"[...string]" json:"nix_overlays,omitempty"`
4650
// DevPackages is the slice of Nix packages that devbox makes available in
4751
// its development environment. They are also available in shell.
@@ -201,3 +205,31 @@ func FileExists(path string) bool {
201205
func WelcomeMessage(s string) string {
202206
return fmt.Sprintf(`echo "%s";`, s)
203207
}
208+
209+
// publicly visible so that json marshalling works
210+
type NixpkgsInfo struct {
211+
URL string
212+
213+
Sha256 string
214+
}
215+
216+
func GetNixpkgsInfo(commitHash string) (*NixpkgsInfo, error) {
217+
218+
// If the featureflag is OFF, then we fallback to the hardcoded commit
219+
// and ignore any value set in the devbox.json
220+
if !featureflag.Get(featureflag.NixpkgVersion).Enabled() {
221+
// Commit hash as of 2022-08-16
222+
// `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
223+
//
224+
// sha256 from:
225+
// nix-prefetch-url --unpack https://github.com/nixos/nixpkgs/archive/<commit-hash>.tar.gz
226+
return &NixpkgsInfo{
227+
URL: "https://github.com/nixos/nixpkgs/archive/af9e00071d0971eb292fd5abef334e66eda3cb69.tar.gz",
228+
Sha256: "1mdwy0419m5i9ss6s5frbhgzgyccbwycxm5nal40c8486bai0hwy",
229+
}, nil
230+
}
231+
232+
return &NixpkgsInfo{
233+
URL: fmt.Sprintf("https://github.com/nixos/nixpkgs/archive/%s.tar.gz", commitHash),
234+
}, nil
235+
}

testdata/rust/rust-stable/devbox.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"init_hook": [
88
"source init-shell.sh"
99
]
10+
},
11+
"nixpkgs": {
12+
"version": "22.05"
1013
}
1114
}

tmpl/development.nix.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
let
22
pkgs = import (fetchTarball {
3-
# Commit hash as of 2022-08-16
4-
# `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
5-
url = "https://github.com/nixos/nixpkgs/archive/af9e00071d0971eb292fd5abef334e66eda3cb69.tar.gz";
6-
sha256 = "1mdwy0419m5i9ss6s5frbhgzgyccbwycxm5nal40c8486bai0hwy";
3+
url = "{{ .NixpkgsInfo.URL }}";
4+
{{- if .NixpkgsInfo.Sha256 }}
5+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
6+
{{- end }}
77
}) {
88
{{- if .NixOverlays }}
99
overlays = [

tmpl/runtime.nix.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
let
22
pkgs = import (fetchTarball {
3-
# Commit hash as of 2022-08-16
4-
# `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
5-
url = "https://github.com/nixos/nixpkgs/archive/af9e00071d0971eb292fd5abef334e66eda3cb69.tar.gz";
6-
sha256 = "1mdwy0419m5i9ss6s5frbhgzgyccbwycxm5nal40c8486bai0hwy";
3+
url = "{{ .NixpkgsInfo.URL }}";
4+
{{- if .NixpkgsInfo.Sha256 }}
5+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
6+
{{- end }}
77
}) {
88
{{- if .NixOverlays }}
99
overlays = [

tmpl/shell.nix.tmpl

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ let
22
pkgs = import (fetchTarball {
33
# Commit hash as of 2022-08-16
44
# `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
5-
url = "https://github.com/nixos/nixpkgs/archive/af9e00071d0971eb292fd5abef334e66eda3cb69.tar.gz";
6-
sha256 = "1mdwy0419m5i9ss6s5frbhgzgyccbwycxm5nal40c8486bai0hwy";
5+
url = "{{ .NixpkgsInfo.URL }}";
6+
{{- if .NixpkgsInfo.Sha256 }}
7+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
8+
{{- end }}
79
}) {
810
{{- if .NixOverlays }}
911
overlays = [

0 commit comments

Comments
 (0)