(ctx context.Context, ref flake.Ref, refresh bool)
| 212 | } |
| 213 | |
| 214 | func lockFlake(ctx context.Context, ref flake.Ref, refresh bool) (flake.Ref, error) { |
| 215 | if ref.Locked() { |
| 216 | return ref, nil |
| 217 | } |
| 218 | |
| 219 | // Nix requires a NAR hash for GitHub flakes to be locked. A Devbox lock |
| 220 | // file is a bit more lenient and only requires a revision so that we |
| 221 | // don't need to download the nixpkgs source for cached packages. If the |
| 222 | // search index is ever able to return the NAR hash then we can remove |
| 223 | // this check. Skip this shortcut when refresh is requested — the caller |
| 224 | // is asking us to re-query upstream for a newer rev. |
| 225 | if !refresh && ref.Type == flake.TypeGitHub && (ref.Rev != "") { |
| 226 | return ref, nil |
| 227 | } |
| 228 | |
| 229 | var meta nix.FlakeMetadata |
| 230 | var err error |
| 231 | // For nixpkgs, we cache resolutions (currently flakeCacheTTL=30 days) to avoid downloading |
| 232 | // new nixpkgs too often which is really slow and rarely changes anything. |
| 233 | // |
| 234 | // Ideally we can do something similar for all packages (flake and otherwise) |
| 235 | // Specifically, if user adds python@3.12 (or python@latest that resolves to 3.12) and that |
| 236 | // package is already installed, we should use it instead of using 3.12 from search service |
| 237 | // (which may have different store path). This would allow all devbox projects to share packages |
| 238 | // if the version resolution is the same. |
| 239 | // |
| 240 | // That said, the logic for caching resolved versions and non-locked flake references would not |
| 241 | // be the same. |
| 242 | if ref.IsNixpkgs() && !refresh { |
| 243 | meta, err = nix.ResolveCachedFlake(ctx, ref) |
| 244 | } else { |
| 245 | meta, err = nix.ResolveFlake(ctx, ref, refresh) |
| 246 | } |
| 247 | |
| 248 | if err != nil { |
| 249 | return ref, err |
| 250 | } |
| 251 | return meta.Locked, nil |
| 252 | } |
no test coverage detected