Skip to content

Commit 4042151

Browse files
authored
[nixpkgs version] change field from version to commit; add validation (#256)
## Summary Changing the field name inside `Config.Nixpkgs` from `version` to `commit`. The motivation stems from the realization that even `YY.MM` stable channels of nixpkgs have periodic updates (mostly for security), which means the commit-hash tracked by the `nixos-stable` branch changes. To simplify the product design for now, and to avoid introducing a lockfile, we change `version` to be the unambiguous `commit`. For users who would like to track the nixpkgs commit from `search.nixos.org` webpage, they can refer to `status.nixos.org` and use the commit for the nixpkgs channel they want. ## How was it tested? `go test ./...` b/c added a testcase for the validation function. ``` > cd testdata/nodejs/nodejs-18 > 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 ```
1 parent 9a50fac commit 4042151

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

config.go

+43-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Config struct {
4242

4343
// Nixpkgs specifies the repository to pull packages from
4444
Nixpkgs struct {
45-
Version string `json:"version,omitempty"`
45+
Commit string `json:"commit,omitempty"`
4646
} `json:"nixpkgs,omitempty"`
4747
}
4848

@@ -51,8 +51,7 @@ type Stage struct {
5151
Command string `cue:"string" json:"command"`
5252
}
5353

54-
// ReadConfig reads a devbox config file.
55-
func ReadConfig(path string) (*Config, error) {
54+
func readConfig(path string) (*Config, error) {
5655
cfg := &Config{}
5756
err := cuecfg.ParseFile(path, cfg)
5857
if err != nil {
@@ -61,15 +60,27 @@ func ReadConfig(path string) (*Config, error) {
6160
return cfg, nil
6261
}
6362

63+
// ReadConfig reads a devbox config file, and validates it.
64+
func ReadConfig(path string) (*Config, error) {
65+
cfg, err := readConfig(path)
66+
if err != nil {
67+
return nil, err
68+
}
69+
if err := validateConfig(cfg); err != nil {
70+
return nil, err
71+
}
72+
return cfg, err
73+
}
74+
6475
func upgradeConfig(cfg *Config, absFilePath string) error {
65-
if cfg.Nixpkgs.Version == "" && featureflag.Get(featureflag.NixpkgVersion).Enabled() {
76+
if cfg.Nixpkgs.Commit == "" && featureflag.Get(featureflag.NixpkgVersion).Enabled() {
6677
// For now, we add the hardcoded value corresponding to the commit hash as of 2022-08-16 in:
6778
// `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
6879
// In the near future, this will be changed to the commit-hash of the unstable tag in nixpkgs github repository
6980
const defaultCommitHash = "af9e00071d0971eb292fd5abef334e66eda3cb69"
7081
debug.Log("Missing nixpkgs.version from config, so adding the default value of %s", defaultCommitHash)
7182

72-
cfg.Nixpkgs.Version = defaultCommitHash
83+
cfg.Nixpkgs.Commit = defaultCommitHash
7384
return WriteConfig(absFilePath, cfg)
7485
}
7586
return nil
@@ -273,3 +284,30 @@ func missingConfigError(path string, didCheckParents bool) error {
273284

274285
return usererr.New("No devbox.json found in %s%s. Did you run `devbox init` yet?", path, parentDirCheckAddendum)
275286
}
287+
288+
func validateConfig(cfg *Config) error {
289+
290+
fns := [](func(cfg *Config) error){validateNixpkg}
291+
for _, fn := range fns {
292+
if err := fn(cfg); err != nil {
293+
return err
294+
}
295+
}
296+
return nil
297+
}
298+
299+
func validateNixpkg(cfg *Config) error {
300+
if cfg.Nixpkgs.Commit == "" {
301+
return nil
302+
}
303+
304+
const commitLength = 40
305+
if len(cfg.Nixpkgs.Commit) != commitLength {
306+
return usererr.New(
307+
"Expected nixpkgs.commit to be of length %d but it has length %d",
308+
commitLength,
309+
len(cfg.Nixpkgs.Commit),
310+
)
311+
}
312+
return nil
313+
}

config_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,32 @@ func TestFindConfigDirAtPath(t *testing.T) {
381381
})
382382
}
383383
}
384+
385+
func TestNixpkgsValidation(t *testing.T) {
386+
testCases := map[string]struct {
387+
commit string
388+
isErrant bool
389+
}{
390+
"invalid_nixpkg_commit": {"1234545", true},
391+
"valid_nixpkg_commit": {"af9e00071d0971eb292fd5abef334e66eda3cb69", false},
392+
}
393+
394+
for name, testCase := range testCases {
395+
t.Run(name, func(t *testing.T) {
396+
assert := assert.New(t)
397+
398+
err := validateNixpkg(&Config{
399+
Nixpkgs: struct {
400+
Commit string `json:"commit,omitempty"`
401+
}{
402+
Commit: testCase.commit,
403+
},
404+
})
405+
if testCase.isErrant {
406+
assert.Error(err)
407+
} else {
408+
assert.NoError(err)
409+
}
410+
})
411+
}
412+
}

0 commit comments

Comments
 (0)