-
Notifications
You must be signed in to change notification settings - Fork 249
Derive nix profile from the generated flake #1692
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
08cdfb0
[StorePathParts] move to nix package
savil fee3221
nix profile from flake
savil 54e0493
fixes: experimental flags, UX improvements
savil d782e61
install profile in offline mode
savil a859c19
ensure packages are installable, and cleanup, enable php testscript
savil d312585
remove ProfileInstallPackage
savil be2f7bd
debugging
savil f996121
always recompute Devbox environment, and reuse buildInputs from print…
savil 4fb3d14
improve validatePackages to only operate on packages to be installed
savil 1f418d5
install packages via nix build
savil 8521fa4
code review feedback fixes
savil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package devbox | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
"go.jetpack.io/devbox/internal/nix" | ||
"go.jetpack.io/devbox/internal/nix/nixprofile" | ||
"go.jetpack.io/devbox/internal/ux" | ||
) | ||
|
||
// syncNixProfile ensures the nix profile has the packages specified in wantStorePaths. | ||
// It also removes any packages from the nix profile that are not in wantStorePaths. | ||
func (d *Devbox) syncNixProfile(ctx context.Context, wantStorePaths []string) error { | ||
profilePath, err := d.profilePath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the store-paths of the packages currently installed in the nix profile | ||
items, err := nixprofile.ProfileListItems(ctx, d.stderr, profilePath) | ||
if err != nil { | ||
return fmt.Errorf("nix profile list: %v", err) | ||
} | ||
gotStorePaths := make([]string, 0, len(items)) | ||
for _, item := range items { | ||
gotStorePaths = append(gotStorePaths, item.StorePaths()...) | ||
} | ||
|
||
// Diff the store paths and install/remove packages as needed | ||
remove, add := lo.Difference(gotStorePaths, wantStorePaths) | ||
if len(remove) > 0 { | ||
packagesToRemove := make([]string, 0, len(remove)) | ||
for _, p := range remove { | ||
storePath := nix.NewStorePathParts(p) | ||
packagesToRemove = append(packagesToRemove, fmt.Sprintf("%s@%s", storePath.Name, storePath.Version)) | ||
} | ||
if len(packagesToRemove) == 1 { | ||
ux.Finfo(d.stderr, "Removing %s\n", strings.Join(packagesToRemove, ", ")) | ||
} else { | ||
ux.Finfo(d.stderr, "Removing packages: %s\n", strings.Join(packagesToRemove, ", ")) | ||
} | ||
|
||
if err := nix.ProfileRemove(profilePath, remove...); err != nil { | ||
return err | ||
} | ||
} | ||
if len(add) > 0 { | ||
total := len(add) | ||
for idx, addPath := range add { | ||
stepNum := idx + 1 | ||
storePath := nix.NewStorePathParts(addPath) | ||
nameAndVersion := fmt.Sprintf("%s@%s", storePath.Name, storePath.Version) | ||
stepMsg := fmt.Sprintf("[%d/%d] %s", stepNum, total, nameAndVersion) | ||
|
||
if err = nixprofile.ProfileInstall(ctx, &nixprofile.ProfileInstallArgs{ | ||
CustomStepMessage: stepMsg, | ||
Installable: addPath, | ||
// Install in offline mode for speed. We know we should have all the files | ||
// locally in /nix/store since we have run `nix print-dev-env` prior to this. | ||
// Also avoids some "substituter not found for store-path" errors. | ||
Offline: true, | ||
PackageName: storePath.Name, | ||
ProfilePath: profilePath, | ||
Writer: d.stderr, | ||
}); err != nil { | ||
return fmt.Errorf("error installing package %s: %w", addPath, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can all of the packages be added to the profile with a single
nix profile install
command? I think in my POC I was assuming that all of the packages were already installed successfully if Nix was able to build the generated Devbox flake.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC that leads to problems because multiple Devbox packages can install the same binary.
Nix will error with the complaint that it cannot install "" from two different paths at the same priority.