Skip to content

[package outputs] add --outputs flag to devbox add, and save in config #1707

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 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Editors
.idea
.vscode
.zed

# NodeJS
node_modules
Expand Down
5 changes: 5 additions & 0 deletions internal/boxcli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type addCmdFlags struct {
platforms []string
excludePlatforms []string
patchGlibc bool
outputs []string
}

func addCmd() *cobra.Command {
Expand Down Expand Up @@ -67,6 +68,9 @@ func addCmd() *cobra.Command {
command.Flags().BoolVar(
&flags.patchGlibc, "patch-glibc", false,
"patch any ELF binaries to use the latest glibc version in nixpkgs")
command.Flags().StringSliceVarP(
&flags.outputs, "outputs", "o", []string{},
"specify the outputs to select for the nix package")

return command
}
Expand All @@ -87,5 +91,6 @@ func addCmdFunc(cmd *cobra.Command, args []string, flags addCmdFlags) error {
Platforms: flags.platforms,
ExcludePlatforms: flags.excludePlatforms,
PatchGlibc: flags.patchGlibc,
Outputs: flags.outputs,
})
}
1 change: 1 addition & 0 deletions internal/devbox/devopt/devboxopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type AddOpts struct {
ExcludePlatforms []string
DisablePlugin bool
PatchGlibc bool
Outputs []string
}

type UpdateOpts struct {
Expand Down
7 changes: 5 additions & 2 deletions internal/devbox/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,14 @@ func (d *Devbox) setPackageOptions(pkgs []string, opts devopt.AddOpts) error {
pkg, opts.DisablePlugin); err != nil {
return err
}

if err := d.cfg.Packages.SetPatchGLibc(
pkg, opts.PatchGlibc); err != nil {
return err
}
if err := d.cfg.Packages.SetOutputs(
d.stderr, pkg, opts.Outputs); err != nil {
return err
}
}

// Resolving here ensures we allow insecure before running ensureStateIsUpToDate
Expand Down Expand Up @@ -175,7 +178,7 @@ func (d *Devbox) printPostAddMessage(
}
}

if len(opts.Platforms) == 0 && len(opts.ExcludePlatforms) == 0 && !opts.AllowInsecure {
if len(opts.Platforms) == 0 && len(opts.ExcludePlatforms) == 0 && len(opts.Outputs) == 0 && !opts.AllowInsecure {
if len(unchangedPackageNames) == 1 {
ux.Finfo(d.stderr, "Package %q was already in devbox.json and was not modified\n", unchangedPackageNames[0])
} else if len(unchangedPackageNames) > 1 {
Expand Down
58 changes: 35 additions & 23 deletions internal/devconfig/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,15 @@ func (c *configAST) appendPlatforms(name, fieldName string, platforms []string)
return
}

pkgObject := c.FindPkgObject(name)
if pkgObject == nil {
return
}
c.appendStringSliceField(name, fieldName, platforms)
}

var arr *hujson.Array
if i := c.memberIndex(pkgObject, fieldName); i == -1 {
arr = &hujson.Array{
Elements: make([]hujson.Value, 0, len(platforms)),
}
pkgObject.Members = append(pkgObject.Members, hujson.ObjectMember{
Name: hujson.Value{
Value: hujson.String(fieldName),
BeforeExtra: []byte{'\n'},
},
Value: hujson.Value{Value: arr},
})
} else {
arr = pkgObject.Members[i].Value.Value.(*hujson.Array)
arr.Elements = slices.Grow(arr.Elements, len(platforms))
func (c *configAST) appendOutputs(name, fieldName string, outputs []string) {
if len(outputs) == 0 {
return
}

for _, p := range platforms {
arr.Elements = append(arr.Elements, hujson.Value{Value: hujson.String(p)})
}
c.root.Format()
c.appendStringSliceField(name, fieldName, outputs)
}

func (c *configAST) FindPkgObject(name string) *hujson.Object {
Expand Down Expand Up @@ -307,3 +290,32 @@ func joinNameVersion(name, version string) string {
}
return name + "@" + version
}

func (c *configAST) appendStringSliceField(name, fieldName string, fieldValues []string) {
pkgObject := c.FindPkgObject(name)
if pkgObject == nil {
return
}

var arr *hujson.Array
if i := c.memberIndex(pkgObject, fieldName); i == -1 {
arr = &hujson.Array{
Elements: make([]hujson.Value, 0, len(fieldValues)),
}
pkgObject.Members = append(pkgObject.Members, hujson.ObjectMember{
Name: hujson.Value{
Value: hujson.String(fieldName),
BeforeExtra: []byte{'\n'},
},
Value: hujson.Value{Value: arr},
})
} else {
arr = pkgObject.Members[i].Value.Value.(*hujson.Array)
arr.Elements = slices.Grow(arr.Elements, len(fieldValues))
}

for _, p := range fieldValues {
arr.Elements = append(arr.Elements, hujson.Value{Value: hujson.String(p)})
}
c.root.Format()
}
62 changes: 62 additions & 0 deletions internal/devconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,68 @@ func TestExcludePlatforms(t *testing.T) {
}
}

func TestSetOutputs(t *testing.T) {
in, want := parseConfigTxtarTest(t, `
-- in --
{
"packages": {
"prometheus": {
"version": "latest"
}
}
}
-- want --
{
"packages": {
"prometheus": {
"version": "latest",
"outputs": ["cli"]
}
}
}`)

err := in.Packages.SetOutputs(io.Discard, "prometheus@latest", []string{"cli"})
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
}
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
}
}

func TestSetOutputsMigrateArray(t *testing.T) {
in, want := parseConfigTxtarTest(t, `
-- in --
{
"packages": ["go", "[email protected]", "prometheus@latest"]
}
-- want --
{
"packages": {
"go": "",
"python": "3.10",
"prometheus": {
"version": "latest",
"outputs": ["cli"]
}
}
}`)

err := in.Packages.SetOutputs(io.Discard, "prometheus@latest", []string{"cli"})
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
}
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
}
}

func TestDefault(t *testing.T) {
path := filepath.Join(t.TempDir())
in := DefaultConfig()
Expand Down
31 changes: 31 additions & 0 deletions internal/devconfig/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ func (pkgs *Packages) SetDisablePlugin(versionedName string, v bool) error {
return nil
}

func (pkgs *Packages) SetOutputs(writer io.Writer, versionedName string, outputs []string) error {
name, version := parseVersionedName(versionedName)
i := pkgs.index(name, version)
if i == -1 {
return errors.Errorf("package %s not found", versionedName)
}

toAdd := []string{}
for _, o := range outputs {
if !slices.Contains(pkgs.Collection[i].Outputs, o) {
toAdd = append(toAdd, o)
}
}

if len(toAdd) > 0 {
pkg := &pkgs.Collection[i]
pkgs.ast.appendOutputs(pkg.name, "outputs", toAdd)
ux.Finfo(writer, "Added outputs %s to package %s\n", strings.Join(toAdd, ", "), versionedName)
}
return nil
}

func (pkgs *Packages) index(name, version string) int {
return slices.IndexFunc(pkgs.Collection, func(p Package) bool {
return p.name == name && p.Version == version
Expand All @@ -220,6 +242,10 @@ type Package struct {
// PatchGlibc applies a function to the package's derivation that
// patches any ELF binaries to use the latest version of nixpkgs#glibc.
PatchGlibc bool `json:"patch_glibc,omitempty"`

// Outputs is the list of outputs to use for this package, assuming
// it is a nix package. If empty, the default output is used.
Outputs []string `json:"outputs,omitempty"`
}

func NewVersionOnlyPackage(name, version string) Package {
Expand All @@ -246,12 +272,17 @@ func NewPackage(name string, values map[string]any) Package {
if e, ok := values["excluded_platforms"]; ok {
excludedPlatforms = e.([]string)
}
var outputs []string
if o, ok := values["outputs"]; ok {
outputs = o.([]string)
}

return Package{
name: name,
Version: version.(string),
Platforms: platforms,
ExcludedPlatforms: excludedPlatforms,
Outputs: outputs,
}
}

Expand Down
19 changes: 19 additions & 0 deletions internal/devconfig/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ func TestJsonifyConfigPackages(t *testing.T) {
},
},
},
{
name: "map-with-platforms-and-excluded-platforms-and-outputs-nixpkgs-reference",
jsonConfig: `{"packages":{"github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello":` +
`{"version":"latest",` +
`"platforms":["x86_64-darwin","aarch64-linux"],` +
`"excluded_platforms":["x86_64-linux"],` +
`"outputs":["cli"]` +
`}}}`,
expected: Packages{
Collection: []Package{
NewPackage("github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello", map[string]any{
"version": "latest",
"platforms": []string{"x86_64-darwin", "aarch64-linux"},
"excluded_platforms": []string{"x86_64-linux"},
"outputs": []string{"cli"},
}),
},
},
},
}

for _, testCase := range testCases {
Expand Down