Skip to content

Added --root-user flag to devbox generate #1359

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

Merged
merged 5 commits into from
Aug 11, 2023
Merged

Conversation

mohsenari
Copy link
Collaborator

Summary

follow up for #1325
This also potentially fixes an issue reported for users having permission issues with .devbox/ directory inside devcontainer because of root user inside the container.

Added a --root-user flag to allow both single user installation and multi user installation for nix as well as setup for Dockerfile and devcontainer.json.

This allows users to choose either devbox as a user in the container environment (default) or root as a user in the container environment (passing --root-user flag).

How was it tested?

devbox generate dockerfile -f -r
compare content of Dockerfile with
devbox generate dockerfile -f

@mohsenari mohsenari requested review from savil and mikeland73 August 9, 2023 21:48
Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with dockerfile changes. Some comments on golang changes.

devbox.go Outdated
Comment on lines 28 to 29
GenerateDevcontainer(ctx context.Context, force bool, rootUser bool) error
GenerateDockerfile(ctx context.Context, force bool, rootUser bool) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool params are bad. Consider using struct. Could even be part of devopt.Opts

devopt.Opts{
  GenerateOpts: devopt.GenerateOpts{
    Force: force,
    RootUser: rootUser,
  }
}

@@ -63,6 +64,8 @@ func devcontainerCmd() *cobra.Command {
}
command.Flags().BoolVarP(
&flags.force, "force", "f", false, "force overwrite on existing files")
command.Flags().BoolVarP(
&flags.rootUser, "root-user", "r", false, "Use root as default user inside the container")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should use a short flag for this (so no -r). It's not a super common flag and we may want to use -r for something else in the future. (when in doubt, it's a bit better not to add short flag)

@@ -80,6 +83,8 @@ func dockerfileCmd() *cobra.Command {
}
command.Flags().BoolVarP(
&flags.force, "force", "f", false, "force overwrite existing files")
command.Flags().BoolVarP(
&flags.rootUser, "root-user", "r", false, "Use root as default user inside the container")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, no short flag.

@@ -394,15 +394,21 @@ func (d *Devbox) GenerateDevcontainer(ctx context.Context, force bool) error {
return redact.Errorf("error creating dev container directory in <project>/%s: %w",
redact.Safe(filepath.Base(devContainerPath)), err)
}
isDevcontainer := true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally prefer comments to local variable that is only used once, but even better would be to use struct to simulate named params.

Comment on lines 26 to 34
type Generate struct {
Ctx context.Context
Path string
RootUser bool
IsDevcontainer bool
Pkgs []string
LocalFlakeDirs []string
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few issues:

  • Not sure about the name (generate.Generate is repetitive and doesn't convey what it does).
  • As currently used it does not need to be exported.
  • It should not contain context. (In general context should never be saved in a struct).

Proposal:

  • Name it Options instead.
  • Remove context.
  • Remove Open

Code would look like:

g := &generate.Options{...}
g.CreateDockerfile(ctx)

@@ -128,7 +158,10 @@ func getDevcontainerContent(pkgs []string) *devcontainerObject {
},
},
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still relevant?

@mohsenari mohsenari requested a review from mikeland73 August 10, 2023 03:20
mohsenari added a commit that referenced this pull request Aug 11, 2023
## Summary
Follow up for #1359
Updates docs for newly added flag for `devbox generate dockerfile` and
`devbox generate devcontainer`

## How was it tested?
N/A

---------

Co-authored-by: Lucille Hua <[email protected]>
@mohsenari mohsenari force-pushed the mohsen--single-user-flag branch from 424dd8b to 3266546 Compare August 11, 2023 00:38
@mohsenari
Copy link
Collaborator Author

@mikeland73 I updated the PR based on the feedback. PTAL

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I'm less confident about the dockerfile stuff so make sure that's well tested.

@@ -139,13 +144,17 @@ func runGenerateCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
if err != nil {
return errors.WithStack(err)
}
generateOpts := devopt.GenerateOpts{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hack, if you embed devopt.GenerateOpts to the flag struct you can pass flags.GenerateOpts into the functions below.

@mohsenari mohsenari merged commit d7a53c0 into main Aug 11, 2023
@mohsenari mohsenari deleted the mohsen--single-user-flag branch August 11, 2023 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants