You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When overriding dependencies using source-repository-package, the overridden packages become local packages.
This means that neither the source code or build results get cached between projects.
It also makes it difficult for nix tooling (or similar) to provide pre-cached packages as cabal-install will be reluctant to use a version of a package coming from a package database provided by this tooling.
Proposal
Add a setting to source-repository-package stanzas that allow setting it to be local or global.
Default this to local if it depends on a local package, ie, to enforce a local package transitive closure property.
I think this default would work well for I think the two main use cases:
overriding an external dependency (global)
bringing in a tightly coupled sibling package (local)
Reacted by Mikolaj Konarski, George Thomas, Siyuan Chen, Piotr Paradziński and Francesco Gazzetta
I think this is somewhat related to #5444. My impression is that a lot of the issues that people run into there could be fixed/alleviated by this proposal.
I don't know enough about this to comment, but I wonder if there are any hidden pitfalls with allowing that. But if there are, perhaps that would lead to a beneficial revision of the assumption and a rewrite.
If the proposal is accepted, who is going to implement that?
Thanks for thinking about this. I think you mention separate problems that are unreleated to the local/global distinction.
This means that neither the source code or build results get cached between projects.
A global cache of source-repository-packages could be implemented without any change in how source-repository-package are considered by the solver. It does require designing how to get it to work well though. Projects enjoy a cabal clean once in a while, while a global cache without gc can be forever.
The cabal store already has this issue so perhaps a common solution is needed.
It also makes it difficult for nix tooling (or similar) to provide pre-cached packages as cabal-install will be reluctant to use a version of a package coming from a package database provided by this tooling.
The problem with getting cabal to work well inside nix is mostly due to the fact inside the nix build sandbox you cannot do arbitrary IO; and that it is hard to get cabal to not do IO. Nix could prefect the repositories for example but there is no simple way to get cabal to use the prefetched repositories rather than doing the checkout itself.
bringing in a tightly coupled sibling package (local)
I don't understand how this situation can arise. A tighly coupled sibiling package ... but living in a separate repository?
A global cache of source-repository-packages could be implemented without any change in how source-repository-package are considered by the solver
Yes this is a great point. I agree that caching and solving are distinct concerns. Though I think the difficulties that haskell.nix runs into suggests that some changes would need to made to the solver for cabal-install to accept source-repository-packages that aren't built in place.
I agree that implementing this feature would make cabal store bloat worse, but that's orthogonal to this.
Nix could prefect the repositories for example but there is no simple way to get cabal to use the prefetched repositories rather than doing the checkout itself.
Just to be clear I'm not suggesting we should be calling cabal-install inside a nix sandbox. The issues I have in mind are about calling cabal-install from a nix shell where we want to provide pre-compiled dependencies. But, really nix stuff is orthogonal to this issue. It just happens to be the case that solving would, I think, make it easier to solve some issues haskell.nix faces.
I don't understand how this situation can arise. A tighly coupled sibiling package ... but living in a separate repository?
Perhaps I shouldn't have mentioned this. I was trying to come up with a scenario where someone truly wanted a local source-repository-package. I agree that this doesn't really happen in the wild: people just use mono-repos for tightly coupled packages. But I could imagine someone doing this.
Putting this another way.
If I want to override the source of one of my dependencies. I can do one of two things:
provide a source-repository-package
run my own hackage-server and upload a version there
(2) is a lot of work but nothing else about cabal-install's behaviour will change other than where the source code is coming from.
Whereas (1) will change how solving and caching works.
I would like to avoid having to set up my own hackage-server but still be able to override source code without changing behaviour.
If I want to override the source of one of my dependencies.
[without that package being considered local to the project]
Apologies if I sound repetitive. In this use case, I don't see the importance of the package not being considered local (beside caching, which has we discussed above is an idepedent problem).
The override is local to the project anyway.
As you notice, we cannot guarantee it can be considered global because it could depend on local packages.
By considering it local, you are guaranteed it will replace any other package with the same name (if you added it to a repository, you would need to use active-repositories: to make sure the solver picks that exact package and not another version from another repo).
One could think of caching some local packages but then we would have another "mode" to take into account (local cached / local not cached). I doubt this last idea would be worth the complexity.
The issues I have in mind are about calling cabal-install from a nix shell where we want to provide pre-compiled dependencies. But, really nix stuff is orthogonal to this issue. It just happens to be the case that solving would, I think, make it easier to solve some issues haskell.nix faces.
Wherever these issues are captured on GitHub, could you ping me on the respective GitHub issues?
The tl;dr is that haskell.nix needs to turn a source-repository-package into a package:. This is conceptually simple but a bit fragile when it's implemented by parsing and rewriting the cabal.project file. If there was a better API to do this, there would be no issues (and now there is, in the cabal-install library). Little steps and we will get there ☺️
beside caching, which has we discussed above is an independent problem
Sorry I think I was unclear above. While I do think clearing things from the cabal store is independent, caching is the main appeal of this feature request.
My problem with source-repository-packages is that they mean that whenever I check out a new worktree of a repository, I then have to download and build external packages that I already have built in another worktree.
Thanks for this link! I agree that this is easier to set up than a hackage-server, but it's still a lot of work compared to just having a set of source-repository-packages in a cabal.project and also it's less declarative.
Wherever these issues are captured on GitHub, could you ping me on the respective GitHub issues?
Done that now.
On haskell.nix stuff, I agree with you that a proper API is necessary for these two projects to interact. Yet, I think this feature request would solve this specific problem with a lot less work than setting that up. haskell.nix does the right thing for stack projects in this case and that's because stack deals with this in the way I'm suggesting.
Let me emphasize that I didn't mean to diminish the problems with the current DX!
I should have pointed out right away that it was my experience with source-repository-packages in the cardano ecosystem that led me to write the tool that Michael mentions in that thread!
I think at some point I had a git-wrapper script that would share the checkouts between projects, it was saving multiple gigabytes of downloads (and time!).
Somehow, in my mind this is orthogonal from the local/global distinction but maybe it should not be (ew.g. tests enabled by default comes up a lot).
The problem cabal-install rebuilding things is a little bit more complicated though.
works and it is considered global, and the build is cached in the store. The source distribution is not shared but downloading a tarball might be more efficient that a git checkout.
On other hand, extra-packages: only supports a package ids and optional-packages: only supports path locations.
The problem of cabal-install rebuilding is a bit more complicated though.
Yes, very fair! Rebuilding logic is always surprisingly tricky
So, it looks like source-repository-packages can automatically considered global. The checkout is not shared but the build is.
The build being shared is good to hear! I'm not sure how I've missed this thanks for pointing it out!
So, I guess this reduces this issue to just putting the checkouts into the cabal store?
[adding an entry to packages with an url] is considered global, and the build is cached in the store
This seems like a great workaround! I'll give this a try. Though I still think having a proper fix for the source-repository-package case is still the way to go, since that's the main thing I see people use.
One thing about this that really confuses me is why this is considered global at all. All of the *package options are documented as introducing local packages.
So a point of confusion about the local/global thing is the particular behaviour we see with haskell.nixshells, namely:
We have a global package-db with foo-X in it
And yet, when running cabal build, cabal chooses to build foo-X from the source-repository-package specification instead.
I think our hypothesis was that this was caused by source-repository-packages being considered as "local" packages. If that isn't true, then there's something else going on there. Something during planning
Though I still think having a proper fix for the source-repository-package case is still the way to go, since that's the main thing I see people use.
I don't know if I can call it "a fix". We might have identified some wishes but nothing that is broken or wrong. There seem to be a desire for:
git fetched packages
cached globally (source and build)
considered non project-local for tests/benchmarks (and I guess for target resolution too)
which cabal does (may) not have a solution for. This use case needs to be understood in the context of the current code and behaviour if we want to make progress.
One thing about this that really confuses me is why this is considered global at all. All of the *package options are documented as introducing local packages.
extra-packages: package list with version bounds (comma separated)
Specifies a list of external packages from Hackage, which should be considered local packages. The motivation for extra-packages is making libraries that are not dependencies of any package in the project available for use in ghci.
Either the documentation is wrong or we are totally confused about what is local and what is global.
~/Scratchpad/temp
❯ echo "extra-packages: aeson" > cabal.project
❯ cabal build --dry-run aeson
Warning: There are no packages or optional-packages in the project
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following would be built (use -v for more details):
- vector-0.13.1.0 (lib) (requires download & build)
- indexed-traversable-instances-0.1.1.2 (lib) (requires build)
- witherable-0.4.2 (lib) (requires build)
- semialign-1.3 (lib) (requires build)
- aeson-2.2.1.0 (lib) (requires build)
❯ jq '."install-plan"[] | select(."pkg-name" == "aeson") | { "pkg-name", style }' < dist-newstyle/cache/plan.json
{
"pkg-name": "aeson",
"style": "global"
}
I don't know if I can call it "a fix". We might have identified some wishes but nothing that is broken or wrong. ...
Yes well put! I would also add the following requirement to your list:
4. easy transition: users should have to make no/minimal changes to their cabal.project to make use of this.
This use case needs to be understood in the context of the current code and behaviour if we want to make progress.
Definitely! It feels to me like someone needs to spend a bit of time figuring out all the details of how all these parts fit together, before we can figure out how to proceed? I'd be happy to tackle this at this at some point, but I can't promise when I'll have time to do that
Notes for our future selves: this seems to be relevant bit
-- For this local build policy, every package that lives in a local source
-- dir (as opposed to a tarball), or depends on such a package, will be
-- built inplace into a shared dist dir. Tarball packages that depend on
-- source dir packages will also get unpacked locally.
shouldBuildInplaceOnly :: SolverPackage loc -> Bool
shouldBuildInplaceOnly pkg = Set.member (packageId pkg)
pkgsToBuildInplaceOnly
pkgsToBuildInplaceOnly :: Set PackageId
pkgsToBuildInplaceOnly =
Set.fromList
$ map packageId
$ SolverInstallPlan.reverseDependencyClosure
solverPlan
(map PlannedId (Set.toList pkgsLocalToProject))
isLocalToProject :: Package pkg => pkg -> Bool
isLocalToProject pkg = Set.member (packageId pkg)
pkgsLocalToProject
pkgsLocalToProject :: Set PackageId
pkgsLocalToProject =
Set.fromList (catMaybes (map shouldBeLocal localPackages))
--TODO: localPackages is a misnomer, it's all project packages
-- here is where we decide which ones will be local!
-- ...
shouldBeLocal :: PackageSpecifier (SourcePackage (PackageLocation loc)) -> Maybe PackageId
shouldBeLocal NamedPackage{} = Nothing
shouldBeLocal (SpecificSourcePackage pkg) = case srcpkgSource pkg of
LocalUnpackedPackage _ -> Just (packageId pkg)
_ -> Nothing
It's in Distribution.Client.ProjectPlanning, part of the project elaboration phase.
And local packages are defined in rebuildProject plan above:
distDirLayout
-- Look for all the cabal packages in the project
-- some of which may be local src dirs, tarballs etc
--
phaseReadLocalPackages :: ProjectConfig
-> Rebuild [PackageSpecifier UnresolvedSourcePackage]
phaseReadLocalPackages projectConfig@ProjectConfig {
projectConfigShared,
projectConfigBuildOnly
} = do
pkgLocations <- findProjectPackages distDirLayout projectConfig
-- Create folder only if findProjectPackages did not throw a
-- BadPackageLocations exception.
liftIO $ do
createDirectoryIfMissingVerbose verbosity True distDirectory
createDirectoryIfMissingVerbose verbosity True distProjectCacheDirectory
fetchAndReadSourcePackages verbosity distDirLayout
projectConfigShared
projectConfigBuildOnly
pkgLocations
Looking at findProjectPackages it looks like everything was called "local package" (as the comment in the elaboration phase above seem to confirm).
Here's an issue with pervasive global caching of src-repository-packages.
Suppose I point to HEAD of a repo. Now, with every build where that repo is updated, I will get a new checkout and a new compilation artifact, and these will go in the global store where they are never garbage collected. I think this could drive users somewhat nuts. We may want to limit to packages that point to a specific commit alone, for example.
Suppose I point to HEAD of a repo. Now, with every build where that repo is updated, I will get a new checkout and a new compilation artifact, and these will go in the global store where they are never garbage collected. I think this could drive users somewhat nuts. We may want to limit to packages that point to a specific commit alone, for example.
This is a great point. I feel like it deserves its own ticket, since it can be a problem irrespective of this feature. Even if you only cache these locally, you'd still have to redownload with each build, which would be quite slow.
I think a better approach then what I originally suggested would be to add a new stanza for global source-repo-packages that would avoid issues with this being a breaking change and also lets us clean up other deficiencies like not requiring a full git SHA. Maybe we could also add a proper hash of the output to avoid git commit SHA collisions.
Activity
TeofilC commentedon Oct 6, 2023
I think this is somewhat related to #5444. My impression is that a lot of the issues that people run into there could be fixed/alleviated by this proposal.
TeofilC commentedon Oct 6, 2023
I think this is one of the haskell.nix issues related to this: ican-group/haskell.nix#1367
Mikolaj commentedon Oct 6, 2023
I don't know enough about this to comment, but I wonder if there are any hidden pitfalls with allowing that. But if there are, perhaps that would lead to a beneficial revision of the assumption and a rewrite.
If the proposal is accepted, who is going to implement that?
andreabedini commentedon Oct 6, 2023
Thanks for thinking about this. I think you mention separate problems that are unreleated to the local/global distinction.
A global cache of source-repository-packages could be implemented without any change in how source-repository-package are considered by the solver. It does require designing how to get it to work well though. Projects enjoy a
cabal cleanonce in a while, while a global cache without gc can be forever.The cabal store already has this issue so perhaps a common solution is needed.
The problem with getting cabal to work well inside nix is mostly due to the fact inside the nix build sandbox you cannot do arbitrary IO; and that it is hard to get cabal to not do IO. Nix could prefect the repositories for example but there is no simple way to get cabal to use the prefetched repositories rather than doing the checkout itself.
I don't understand how this situation can arise. A tighly coupled sibiling package ... but living in a separate repository?
TeofilC commentedon Oct 6, 2023
I'd be happy to give it a go. Though I can't make any promises about when I'll have capacity
TeofilC commentedon Oct 6, 2023
Yes this is a great point. I agree that caching and solving are distinct concerns. Though I think the difficulties that haskell.nix runs into suggests that some changes would need to made to the solver for cabal-install to accept source-repository-packages that aren't built in place.
I agree that implementing this feature would make cabal store bloat worse, but that's orthogonal to this.
Just to be clear I'm not suggesting we should be calling
cabal-installinside a nix sandbox. The issues I have in mind are about callingcabal-installfrom a nix shell where we want to provide pre-compiled dependencies. But, really nix stuff is orthogonal to this issue. It just happens to be the case that solving would, I think, make it easier to solve some issues haskell.nix faces.Perhaps I shouldn't have mentioned this. I was trying to come up with a scenario where someone truly wanted a local
source-repository-package. I agree that this doesn't really happen in the wild: people just use mono-repos for tightly coupled packages. But I could imagine someone doing this.TeofilC commentedon Oct 6, 2023
Putting this another way.
If I want to override the source of one of my dependencies. I can do one of two things:
source-repository-package(2) is a lot of work but nothing else about cabal-install's behaviour will change other than where the source code is coming from.
Whereas (1) will change how solving and caching works.
I would like to avoid having to set up my own hackage-server but still be able to override source code without changing behaviour.
andreabedini commentedon Oct 9, 2023
[without that package being considered local to the project]
Apologies if I sound repetitive. In this use case, I don't see the importance of the package not being considered local (beside caching, which has we discussed above is an idepedent problem).
active-repositories:to make sure the solver picks that exact package and not another version from another repo).You can also use local no-index repository, which is much easier to set up.
One could think of caching some local packages but then we would have another "mode" to take into account (local cached / local not cached). I doubt this last idea would be worth the complexity.
andreabedini commentedon Oct 9, 2023
Wherever these issues are captured on GitHub, could you ping me on the respective GitHub issues?
The tl;dr is that haskell.nix needs to turn a☺️
source-repository-packageinto apackage:. This is conceptually simple but a bit fragile when it's implemented by parsing and rewriting the cabal.project file. If there was a better API to do this, there would be no issues (and now there is, in the cabal-install library). Little steps and we will get thereTeofilC commentedon Oct 9, 2023
Sorry I think I was unclear above. While I do think clearing things from the cabal store is independent, caching is the main appeal of this feature request.
My problem with
source-repository-packagesis that they mean that whenever I check out a new worktree of a repository, I then have to download and build external packages that I already have built in another worktree.Thanks for this link! I agree that this is easier to set up than a hackage-server, but it's still a lot of work compared to just having a set of
source-repository-packagesin a cabal.project and also it's less declarative.Done that now.
On haskell.nix stuff, I agree with you that a proper API is necessary for these two projects to interact. Yet, I think this feature request would solve this specific problem with a lot less work than setting that up. haskell.nix does the right thing for stack projects in this case and that's because stack deals with this in the way I'm suggesting.
TeofilC commentedon Oct 9, 2023
This comment from one of the linked issues seems to be calling for this feature as well: #5444 (comment)
andreabedini commentedon Oct 10, 2023
@TeofilC Thank you for the link and the ping.
Let me emphasize that I didn't mean to diminish the problems with the current DX!
I should have pointed out right away that it was my experience with source-repository-packages in the cardano ecosystem that led me to write the tool that Michael mentions in that thread!
I think at some point I had a git-wrapper script that would share the checkouts between projects, it was saving multiple gigabytes of downloads (and time!).
Somehow, in my mind this is orthogonal from the local/global distinction but maybe it should not be (ew.g. tests enabled by default comes up a lot).
The problem cabal-install rebuilding things is a little bit more complicated though.
andreabedini commentedon Oct 10, 2023
Oh well, it looks like Oleg is right (I should not be surprised 😂).
So, it looks like source-repository-packages can automatically considered global. The checkout is not shared but the build is.
andreabedini commentedon Oct 10, 2023
Another data point as I rediscover the codebase:
works and it is considered global, and the build is cached in the store. The source distribution is not shared but downloading a tarball might be more efficient that a git checkout.
On other hand,
extra-packages:only supports a package ids andoptional-packages:only supports path locations.At least the user guide clear about the use case for each option.
https://cabal.readthedocs.io/en/stable/cabal-project.html#specifying-the-local-packages
TeofilC commentedon Oct 10, 2023
Yes, very fair! Rebuilding logic is always surprisingly tricky
The build being shared is good to hear! I'm not sure how I've missed this thanks for pointing it out!
So, I guess this reduces this issue to just putting the checkouts into the cabal store?
This seems like a great workaround! I'll give this a try. Though I still think having a proper fix for the
source-repository-packagecase is still the way to go, since that's the main thing I see people use.One thing about this that really confuses me is why this is considered global at all. All of the
*packageoptions are documented as introducing local packages.michaelpj commentedon Oct 10, 2023
So a point of confusion about the local/global thing is the particular behaviour we see with
haskell.nixshells, namely:foo-Xin itcabal build, cabal chooses to buildfoo-Xfrom thesource-repository-packagespecification instead.I think our hypothesis was that this was caused by
source-repository-packages being considered as "local" packages. If that isn't true, then there's something else going on there. Something during planningandreabedini commentedon Oct 10, 2023
This is the script I was using https://gist.github.com/andreabedini/9647abb8b3d647a4020fd07e568a85c4, of course use it at your own risk :)
I don't know if I can call it "a fix". We might have identified some wishes but nothing that is broken or wrong. There seem to be a desire for:
which cabal does (may) not have a solution for. This use case needs to be understood in the context of the current code and behaviour if we want to make progress.
Either the documentation is wrong or we are totally confused about what is local and what is global.
TeofilC commentedon Oct 10, 2023
Thanks! I'll take a look
Yes well put! I would also add the following requirement to your list:
4. easy transition: users should have to make no/minimal changes to their cabal.project to make use of this.
Definitely! It feels to me like someone needs to spend a bit of time figuring out all the details of how all these parts fit together, before we can figure out how to proceed? I'd be happy to tackle this at this at some point, but I can't promise when I'll have time to do that
andreabedini commentedon Oct 10, 2023
Notes for our future selves: this seems to be relevant bit
It's in Distribution.Client.ProjectPlanning, part of the project elaboration phase.
andreabedini commentedon Oct 10, 2023
And local packages are defined in rebuildProject plan above:
Looking at findProjectPackages it looks like everything was called "local package" (as the comment in the elaboration phase above seem to confirm).
gbaz commentedon Oct 10, 2023
Here's an issue with pervasive global caching of src-repository-packages.
Suppose I point to HEAD of a repo. Now, with every build where that repo is updated, I will get a new checkout and a new compilation artifact, and these will go in the global store where they are never garbage collected. I think this could drive users somewhat nuts. We may want to limit to packages that point to a specific commit alone, for example.
mpickering commentedon Oct 11, 2023
Using a local noindex repository sounds like the right thing to do for this use case.
TeofilC commentedon Oct 12, 2023
This is a great point. I feel like it deserves its own ticket, since it can be a problem irrespective of this feature. Even if you only cache these locally, you'd still have to redownload with each build, which would be quite slow.
mpickering commentedon Jan 15, 2026
I don't think I agree with this comment any longer, having a global cache does sound useful if you have many projects pinning the same repos.
TeofilC commentedon Jan 15, 2026
I think a better approach then what I originally suggested would be to add a new stanza for global source-repo-packages that would avoid issues with this being a breaking change and also lets us clean up other deficiencies like not requiring a full git SHA. Maybe we could also add a proper hash of the output to avoid git commit SHA collisions.