Project specific .gemrc files using the GEMRC environment variable
Recently I needed to add a new source entry to my .gemrc
for a
private RubyGems server. I commit my .gemrc
to
Git, so this was a problem. Not only because I don't want
project specific changes cluttering up my .gemrc
(I don't), but also because
the URL of the source contains secrets that shouldn't be shared.
My default gemrc
:sources:
entry looked like this.
1
2
3
---
:sources:
- https://rubygems.org
There doesn't seem to be a supported way to have per-project .gemrc
files but
you can override the file using the GEMRC
environment variable. So if you can set a per-project GEMRC
environment
variable, you can have a per-project .gemrc
.
I use direnv
to set environment variables for my projects by
creating a .envrc
file in each of my project directories. So I can also use
that here. I just add a line like this.
1
export GEMRC=path/to/project/specific/gemrc
And in that file I add the extra key to :sources:
1
2
3
4
---
:sources:
- https://rubygems.org
- https://user:pass@custom.gem.server
I need to add the original https://rubygems.org
source too because although
the ~/.gemrc
will still be read, the keys in the project specific file will
replace any keys with the same name completely - they are not merged. You can
see what values gem
is using by running gem environment
. Great for
debugging.
I think using GEMRC
for this purpose is a reasonable workaround for
per-project .gemrc
files.