Skip to content

Commit 58ed80e

Browse files
authored
[nix] Add DetSys installer behind feature flag (#2303)
## Summary * Adds detsys installer. * Puts it behind disabled flag. Add CICD matrix to test. * Special casing: Handles missing systemd by just skipping it. ## How was it tested? * Tested `devbox setup nix` in linux docker container. * Will test in CICD * Much more testing needed before removing feature flag
1 parent 68edd30 commit 58ed80e

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

.github/workflows/cli-tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ jobs:
183183
strategy:
184184
matrix:
185185
os: [ubuntu-latest, macos-13]
186+
use-detsys: [true, false]
186187
runs-on: ${{ matrix.os }}
187188
steps:
188189
- uses: actions/checkout@v4
@@ -197,6 +198,7 @@ jobs:
197198
- name: Install nix and devbox packages
198199
run: |
199200
export NIX_INSTALLER_NO_CHANNEL_ADD=1
201+
export DEVBOX_FEATURE_DETSYS_INSTALLER=${{ matrix.use-detsys }}
200202
201203
# Setup github authentication to ensure Github's rate limits are not hit.
202204
# If this works, we can consider refactoring this into a reusable github action helper.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package featureflag
2+
3+
var UseDetSysInstaller = disable("DETSYS_INSTALLER")

internal/boxcli/setup.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@ func setupCmd() *cobra.Command {
2929
},
3030
}
3131

32-
installNixCommand.Flags().Bool(nixDaemonFlag, false, "Install Nix in multi-user mode.")
32+
installNixCommand.Flags().Bool(
33+
nixDaemonFlag,
34+
false,
35+
"Install Nix in multi-user mode. This flag is not supported if you are using DetSys installer",
36+
)
3337
setupCommand.AddCommand(installNixCommand)
3438
return setupCommand
3539
}
3640

3741
func runInstallNixCmd(cmd *cobra.Command) error {
3842
if nix.BinaryInstalled() {
43+
// TODO: If existing installation is not detsys, but new installation is detsys can we detect
44+
// that and replace it?
3945
ux.Finfof(
4046
cmd.ErrOrStderr(),
4147
"Nix is already installed. If this is incorrect "+
4248
"please remove the nix-shell binary from your path.\n",
4349
)
4450
return nil
4551
}
46-
return nix.Install(cmd.ErrOrStderr(), nixDaemonFlagVal(cmd)())
52+
return nix.Install(cmd.ErrOrStderr(), nixDaemonFlagVal(cmd))
4753
}
4854

4955
// ensureNixInstalled verifies that nix is installed and that it is of a supported version

internal/nix/install.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/mattn/go-isatty"
1717
"github.com/pkg/errors"
1818

19+
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1920
"go.jetpack.io/devbox/internal/boxcli/usererr"
2021
"go.jetpack.io/devbox/internal/build"
2122
"go.jetpack.io/devbox/internal/cmdutil"
@@ -28,7 +29,7 @@ const rootError = "warning: installing Nix as root is not supported by this scri
2829

2930
// Install runs the install script for Nix. daemon has 3 states
3031
// nil is unset. false is --no-daemon. true is --daemon.
31-
func Install(writer io.Writer, daemon *bool) error {
32+
func Install(writer io.Writer, daemonFn func() *bool) error {
3233
if isRoot() && build.OS() == build.OSWSL {
3334
return usererr.New("Nix cannot be installed as root on WSL. Please run as a normal user with sudo access.")
3435
}
@@ -39,7 +40,18 @@ func Install(writer io.Writer, daemon *bool) error {
3940
defer r.Close()
4041

4142
installScript := "curl -L https://releases.nixos.org/nix/nix-2.24.7/install | sh -s"
42-
if daemon != nil {
43+
if featureflag.UseDetSysInstaller.Enabled() {
44+
// Should we pin version? Or just trust detsys
45+
installScript = "curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install"
46+
if isLinuxWithoutSystemd() {
47+
ux.Fwarningf(
48+
writer,
49+
"Could not detect systemd on your system. Installing Nix in root only mode (--init none).\n",
50+
)
51+
installScript += " linux --init none"
52+
}
53+
installScript += " --no-confirm"
54+
} else if daemon := daemonFn(); daemon != nil {
4355
if *daemon {
4456
installScript += " -- --daemon"
4557
} else {
@@ -157,7 +169,7 @@ func EnsureNixInstalled(writer io.Writer, withDaemonFunc func() *bool) (err erro
157169
fmt.Scanln() //nolint:errcheck
158170
}
159171

160-
if err = Install(writer, withDaemonFunc()); err != nil {
172+
if err = Install(writer, withDaemonFunc); err != nil {
161173
return err
162174
}
163175

@@ -169,3 +181,14 @@ func EnsureNixInstalled(writer io.Writer, withDaemonFunc func() *bool) (err erro
169181
fmt.Fprintln(writer, "Nix installed successfully. Devbox is ready to use!")
170182
return nil
171183
}
184+
185+
func isLinuxWithoutSystemd() bool {
186+
if build.OS() != build.OSLinux {
187+
return false
188+
}
189+
// My best interpretation of https://github.com/DeterminateSystems/nix-installer/blob/66ad2759a3ecb6da345373e3c413c25303305e25/src/action/common/configure_init_service.rs#L108-L118
190+
if _, err := os.Stat("/run/systemd/system"); errors.Is(err, os.ErrNotExist) {
191+
return true
192+
}
193+
return !cmdutil.Exists("systemctl")
194+
}

0 commit comments

Comments
 (0)