Skip to content

Commit 972e50b

Browse files
committed
Add check before executing nix commands
1 parent a2d8363 commit 972e50b

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

toolprovider/mise/install.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,29 @@ func installRequest(toolRequest provider.ToolRequest, useNix bool) provider.Tool
2626
}
2727

2828
func canBeInstalledWithNix(tool provider.ToolRequest, execEnv execenv.ExecEnv) bool {
29+
// Force switch for integration testing. No fallback to regular install when this is active. This makes failures explicit.
30+
forceNix := os.Getenv("BITRISE_TOOLSETUP_FAST_INSTALL_FORCE") == "1"
31+
if forceNix {
32+
return true
33+
}
34+
2935
if !nixpkgs.ShouldUseBackend(tool) {
3036
return false
3137
}
3238

33-
// Force switch for integration testing. No fallback to regular install when this is active. This makes failures explicit.
34-
forceNix := os.Getenv("BITRISE_TOOLSETUP_FAST_INSTALL_FORCE") == "1"
35-
3639
_, err := execEnv.RunMisePlugin("install", nixpkgs.PluginName, nixpkgs.PluginGitURL)
3740
if err != nil {
38-
if forceNix {
39-
return true
40-
}
4141
log.Warnf("Error while installing nixpkgs plugin (%s). Falling back to core plugin installation.", nixpkgs.PluginGitURL, err)
4242
return false
4343
}
4444

4545
nameWithBackend := provider.ToolID(fmt.Sprintf("nixpkgs:%s", tool.ToolName))
4646
available, err := versionExists(execEnv, nameWithBackend, tool.UnparsedVersion)
4747
if err != nil {
48-
if forceNix {
49-
return true
50-
}
5148
log.Warnf("Error while checking nixpkgs index for %s@%s: %v. Falling back to core plugin installation.", tool.ToolName, tool.UnparsedVersion, err)
5249
return false
5350
}
5451
if !available {
55-
if forceNix {
56-
return true
57-
}
5852
log.Warnf("%s@%s not found in nixpkgs index, doing a source build. This may take some time...", tool.ToolName, tool.UnparsedVersion)
5953
return false
6054
}

toolprovider/mise/nixpkgs/nixpkgs.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nixpkgs
22

33
import (
44
"os"
5+
"os/exec"
56
"strings"
67

78
"github.com/bitrise-io/bitrise/v2/log"
@@ -13,19 +14,39 @@ const (
1314
PluginName = "nixpkgs"
1415
)
1516

16-
// TODO: check if Nix works at all
17-
1817
func ShouldUseBackend(request provider.ToolRequest) bool {
1918
if request.ToolName != "ruby" {
2019
log.Debugf("[TOOLPROVIDER] The mise-nixpkgs backend is only enabled for Ruby for now. Using core plugin to install %s", request.ToolName)
2120
return false
2221
}
2322

2423
value, ok := os.LookupEnv("BITRISE_TOOLSETUP_FAST_INSTALL")
25-
if ok && strings.TrimSpace(value) == "1" {
26-
log.Debugf("[TOOLPROVIDER] Using nixpkgs backend for %s as BITRISE_TOOLSETUP_FAST_INSTALL is set", request.ToolName)
27-
return true
24+
if !ok || strings.TrimSpace(value) != "1" {
25+
log.Debugf("[TOOLPROVIDER] Using core mise plugin for %s", request.ToolName)
26+
return false
27+
}
28+
29+
if !isNixAvailable() {
30+
log.Debugf("[TOOLPROVIDER] Nix is not available on the system, cannot use nixpkgs backend for %s", request.ToolName)
31+
return false
32+
}
33+
34+
log.Debugf("[TOOLPROVIDER] Using nixpkgs backend for %s as BITRISE_TOOLSETUP_FAST_INSTALL is set", request.ToolName)
35+
return true
36+
}
37+
38+
func isNixAvailable() bool {
39+
_, err := exec.LookPath("nix")
40+
if err != nil {
41+
return false
2842
}
29-
log.Debugf("[TOOLPROVIDER] Using core plugin for %s", request.ToolName)
30-
return false
43+
44+
cmd := exec.Command("nix", "--version")
45+
out, err := cmd.CombinedOutput()
46+
if err != nil {
47+
log.Debugf("[TOOLPROVIDER] Exec nix --version: %s, output:\n%s", err, out)
48+
return false
49+
}
50+
51+
return true
3152
}

0 commit comments

Comments
 (0)