Skip to content

Commit b487c93

Browse files
committed
Prefer non pre-existing packages
When choosing between two packages in the plan with the same version number, choose the non pre-existing package.
1 parent be65bfc commit b487c93

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

nix-tools/nix-tools/plan2nix/Plan2Nix.hs

+15-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ writeDoc file doc =
7979
hPutDoc handle doc
8080
hClose handle
8181

82+
-- PackageKey is used when selecting which version is the most recent
83+
-- when to deduplicate the plan.
84+
-- By including PackageType as well as version we can select the
85+
-- NonPreExisting version of the package if the versions match.
86+
data PackageType = PreExisting | NotPreExisting deriving (Eq, Ord)
87+
data PackageKey = PackageKey Version PackageType deriving (Eq, Ord)
88+
8289
plan2nix :: Args -> Plan -> IO NExpr
8390
plan2nix args Plan { packages, extras, components, compilerVersion, compilerPackages } = do
8491
-- TODO: this is an aweful hack and expects plan-to-nix to be
@@ -260,13 +267,19 @@ value2plan plan = Plan { packages, components, extras, compilerVersion, compiler
260267
filterInstallPlan :: (Value -> Maybe b) -> HashMap Text b
261268
filterInstallPlan f = fmap snd .
262269
-- If the same package occurs more than once, choose the latest
263-
Map.fromListWith (\a b -> if parseVersion (fst a) > parseVersion (fst b) then a else b)
264-
$ mapMaybe (\pkg -> (,) (pkg ^. key "pkg-name" . _String) . (pkg ^. key "pkg-version" . _String,) <$> f pkg)
270+
Map.fromListWith (\a b -> if fst a > fst b then a else b)
271+
$ mapMaybe (\pkg -> (,) (pkg ^. key "pkg-name" . _String) . (getPackageKey pkg,) <$> f pkg)
265272
$ Vector.toList (plan ^. key "install-plan" . _Array)
266273

267274
parseVersion :: Text -> Version
268275
parseVersion s = fromMaybe (error $ "Unable to parse version " <> show s) . simpleParsec $ Text.unpack s
269276

277+
getPackageKey :: Value -> PackageKey
278+
getPackageKey pkg = PackageKey (parseVersion (pkg ^. key "pkg-version" . _String)) (
279+
if pkg ^. key "type" . _String == "pre-existing"
280+
then PreExisting
281+
else NotPreExisting)
282+
270283
-- Set of components that are included in the plan.
271284
components :: HashSet Text
272285
components =

0 commit comments

Comments
 (0)