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.
(profile string)
| 101 | // nix profile wipe-history. profile should be a path to the "default" symlink, |
| 102 | // like .devbox/nix/profile/default. |
| 103 | func wipeProfileHistory(profile string) error { |
| 104 | link, err := os.Readlink(profile) |
| 105 | if errors.Is(err, os.ErrNotExist) { |
| 106 | return nil |
| 107 | } |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | dir := filepath.Dir(profile) |
| 113 | entries, err := os.ReadDir(dir) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | for _, dent := range entries { |
| 118 | if dent.Name() == "default" || dent.Name() == link { |
| 119 | continue |
| 120 | } |
| 121 | err := os.Remove(filepath.Join(dir, dent.Name())) |
| 122 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 123 | return err |
| 124 | } |
| 125 | } |
| 126 | return nil |
| 127 | } |
no test coverage detected