Skip to content

Commit 402a343

Browse files
authored
[nipkgs version] add feature flag and write to devbox.json if missing (#252)
## Summary We add `nixpkgs.version` to `devbox.json` if it is missing. ## How was it tested? ``` > cd testdata/rust/rust-stable # control > DEVBOX_DEBUG=1 devbox shell > git diff devbox.json # no change as expected > DEVBOX_FEATURE_NIXPKG_VERSION=1 DEVBOX_DEBUG=1 devbox shell > git diff devbox.json diff --git a/testdata/rust/rust-stable/devbox.json b/testdata/rust/rust-stable/devbox.json index 89b7c0d..028385d 100644 --- a/testdata/rust/rust-stable/devbox.json +++ b/testdata/rust/rust-stable/devbox.json @@ -7,5 +7,8 @@ "init_hook": [ "source init-shell.sh" ] + }, + "nixpkgs": { + "version": "af9e00071d0971eb292fd5abef334e66eda3cb69" } -} +} \ No newline at end of file ```
1 parent d41ec67 commit 402a343

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package featureflag
2+
3+
const NixpkgVersion = "NIXPKG_VERSION"
4+
5+
func init() {
6+
disabled(NixpkgVersion)
7+
}

config.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"unicode"
1313

1414
"github.com/pkg/errors"
15+
"go.jetpack.io/devbox/boxcli/featureflag"
1516
"go.jetpack.io/devbox/boxcli/usererr"
1617
"go.jetpack.io/devbox/cuecfg"
1718
"go.jetpack.io/devbox/debug"
@@ -21,7 +22,7 @@ import (
2122
// Config defines a devbox environment as JSON.
2223
type Config struct {
2324
// Packages is the slice of Nix packages that devbox makes available in
24-
// its environment.
25+
// its environment. Deliberately do not omitempty.
2526
Packages []string `cue:"[...string]" json:"packages"`
2627
// InstallStage defines the actions that should be taken when
2728
// installing language-specific libraries.
@@ -38,6 +39,11 @@ type Config struct {
3839
// InitHook contains commands that will run at shell startup.
3940
InitHook ConfigShellCmds `json:"init_hook,omitempty"`
4041
} `json:"shell,omitempty"`
42+
43+
// Nixpkgs specifies the repository to pull packages from
44+
Nixpkgs struct {
45+
Version string `json:"version,omitempty"`
46+
} `json:"nixpkgs,omitempty"`
4147
}
4248

4349
// This contains a subset of fields from plansdk.Stage
@@ -55,6 +61,20 @@ func ReadConfig(path string) (*Config, error) {
5561
return cfg, nil
5662
}
5763

64+
func upgradeConfig(cfg *Config, absFilePath string) error {
65+
if cfg.Nixpkgs.Version == "" && featureflag.Get(featureflag.NixpkgVersion).Enabled() {
66+
// For now, we add the hardcoded value corresponding to the commit hash as of 2022-08-16 in:
67+
// `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
68+
// In the near future, this will be changed to the commit-hash of the unstable tag in nixpkgs github repository
69+
const defaultCommitHash = "af9e00071d0971eb292fd5abef334e66eda3cb69"
70+
debug.Log("Missing nixpkgs.version from config, so adding the default value of %s", defaultCommitHash)
71+
72+
cfg.Nixpkgs.Version = defaultCommitHash
73+
return WriteConfig(absFilePath, cfg)
74+
}
75+
return nil
76+
}
77+
5878
// WriteConfig saves a devbox config file.
5979
func WriteConfig(path string, cfg *Config) error {
6080
return cuecfg.WriteFile(path, cfg)

devbox.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func Open(dir string, writer io.Writer) (*Devbox, error) {
6969
return nil, errors.WithStack(err)
7070
}
7171

72+
if err = upgradeConfig(cfg, cfgPath); err != nil {
73+
return nil, err
74+
}
75+
7276
box := &Devbox{
7377
cfg: cfg,
7478
srcDir: cfgDir,

0 commit comments

Comments
 (0)