Cabal: Cabal doesn't work when $HOME is not writable

Created on 17 Sep 2017  路  9Comments  路  Source: haskell/cabal

If you ever try to write a Nix package that runs cabal build, you run into this issue:

Config file /homeless-shelter/.cabal/config not found.
Writing default configuration to /homeless-shelter/.cabal/config
/homeless-shelter: createDirectory: permission denied (Permission denied)

This is because in the context of a Nix build, you don't have a home directory (and, I believe, Nix sets the HOME variable to /homeless-shelter, which is not a real location, just to be cute).

This isn't a huge problem - you can work around it by running HOME=$TMP cabal build instead to use the temporary directory as your home directory - but this seems like an unnecessary complication and I'm wondering if we could avoid it altogether.

Does Cabal actually need to write to $HOME/.cabal? Could it skip whatever it's trying to do there and just build the package?

enhancement

Most helpful comment

@23Skidoo That's because new-build builds things into a common directory to avoid unnecessarily rebuilding dependencies that are shared multiple projects, right?

The best suggestion I can come up with is: If ~/.cabal isn't writable, print an error message that explains why Cabal wants an external directory to write to, what directory it tried to use by default, and what command-line option you can pass it to specify a different directory.

Maybe something like this? -

To avoid unnecessarily rebuilding dependencies, Cabal keeps a cache of build
artifacts in a shared location outside of the present working directory.

The default directory is: $HOME/.cabal
which resolved to: /homeless-shelter/.cabal

You are unable to write to this directory, so Cabal cannot continue.
To specify a different path, use the --cabal-home option.

All 9 comments

@chris-martin just wondering, does it work if you specify a file it can write to via cabal --config-file=/tmp/dummy.config build? (you could also try --config-file=/dev/null, but that behaves differently)

Does Cabal actually need to write to $HOME/.cabal? Could it skip whatever it's trying to do there and just build the package?

It should be easy to fix cabal build in this way, cabal new-build -- not so much.

@hvr Nope, doesn't help. The first lines of output about the config file go away, but I still end up with

/homeless-shelter: createDirectory: permission denied (Permission denied)

@23Skidoo That's because new-build builds things into a common directory to avoid unnecessarily rebuilding dependencies that are shared multiple projects, right?

The best suggestion I can come up with is: If ~/.cabal isn't writable, print an error message that explains why Cabal wants an external directory to write to, what directory it tried to use by default, and what command-line option you can pass it to specify a different directory.

Maybe something like this? -

To avoid unnecessarily rebuilding dependencies, Cabal keeps a cache of build
artifacts in a shared location outside of the present working directory.

The default directory is: $HOME/.cabal
which resolved to: /homeless-shelter/.cabal

You are unable to write to this directory, so Cabal cannot continue.
To specify a different path, use the --cabal-home option.

related/dup: https://github.com/haskell/cabal/issues/924

(i'd prefer to close that in favor of this, since this has more details, but i think cabal maintainers lean the other way, so not taking either action)

Hi @23Skidoo @chris-martin , is there any workaround for using cabal new-build in Nix project? I encounter this also when using cabal new-build inside my derivation buildPhase phase.

On master (not sure about 2.2) you can set $CABAL_DIR to point somewhere other then ~/.cabal.

@23Skidoo thank you, yes i'm using master branch. It solved, but i got another weird behavior.

in my derivation i have:

buildPhase = ''
  export CABAL_DIR=$src/.cabal
  cabal new-build .
'';

when i execute with nix-build it says that No cabal.project file or cabal file matching the default glob./*cabalwas found error. Does doing custom $CABAL_DIR will somehow affect this behavior?

i tried cabal new-build $src and cabal new-build but the error still happening.

I saw such an error when I had an empty cabal.project file in one of the parent directories.

Was this page helpful?
0 / 5 - 0 ratings