cpanpm
cpanpm copied to clipboard
Add environment variable to customize .cpan path
So my home directory is not cluttered and I can make CPAN follow the XDG Basedir Spec.
PS: Can I customize where local::lib
dumps its shit? That perl5
folder is bogging me...
PS: Can I customize where
local::lib
dumps its shit? Thatperl5
folder is bogging me...
A workaround for that is to install local::lib and export the environment variables before starting cpan for the first time. I agree that it would be nice if it was easier to do without installing local::lib beforehand.
EDIT: one example of the environment variables is
export PATH="$HOME/.local/share/perl/bin${PATH:+:${PATH}}"
export PERL5LIB="$HOME/.local/share/perl/lib/perl5"
export PERL_LOCAL_LIB_ROOT="$HOME/.local/share/perl"
export PERL_MB_OPT="--install_base '$HOME/.local/share/perl'"
export PERL_MM_OPT="INSTALL_BASE=$HOME/.local/share/perl"
A workaround for that is to install local::lib and export the environment variables before starting cpan for the first time. I agree that it would be nice if it was easier to do without installing local::lib beforehand.
EDIT: one example of the environment variables is
export PATH="$HOME/.local/share/perl/bin${PATH:+:${PATH}}" export PERL5LIB="$HOME/.local/share/perl/lib/perl5" export PERL_LOCAL_LIB_ROOT="$HOME/.local/share/perl" export PERL_MB_OPT="--install_base '$HOME/.local/share/perl'" export PERL_MM_OPT="INSTALL_BASE=$HOME/.local/share/perl"
This is helpful, but I can't get cpan
to work with such a setup.
I have moved the perl stuff to ${HOME}/.local/share/perl
with the suggested PERL
and PATH
environment variables set.
I run cpan
initially, and it creates ${HOME}/.cpan
. If I block this creation (for example, by creating a read-only ${HOME}/.cpan
file before running cpan
, it exits with
mkdir /Users/bwaters/.cpan: File exists at /System/Library/Perl/5.30/CPAN/HandleConfig.pm line 595.
If I remove that experimental block, it goes through the full setup of cpan. I tell it where it should set things. And indeed, after all there are correct paths in the resulting MyConfig.pm
-- just in the $HOME/.cpan
dirs.
Try again (all commands are run from $HOME):
% rm -rf .cpan
% rm -rf .local/share/{cpan,perl}
% mkdir .local/share/{cpan,perl}
% ln -s .local/share/cpan .cpan
% cpan
And we do another first-time setup, only this time, I specify the cpan_home
, but accept all other defaults. Modules seem to get built. Then we are at a working interactive cpan prompt.
I exit, then remove the vestigial symlink in $HOME/.cpan => $HOME/.local/share/cpan
.
Then run cpan
again. Of course, it has no idea what I'm talking about, and gets set to create the whole thing again in $HOME/.cpan.
I try to get a bit further by specifying my CPAN config that we just created, there's all the right paths there:
% grep cpan_home .local/share/cpan/CPAN/MyConfig.pm
'cpan_home' => q[/Users/bwaters/.local/share/cpan],
CPAN is happy with this. So happy, it claims everything has been done already and we can all go home:
% cpan -j ${HOME}/.local/share/cpan/CPAN/MyConfig.pm
Nothing to install!
I haven't yet found a way to explain $XDG_DATA_HOME
to cpan.
% cpan -j ${HOME}/.local/share/cpan/CPAN/MyConfig.pm Nothing to install!
I haven't yet found a way to explain
$XDG_DATA_HOME
to cpan.
Got this working.
Make sure that whatever directory you're using for your .cpan
configuration dir is in perl's @INC
path.
Here is a shell init script that I'm using to set environment variables appropriately:
# updated: 2023-12-06 18:49:10
export VERSIONER_PERL_VERSION=5.34
export PERL_LOCAL_LIB_ROOT="${XDG_DATA_HOME}/perl"
export PERL_CPANM_HOME="${PERL_LOCAL_LIB_ROOT}/cpan"
export PERL5LIB="${PERL_CPANM_HOME}:${PERL_LOCAL_LIB_ROOT}/lib/perl5"
export PERL_MB_OPT="--install_base '${PERL_LOCAL_LIB_ROOT}'"
export PERL_MM_OPT=" INSTALL_BASE='${PERL_LOCAL_LIB_ROOT}'"
Note that the environment variable PERL_CPANM_HOME
is a variable used by the perl module App:cpanminus
cpanminus and IS NOT used by mainline CPAN -- but it's a convenient idea so I've used it in my shell script.
Note that (again, for mainline CPAN) you also have to set the absolute path to the .cpan
config dir (whatever name you want not just 'dot-cpan') in your CPAN/MyConfig.pm
file. If you have already configured CPAN and have moved a populated directory (as I did in my previous comment), it may be easiest to just search/replace the text in the config file via your editor or method of choice. Otherwise, you can use the cpan o conf
command in the cpan
shell.
Here is what I get when I have that CPAN/MyConfig.pm
set to correspond to my environment:
❯ cpan
Loading internal logger. Log::Log4perl recommended for better logging
cpan shell -- CPAN exploration and modules installation (v2.28)
Enter 'h' for help.
cpan[1]> o conf | grep local
$CPAN::Config options from /Users/bwaters/.local/share/perl/cpan/CPAN/MyConfig.pm:
build_dir [/Users/bwaters/.local/share/perl/cpan/build]
cpan_home [/Users/bwaters/.local/share/perl/cpan]
histfile [/Users/bwaters/.local/share/perl/cpan/histfile]
keep_source_where [/Users/bwaters/.local/share/perl/cpan/sources]
prefs_dir [/Users/bwaters/.local/share/perl/cpan/prefs]
cpan[2]>
To summarize:
- Initially, run
cpan
and run through the manual init process. There, make sure to setcpan_home
to the directory path of your choice. Exit thecpan
session; your configuration will be written to$HOME/.cpan
(CPAN home path). - Move that
$HOME/.cpan
to the CPAN home path that you chose in Step 1. - Edit the
CPAN/MyConfig.pm
text and replace$HOME/.cpan
with your CPAN home path. - Use a shell init script to set shell environment variables to ensure that the CPAN home path is in
perl
's@INC
path.
(You may see that in my example here, I'm running macOS. Apple ships two versions of Perl with the OS, 5.30 and 5.34. I've chosen to run with the more recent version (which is still out of date); I've set this choice via the VERSIONER_PERL_VERSION
environment variable.)