Skip to content

[RFC] [refactor] Consolidate profile update code under syncPackagesToProfile #1529

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 4 commits into from
Oct 5, 2023

Conversation

savil
Copy link
Collaborator

@savil savil commented Oct 4, 2023

Summary

This PR refactors code to reduce duplication of logic around nix profile updates.

Specifically:

  • the implementation under syncPackagesToProfile is refactored so that we first prepare profileDir, and installablePackages, and then remove extra packages from profile, and add missing packages to profile. Preparing these variables upfront reduces much code duplication.
  • in devbox.Remove, we now rely on ensurePackagesAreInstalled calling syncPackagesToProfile to remove the package from the profile. We can eliminate the extra removePackagesFromProfile function.
  • in ensurePackagesAreInstalled, we now always execute it if mode is install or uninstall. We only check the lockfile status for ensure mode.

How was it tested?

Did some basic adding and removing of packages

Copy link
Collaborator Author

savil commented Oct 4, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@savil savil force-pushed the savil/refactor-profile-updates branch 2 times, most recently from 1cd1389 to 754575d Compare October 4, 2023 18:33
@savil savil marked this pull request as ready for review October 4, 2023 18:37
@savil savil requested review from mikeland73 and ipince October 4, 2023 18:37
@savil savil force-pushed the savil/refactor-profile-updates branch from 754575d to 5cd0ec8 Compare October 4, 2023 18:40
@savil
Copy link
Collaborator Author

savil commented Oct 5, 2023

My proposal for the next PR is:
Drop ensurePackagesAreInstalled from add, rm, update. Print a message to users to run devbox install to ensure their devbox environment is up-to-date.

In the future, we can also augment add, rm, update to only update the devbox environment with changes to the specified packages. In practice, we have to evaluate how feasible this is, because certain operations are applied holistically. For example:

  1. nix print-dev-env applies on the entire generated flake.nix, which is sourced from devbox.json. So, if there's an unapplied change to the packages in devbox.json, I don't see how we'd avoid having that be included.
  2. nix profile install has a priority field that relies on the order of packages in devbox.json

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.

Much nicer!

Comment on lines +216 to +217
// if mode is install or uninstall, then we need to update the nix-profile
// and lockfile, so we must continue below.
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 strictly true? for example adding a package that is already in devbox.json. Doesn't feel like a blocker though, I think it's OK

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm you are correct, its not strictly needed if the same package is being added or removed. We do run it, but its fairly fast (1 second), because its essentially a no-op, but not instant (0 seconds).

for _, pkg := range packages {
if item.Matches(pkg, d.lockfile) {
itemsToKeep = append(itemsToKeep, item)
continue outer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, can we replace continue outer with a found boolean?

for _, item := range profileItems {
	found := false
	for _, pkg := range packages {
		if item.Matches(pkg, d.lockfile) {
			itemsToKeep = append(itemsToKeep, item)
			found = true
                        break
		}
	}
	if !found {
		extras = append(extras, item)
	}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

haha yes, lets do that.

@savil savil force-pushed the savil/refactor-profile-updates branch from f110f09 to d5748d7 Compare October 5, 2023 21:33
@savil savil merged commit 06050da into main Oct 5, 2023
@savil savil deleted the savil/refactor-profile-updates branch October 5, 2023 22:27
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