Skip to content

Commit fa4aa27

Browse files
authored
plansdk: use nixpkgs mirror when in cloud VM (#521)
This change is a bit hacky. I'd like to eventually clean this up to work better with the various nix commands. It also reverts the previous env var change since it's no longer needed. When generating the various *.nix files, check to see if the cloud mirror is reachable. If it is, download nixpkgs archives from there instead of GitHub. Also update the default commit hash to be the current nixpkgs unstable, which is also in the cache. This guarantees that any new devbox projects created after this release will hit the cache in the cloud.
1 parent e7427cf commit fa4aa27

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

devbox.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
}
1313
},
1414
"nixpkgs": {
15-
"commit": "aa7e3b940ad90ba58a5d8c0a2269ec557c9ecc70"
15+
"commit": "3954218cf613eba8e0dcefa9abe337d26bc48fd0"
1616
}
1717
}

internal/impl/tmpl/development.nix.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
let
2-
mirrorURL = builtins.getEnv "DEVBOX_NIXPKGS_URL";
32
pkgs = import (fetchTarball {
4-
url = if mirrorURL == "" then "{{ .NixpkgsInfo.URL }}" else mirrorURL;
3+
url = "{{ .NixpkgsInfo.URL }}";
4+
{{- if .NixpkgsInfo.Sha256 }}
5+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
6+
{{- end }}
57
}) {
68
{{- if .NixOverlays }}
79
overlays = [

internal/impl/tmpl/runtime.nix.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
let
2-
mirrorURL = builtins.getEnv "DEVBOX_NIXPKGS_URL";
32
pkgs = import (fetchTarball {
4-
url = if mirrorURL == "" then "{{ .NixpkgsInfo.URL }}" else mirrorURL;
3+
url = "{{ .NixpkgsInfo.URL }}";
4+
{{- if .NixpkgsInfo.Sha256 }}
5+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
6+
{{- end }}
57
}) {
68
{{- if .NixOverlays }}
79
overlays = [

internal/impl/tmpl/shell.nix.tmpl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
let
2-
mirrorURL = builtins.getEnv "DEVBOX_NIXPKGS_URL";
32
pkgs = import (fetchTarball {
4-
url = if mirrorURL == "" then "{{ .NixpkgsInfo.URL }}" else mirrorURL;
3+
# Commit hash as of 2022-08-16
4+
# `git ls-remote https://github.com/nixos/nixpkgs nixos-unstable`
5+
url = "{{ .NixpkgsInfo.URL }}";
6+
{{- if .NixpkgsInfo.Sha256 }}
7+
sha256 = "{{ .NixpkgsInfo.Sha256 }}";
8+
{{- end }}
59
}) {
610
{{- if .NixOverlays }}
711
overlays = [

internal/planner/plansdk/plansdk.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package plansdk
55

66
import (
77
"fmt"
8+
"net/http"
89
"os"
10+
"time"
911

1012
"github.com/imdario/mergo"
1113
"go.jetpack.io/devbox/internal/cuecfg"
@@ -91,11 +93,34 @@ type NixpkgsInfo struct {
9193
Sha256 string
9294
}
9395

94-
// The commit hash for nixos-22.11 on 2022-12-06 from status.nixos.org
95-
const DefaultNixpkgsCommit = "52e3e80afff4b16ccb7c52e9f0f5220552f03d04"
96+
// The commit hash for nixpkgs-unstable on 2023-01-25 from status.nixos.org
97+
const DefaultNixpkgsCommit = "3954218cf613eba8e0dcefa9abe337d26bc48fd0"
9698

9799
func GetNixpkgsInfo(commitHash string) (*NixpkgsInfo, error) {
100+
mirror := nixpkgsMirrorURL(commitHash)
101+
if mirror != "" {
102+
return &NixpkgsInfo{
103+
URL: mirror,
104+
}, nil
105+
}
98106
return &NixpkgsInfo{
99107
URL: fmt.Sprintf("https://github.com/nixos/nixpkgs/archive/%s.tar.gz", commitHash),
100108
}, nil
101109
}
110+
111+
func nixpkgsMirrorURL(commitHash string) string {
112+
// Use DEVBOX_REGION as a hint to see if we're in Devbox Cloud.
113+
if os.Getenv("DEVBOX_REGION") == "" {
114+
return ""
115+
}
116+
117+
// Check that the mirror is responsive and has the tar file. We can't
118+
// leave this up to Nix because fetchTarball will retry indefinitely.
119+
client := &http.Client{Timeout: 3 * time.Second}
120+
mirrorURL := fmt.Sprintf("http://[fdaa:0:a780:0:1::2]:8081/nixos/nixpkgs/archive/%s.tar.gz", commitHash)
121+
resp, err := client.Head(mirrorURL)
122+
if err != nil || resp.StatusCode != http.StatusOK {
123+
return ""
124+
}
125+
return mirrorURL
126+
}

0 commit comments

Comments
 (0)