Skip to content

Commit b4a2add

Browse files
authored
nixcache: improve sudo prompt and help docs (#2107)
1 parent 7f349b7 commit b4a2add

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

internal/boxcli/cache.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,23 @@ func cacheCmd() *cobra.Command {
8080
func cacheConfigureCmd() *cobra.Command {
8181
username := ""
8282
cmd := &cobra.Command{
83-
Use: "configure",
84-
Short: "Configure Nix to use the Devbox cache as a substituter",
83+
Use: "configure",
84+
Short: "Configure Nix to use the Devbox cache as a substituter",
85+
Long: heredoc.Doc(`
86+
Configure Nix to use the Devbox cache as a substituter.
87+
88+
If the current Nix installation is multi-user, this command grants the Nix
89+
daemon access to Devbox caches by making the following changes:
90+
91+
- Adds the current user to Nix's list of trusted users in the system nix.conf.
92+
- Adds the cache credentials to ~root/.aws/config.
93+
94+
Configuration requires sudo, but only needs to happen once. The changes persist
95+
across Devbox accounts and organizations.
96+
97+
This command is a no-op for single-user Nix installs that aren't running the
98+
Nix daemon.
99+
`),
85100
Hidden: true,
86101
Args: cobra.MaximumNArgs(0),
87102
RunE: func(cmd *cobra.Command, args []string) error {

internal/devbox/providers/nixcache/setup.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,28 @@ func Configure(ctx context.Context) error {
3838
if err != nil {
3939
return redact.Errorf("nixcache: lookup current user: %v", err)
4040
}
41-
return configure(ctx, u.Username, false)
42-
}
4341

44-
func ConfigureReprompt(ctx context.Context, username string) error {
45-
return configure(ctx, username, true)
46-
}
42+
task := &setupTask{u.Username}
4743

48-
func configure(ctx context.Context, username string, reprompt bool) error {
49-
if reprompt {
50-
setup.Reset(setupKey)
44+
// This function might be called from other Devbox commands
45+
// (such as devbox add), so we need to provide some context in the sudo
46+
// prompt.
47+
const sudoPrompt = "You're logged into a Devbox account, but Nix isn't setup to use your account's caches. " +
48+
"Allow sudo to configure Nix?"
49+
err = setup.ConfirmRun(ctx, setupKey, task, sudoPrompt)
50+
if err != nil {
51+
return redact.Errorf("nixcache: run setup: %w", err)
5152
}
53+
return nil
54+
}
5255

56+
func ConfigureReprompt(ctx context.Context, username string) error {
57+
setup.Reset(setupKey)
5358
task := &setupTask{username}
54-
const sudoPrompt = "You're logged into a Devbox account that now has access to a Nix cache. " +
55-
"Allow Devbox to configure Nix to use the new cache (requires sudo)?"
56-
err := setup.ConfirmRun(ctx, setupKey, task, sudoPrompt)
59+
60+
// We're reprompting, so the user explicitly asked to configure the
61+
// cache. We can keep the sudo prompt short.
62+
err := setup.ConfirmRun(ctx, setupKey, task, "Allow sudo to configure Nix?")
5763
if err != nil {
5864
return redact.Errorf("nixcache: run setup: %w", err)
5965
}

0 commit comments

Comments
 (0)