Skip to content
Merged
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
39 changes: 39 additions & 0 deletions internal/devbox/nixprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/samber/lo"
Expand Down Expand Up @@ -84,5 +86,42 @@ func (d *Devbox) syncNixProfileFromFlake(ctx context.Context) error {
return fmt.Errorf("error installing packages in nix profile %s: %w", add, err)
}
}
if len(add) > 0 || len(remove) > 0 {
err := wipeProfileHistory(profilePath)
if err != nil {
// Log the error, but nothing terrible happens if this
// fails.
slog.DebugContext(ctx, "error cleaning up profile history", "err", err)
}
}
return nil
}

// wipeProfileHistory removes all old generations of a Nix profile, similar to
// nix profile wipe-history. profile should be a path to the "default" symlink,
// like .devbox/nix/profile/default.
func wipeProfileHistory(profile string) error {
link, err := os.Readlink(profile)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}

dir := filepath.Dir(profile)
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, dent := range entries {
if dent.Name() == "default" || dent.Name() == link {
continue
}
err := os.Remove(filepath.Join(dir, dent.Name()))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
}
return nil
}
Loading