Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixcache: improve sudo prompt and help docs #2107

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
nixcache: improve sudo prompt and help docs
  • Loading branch information
gcurtis committed May 31, 2024
commit 9cb8f9c10d95cf2fb2f71f76cf5240cde133aa82
17 changes: 15 additions & 2 deletions internal/boxcli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,21 @@ func cacheCmd() *cobra.Command {
func cacheConfigureCmd() *cobra.Command {
username := ""
cmd := &cobra.Command{
Use: "configure",
Short: "Configure Nix to use the Devbox cache as a substituter",
Use: "configure",
Short: "Configure Nix to use the Devbox cache as a substituter",
Long: "Configure Nix to use the Devbox cache as a substituter.\n" +
"\n" +
"If the current Nix installation is multi-user, this command grants the Nix\n" +
"daemon access to Devbox caches by making the following changes:\n" +
"\n" +
"- Adds the current user to Nix's list of trusted users in the system nix.conf.\n" +
"- Adds the cache credentials to ~root/.aws/config.\n" +
"\n" +
"Configuration requires sudo, but only needs to happen once. The changes persist\n" +
"across Devbox accounts and organizations.\n" +
"\n" +
"This command is a no-op for single-user Nix installs that aren't running the\n" +
"Nix daemon.",
gcurtis marked this conversation as resolved.
Show resolved Hide resolved
Hidden: true,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
28 changes: 17 additions & 11 deletions internal/devbox/providers/nixcache/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,28 @@ func Configure(ctx context.Context) error {
if err != nil {
return redact.Errorf("nixcache: lookup current user: %v", err)
}
return configure(ctx, u.Username, false)
}

func ConfigureReprompt(ctx context.Context, username string) error {
return configure(ctx, username, true)
}
task := &setupTask{u.Username}

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

func ConfigureReprompt(ctx context.Context, username string) error {
setup.Reset(setupKey)
task := &setupTask{username}
const sudoPrompt = "You're logged into a Devbox account that now has access to a Nix cache. " +
"Allow Devbox to configure Nix to use the new cache (requires sudo)?"
err := setup.ConfirmRun(ctx, setupKey, task, sudoPrompt)

// We're reprompting, so the user explicitly asked to configure the
// cache. We can keep the sudo prompt short.
err := setup.ConfirmRun(ctx, setupKey, task, "Allow sudo to configure Nix?")
if err != nil {
return redact.Errorf("nixcache: run setup: %w", err)
}
Expand Down