Skip to content

Commit 3e1c02c

Browse files
authored
[Rust Shell] change to use rustup nix package to avoid overlay (#235)
1 parent 0faa80b commit 3e1c02c

File tree

15 files changed

+125
-91
lines changed

15 files changed

+125
-91
lines changed

planner/languages/rust/rust_planner.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import (
1313
"go.jetpack.io/devbox/planner/plansdk"
1414
)
1515

16-
// Source and reference: https://github.com/oxalica/rust-overlay
17-
const RustOxalicaOverlay = "https://github.com/oxalica/rust-overlay/archive/stable.tar.gz"
18-
1916
// `cargo new` generates a file with uppercase Cargo.toml
2017
const cargoToml = "Cargo.toml"
2118

@@ -32,23 +29,10 @@ func (p *Planner) IsRelevant(srcDir string) bool {
3229
return p.cargoTomlPath(srcDir) != ""
3330
}
3431

35-
func (p *Planner) GetShellPlan(srcDir string) *plansdk.ShellPlan {
36-
plan := &plansdk.ShellPlan{
37-
NixOverlays: []string{RustOxalicaOverlay},
38-
}
39-
manifest, err := p.cargoManifest(srcDir)
40-
if err != nil {
41-
return plan
32+
func (p *Planner) GetShellPlan(_srcDir string) *plansdk.ShellPlan {
33+
return &plansdk.ShellPlan{
34+
DevPackages: []string{"rustup"},
4235
}
43-
rustVersion, err := p.rustOxalicaVersion(manifest)
44-
if err != nil {
45-
return plan
46-
}
47-
48-
rustPkgDev := fmt.Sprintf("rust-bin.stable.%s.default", rustVersion)
49-
plan.DevPackages = []string{rustPkgDev, "gcc"}
50-
51-
return plan
5236
}
5337

5438
func (p *Planner) GetBuildPlan(srcDir string) *plansdk.BuildPlan {
@@ -68,43 +52,46 @@ func (p *Planner) getBuildPlan(srcDir string) (*plansdk.BuildPlan, error) {
6852
if err != nil {
6953
return nil, err
7054
}
71-
rustVersion, err := p.rustOxalicaVersion(manifest)
55+
rustupVersion, err := p.rustupVersion(manifest)
7256
if err != nil {
7357
return nil, errors.WithStack(err)
7458
}
7559

76-
rustPkgDev := fmt.Sprintf("rust-bin.stable.%s.default", rustVersion)
60+
envSetup := p.envsetupCommands(rustupVersion)
7761

7862
return &plansdk.BuildPlan{
79-
NixOverlays: []string{RustOxalicaOverlay},
8063
// 'gcc' added as a linker for libc (C toolchain)
8164
// 1. https://softwareengineering.stackexchange.com/a/332254
8265
// 2. https://stackoverflow.com/a/56166959
83-
DevPackages: []string{rustPkgDev, "gcc"},
84-
RuntimePackages: []string{"glibc"},
66+
DevPackages: []string{"rustup", "gcc"},
67+
RuntimePackages: []string{"rustup", "gcc"},
68+
8569
InstallStage: &plansdk.Stage{
8670
InputFiles: []string{"."},
87-
Command: "cargo fetch",
71+
Command: fmt.Sprintf("%s && cargo fetch", envSetup),
8872
},
8973
BuildStage: &plansdk.Stage{
9074
InputFiles: []string{"."},
91-
Command: "cargo build --release --offline",
75+
Command: fmt.Sprintf("%s && cargo build --release --offline", envSetup),
9276
},
9377
StartStage: &plansdk.Stage{
94-
InputFiles: []string{fmt.Sprintf("target/release/%s", manifest.PackageField.Name)},
95-
Command: fmt.Sprintf("./%s", manifest.PackageField.Name),
78+
InputFiles: []string{"."},
79+
Command: fmt.Sprintf("%s && cargo run --release --offline", envSetup),
9680
},
9781
}, nil
9882
}
9983

100-
// Follows the Oxalica convention where it needs to be either:
101-
// 1. latest
84+
// Follows the Rustup convention where it needs to be either:
85+
// 1. stable
10286
// 2. "<version>", including the quotation marks. Example: "1.62.0"
10387
//
104-
// This result is spliced into (for example) "rust-bin.stable.<result>.default"
105-
func (p *Planner) rustOxalicaVersion(manifest *cargoManifest) (string, error) {
88+
// TODO: add support for beta, nightly, and [stable|beta|nightly]-<archive-date>
89+
// <channel> = stable|beta|nightly|<major.minor>|<major.minor.patch>
90+
// Channel names can be optionally appended with an archive date, as in nightly-2014-12-18
91+
// https://rust-lang.github.io/rustup/concepts/toolchains.html
92+
func (p *Planner) rustupVersion(manifest *cargoManifest) (string, error) {
10693
if manifest.PackageField.RustVersion == "" {
107-
return "latest", nil
94+
return "stable", nil
10895
}
10996

11097
rustVersion, err := plansdk.NewVersion(manifest.PackageField.RustVersion)
@@ -148,3 +135,26 @@ func (p *Planner) cargoTomlPath(srcDir string) string {
148135
}
149136
return ""
150137
}
138+
139+
// envsetupCommands are bash commands that ensure the rustup toolchain is setup so
140+
// it always works. We tradeoff robustness for performance in this implementation,
141+
// which is a polite way of saying that it is slow.
142+
func (p *Planner) envsetupCommands(rustupVersion string) string {
143+
144+
// RUSTUP_HOME sets the root rustup folder, which is used for storing installed toolchains
145+
// and configuration options. CARGO_HOME contains cache files used by cargo.
146+
//
147+
// Note that you will need to ensure these environment variables are always set and
148+
// that CARGO_HOME/bin is in the $PATH environment variable when using the toolchain.
149+
// source: https://rust-lang.github.io/rustup/installation/index.html
150+
cargoHome := "./.devbox/rust/cargo"
151+
cargoSetup := fmt.Sprintf("mkdir -p %s && export CARGO_HOME=%s && export PATH=$PATH:$CARGO_HOME", cargoHome,
152+
cargoHome)
153+
154+
rustupHome := "./.devbox/rust/rustup/"
155+
rustupSetup := fmt.Sprintf("mkdir -p %s && export RUSTUP_HOME=%s && rustup default %s", rustupHome, rustupHome,
156+
rustupVersion)
157+
envSetup := fmt.Sprintf("%s && %s", cargoSetup, rustupSetup)
158+
159+
return envSetup
160+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target/
2+
.rustup/
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
{
2-
"nix_overlays": [
3-
"https://github.com/oxalica/rust-overlay/archive/stable.tar.gz"
4-
],
52
"dev_packages": [
6-
"rust-bin.stable.\"1.62.0\".default",
3+
"rustup",
74
"gcc"
85
],
96
"runtime_packages": [
10-
"glibc"
7+
"rustup",
8+
"gcc"
119
],
1210
"install_stage": {
13-
"command": "cargo fetch",
11+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default \"1.62.0\" && cargo fetch",
1412
"input_files": [
1513
"."
1614
]
1715
},
1816
"build_stage": {
19-
"command": "cargo build --release --offline",
17+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default \"1.62.0\" && cargo build --release --offline",
2018
"input_files": [
2119
"."
2220
]
2321
},
2422
"start_stage": {
25-
"command": "./hello-rust",
23+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default \"1.62.0\" && cargo run --release --offline",
2624
"input_files": [
27-
"target/release/hello-rust"
25+
"."
2826
]
29-
},
30-
"definitions": null
27+
}
3128
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"packages": [
3-
"rust-bin.stable.\"1.62.0\".default",
3+
"rustup",
44
"gcc"
5-
]
5+
],
6+
"shell": {
7+
"init_hook": [
8+
"source init-shell.sh"
9+
]
10+
}
611
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# TODO this only works when devbox shell is started in this directory. Using
3+
# the --config flag to start the shell will break this.
4+
# We could inject $JETPACK_CONFIG env-var into the shell environment to replace this.
5+
projectDir=$(dirname $(readlink -f "$0"))
6+
echo "project dir is $projectDir"
7+
8+
rustupHomeDir="$projectDir"/.rustup
9+
mkdir -p $rustupHomeDir
10+
export RUSTUP_HOME=$rustupHomeDir
11+
export LIBRARY_PATH=$LIBRARY_PATH:"$projectDir/nix/profile/default/lib"
12+
13+
rustup default 1.62.0
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
{
22
"dev_packages": [
3-
"rust-bin.stable.\"1.62.0\".default",
3+
"rustup",
44
"gcc"
5-
],
6-
"nix_overlays": [
7-
"https://github.com/oxalica/rust-overlay/archive/stable.tar.gz"
85
]
96
}
10-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
target/
2+
.devbox/
3+
.rustup/
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
{
2-
"nix_overlays": [
3-
"https://github.com/oxalica/rust-overlay/archive/stable.tar.gz"
4-
],
52
"dev_packages": [
6-
"rust-bin.stable.latest.default",
3+
"rustup",
4+
"libiconv",
75
"gcc"
86
],
97
"runtime_packages": [
10-
"glibc"
8+
"rustup",
9+
"gcc"
1110
],
1211
"install_stage": {
13-
"command": "cargo fetch",
12+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default stable && cargo fetch",
1413
"input_files": [
1514
"."
1615
]
1716
},
1817
"build_stage": {
19-
"command": "cargo build --release --offline",
18+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default stable && cargo build --release --offline",
2019
"input_files": [
2120
"."
2221
]
2322
},
2423
"start_stage": {
25-
"command": "./rust-stable-hello-world",
24+
"command": "mkdir -p ./.devbox/rust/cargo && export CARGO_HOME=./.devbox/rust/cargo && export PATH=$PATH:$CARGO_HOME && mkdir -p ./.devbox/rust/rustup/ && export RUSTUP_HOME=./.devbox/rust/rustup/ && rustup default stable && cargo run --release --offline",
2625
"input_files": [
27-
"target/release/rust-stable-hello-world"
26+
"."
2827
]
29-
},
30-
"definitions": null
28+
}
3129
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"packages": [
3-
"rust-bin.stable.latest.default",
4-
"gcc"
3+
"rustup",
4+
"libiconv"
55
],
6-
"shell": {}
7-
}
6+
"shell": {
7+
"init_hook": [
8+
"source init-shell.sh"
9+
]
10+
}
11+
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"dev_packages": [
3-
"rust-bin.stable.latest.default",
4-
"gcc"
5-
],
6-
"nix_overlays": [
7-
"https://github.com/oxalica/rust-overlay/archive/stable.tar.gz"
3+
"rustup",
4+
"libiconv"
85
]
96
}
107

0 commit comments

Comments
 (0)