Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove pkgs from nix profile that are no longer on devbox.json (jetif…
…y-com#1181) ## Summary The goal is simple: if a package no longer exists in `devbox.json`, then it should be removed from the nix profile. The implementation is not as straightforward, because comparing packages can be very slow. Here we: * Add memoization to `nix.Input` so that it remembers the normalized package attribute path if it ever had to resolve it. * Compares list of of n packages in devbox.json against m pkgs in the profile, resulting in n+m calls, but _only_ if n != m, which is usually not the case in the happy path. * Removes pkgs from the profile by passing in the indeces. This is so that we don't have to compare again whether an Input.Raw exists in the profile. Notes/questions for future improvements: * We're still creating a bunch of `nix.Input`s in a bunch of places, and we end up making more calls to get the normalized attribute path when comparing packages. We should add a global cache or something to avoid duplicate calls. * Apart from avoiding _duplicate_ calls, I think there's ways to avoid the calls altogether: 1. Could we change `devbox.lock` to already contain the normalized attribute paths in its `resolved` field? That way we can probably skip many of the normalization calls. 2. When reading an item from the nix profile, can't we assume that the resolvedReference already has the normalized attribute path? Fixes jetify-com#1138 ## How was it tested? Added a bunch of packages with `devbox add`. Removed them manually from `devbox.json` and called a devbox command. Added fine logging to inspect what was going on and ensure not too many extra expensive calls are being added. Example output below: ``` > nix profile list --profile .devbox/nix/profile/default 0 github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.go github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.go /nix/store/ihz3bv2mzrwxrjzq0i2ysin7kj4i61bv-go-1.20.3 1 github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#legacyPackages.x86_64-darwin.actionlint github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#legacyPackages.x86_64-darwin.actionlint /nix/store/68valrl03a40yzf3qcppfyq468cbc18k-actionlint-1.6.23 2 github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.golangci-lint github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.golangci-lint /nix/store/sc143w85a2sb6xxg6lp073az6rraa1s8-golangci-lint-1.52.2 3 github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.cowsay github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.cowsay /nix/store/c2a8wbqcss1y8yhnx5n1p3awn8cxw0pv-cowsay-3.7.0 /nix/store/v9dccca6yrg53j6zxa9wi46h5n5vxsbl-cowsay-3.7.0-man > cat devbox.json { "packages": [ "go@1.20", "actionlint@1.6.23", "golangci-lint@1.52.2" ], ... > db run -- cowsay 'hi' # after cowsay was manually removed Ensuring packages are installed. checking profile input (unresolved): github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.cowsay against: go@1.20 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go) !! searched: legacyPackages.x86_64-darwin.cowsay !! searched: legacyPackages.x86_64-darwin.go against: actionlint@1.6.23 (resolved=github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#actionlint) against: golangci-lint@1.52.2 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#golangci-lint) memoized: legacyPackages.x86_64-darwin.cowsay !! searched: legacyPackages.x86_64-darwin.golangci-lint checking profile input (unresolved): github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.go against: go@1.20 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go) !! searched: legacyPackages.x86_64-darwin.go memoized: legacyPackages.x86_64-darwin.go checking profile input (unresolved): github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#legacyPackages.x86_64-darwin.actionlint against: go@1.20 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go) against: actionlint@1.6.23 (resolved=github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#actionlint) !! searched: legacyPackages.x86_64-darwin.actionlint !! searched: legacyPackages.x86_64-darwin.actionlint checking profile input (unresolved): github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.golangci-lint against: go@1.20 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go) !! searched: legacyPackages.x86_64-darwin.golangci-lint memoized: legacyPackages.x86_64-darwin.go against: actionlint@1.6.23 (resolved=github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#actionlint) against: golangci-lint@1.52.2 (resolved=github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#golangci-lint) memoized: legacyPackages.x86_64-darwin.golangci-lint memoized: legacyPackages.x86_64-darwin.golangci-lint found 1 extras to remove .cmd.sh: line 3: cowsay: command not found > nix profile list --profile .devbox/nix/profile/default 0 github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.go github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.go /nix/store/ihz3bv2mzrwxrjzq0i2ysin7kj4i61bv-go-1.20.3 1 github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#legacyPackages.x86_64-darwin.actionlint github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#legacyPackages.x86_64-darwin.actionlint /nix/store/68valrl03a40yzf3qcppfyq468cbc18k-actionlint-1.6.23 2 github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.golangci-lint github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#legacyPackages.x86_64-darwin.golangci-lint /nix/store/sc143w85a2sb6xxg6lp073az6rraa1s8-golangci-lint-1.52.2 ```
- Loading branch information