An unofficial Ruby wrapper for the Varlock CLI.
Varlock resolves and validates your configuration. This gem returns the results as
typed Ruby values and can apply them to ENV.
Platform gems include Varlock 1.23.0 for macOS ARM64 (arm64-darwin) or Linux
x86_64 with glibc (x86_64-linux-gnu). You do not need to install Varlock or Node.js
separately. Alpine/musl, Intel macOS, Linux ARM64, and Windows are not packaged.
Configure your .env.schema and .env files using Varlock.
For a source checkout, run ruby script/build.rb before bundle install to
download and package the host binary. Published platform gems include the binary,
so they do not download it during installation or at runtime.
Version 1.23.0 is available on RubyGems. Add it to your Gemfile:
# Gemfile
gem "varlock", "~> 1.23.0"Then run bundle install. To use a source checkout instead, add
path: "/path/to/varlock" to the gem declaration or install the built platform
gem from pkg/.
bundle exec varlock --version
bundle exec varlock load
bundle exec varlock run -- ruby app.rbThe Ruby launcher runs the bundled binary and passes through arguments,
streams, and exit status. It never searches PATH for another varlock.
require "varlock"
config = Varlock.load! # Typed values; leaves ENV unchanged
config.fetch("DATABASE_URL")
Varlock.load_and_set! # Stringifies values and updates ENV
ENV.fetch("DATABASE_URL")
# Optional settings:
Varlock.load_and_set!(path: "/path/to/app", env: "production", executable: "/usr/local/bin/varlock")
Varlock.load_and_set!(path: "/path/to/monorepo", env: nil, filter: "#api")Both methods accept the same options and run varlock load --format json without
a shell. If you omit the path, Varlock uses its default search location.
The environment defaults to
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development", evaluated on each call.
Pass env: to override it, or env: nil to omit the CLI's --env option.
A schema's @currentEnv takes precedence over env:. By default, the loader uses
the bundled binary.
The executable option is a single program path, not a shell command.
load! returns a hash with the original JSON types, including nil values, without
changing ENV. load_and_set! returns a hash of the strings it applies to ENV,
overwriting matching keys with Varlock's resolved values and leaving unrelated
keys unchanged. Strings stay as they are, numbers and booleans become strings,
and arrays and objects are JSON encoded. Null values and omitted keys leave any
existing value unchanged. The loader parses and checks all output before changing
any environment variables. Call it at startup, before starting application threads.
Each call resolves the configuration again.
Varlock.config contains the original JSON values from the last successful load,
with unchanged string keys and native Ruby numbers, booleans, arrays, hashes, and
nil values. It is nil before the first successful load. Each successful load replaces
it; failed loads leave it unchanged. Both methods update Varlock.config.
Code that previously used load! to populate ENV should use load_and_set!.
To use a Sorbet struct, transform the keys in your application's env.rb and
create the struct after loading. For example, given schema keys APP_PORT and
APP_DEBUG:
class EnvConfig < T::Struct
const :port, Integer
const :debug, T::Boolean
end
config = T.must(Varlock.config)
attributes = config.transform_keys { |key| key.delete_prefix("APP_").downcase.to_sym }
Env = T.let(EnvConfig.new(**attributes), EnvConfig)The config keys must match the struct's fields after transformation. Custom enum
values still need conversion to their Ruby enum instances. No sorbet-schema
deserialization or custom coercer is needed for native JSON types.
The loader raises Varlock::Error if the CLI fails, the executable is missing, or
the output is invalid. CLI errors include Varlock's stderr diagnostics, so you can
see configuration errors without resolving a second time. Varlock redacts its
diagnostics; custom executables should also avoid writing secrets to stderr.
Captured JSON stdout and JSON parse details are never included in errors.
If the CLI provides no diagnostics, the error suggests running bundle exec varlock load.
Use the optional Rails entry point instead of the default require:
# Gemfile
gem "varlock", "~> 1.23.0", require: "varlock/rails"The Railtie calls Varlock.load_and_set! in before_configuration, using the application
root and Rails.env. It runs in all environments, including tests, and a failure
stops application boot. Rails remains an optional dependency.
For variables needed earlier (for example, by another gem at require time), use
require: false in the Gemfile and load explicitly in config/boot.rb, immediately
after require "bundler/setup":
require "varlock"
Varlock.load_and_set!(path: File.expand_path("..", __dir__))Choose either explicit boot loading or the Rails adapter to avoid resolving twice. Avoid loading a second dotenv loader afterward, which could overwrite validated values.
If you already launch through varlock run -- bundle exec rails server, you can
use the injected ENV directly without this gem. The adapter does not detect or
skip a surrounding varlock run, and this gem does not add runtime log redaction.
The Rails adapter registers the validation task automatically:
bin/rails varlock:check
RAILS_ENV=production bin/rails varlock:check
# Or: bundle exec rake varlock:checkFor a plain Ruby project, add this to your Rakefile:
require "varlock/tasks"Then run bundle exec rake varlock:check. The task runs varlock load --format pretty
and passes through stdout and stderr, including Varlock's validation summary and redaction.
It preserves the CLI's exit code on validation failure. It does not apply values
to ENV or update Varlock.config.
Configure the task using VARLOCK_PATH, VARLOCK_FILTER, and VARLOCK_ENV:
VARLOCK_PATH=/path/to/app VARLOCK_FILTER='#api' bundle exec rake varlock:check
VARLOCK_PATH=/path/to/app VARLOCK_FILTER='#api' VARLOCK_ENV=production bin/rails varlock:check
VARLOCK_FILTER='APP_*,!APP_SECRET' bundle exec rake varlock:checkThe task accepts configuration only through environment variables. Without overrides,
plain Rake uses the loader's default path and environment; Rails uses the application
root and Rails.env. VARLOCK_ENV overrides the default environment in both cases.
The filter controls which items the CLI shows.
The task does not initialize the Rails application. When varlock:check is the
only requested task, the Rails adapter skips its auto-loading hook, so validation
runs once with the supplied overrides. If you request additional tasks in the same
command, the hook still runs to supply their environment configuration.
ruby script/build.rb
bundle install
bundle exec rake test
ruby script/smoke.rbDefault tests use a fixture executable and do not need Varlock or real secrets. To include
the actual Rails boot test, install with RAILS_VERSION='~> 7.2.0' bundle install
and run RAILS_VERSION='~> 7.2.0' bundle exec rake test on Ruby 3.2+.
CI builds and tests Linux x86_64 and macOS ARM64 packages, including the installed
gem launcher and real CLI integration. The gem supports Ruby 2.7+;
Rails requires a newer Ruby version.
Use Ruby 3.2+ for development tooling, then run bundle install and
bundle exec rubocop. The configuration comes from 811spotter's API
and keeps core Ruby rules, rubocop-performance, and the pinned rubocop-sane
plugin. It excludes Rails, RSpec, GraphQL, Sorbet, and app-specific rules.
Test exclusions use test/ rather than spec/.
The lint target is Ruby 2.7, the oldest target supported by this RuboCop version; the gem's runtime requirement is Ruby 2.7+. Importing these rules did not include reformatting existing files in bulk.
Build the bundled CLI, then enable the optional integration tests:
ruby script/build.rb
VARLOCK_INTEGRATION=1 bundle exec rake test
# Include the Rails test too, after installing its dependency as described above:
RAILS_VERSION='~> 7.2.0' VARLOCK_INTEGRATION=1 bundle exec rake testThe real CLI tests copy test/fixtures/schema/.env.schema into a temporary
directory. They check defaults, numbers and booleans, interpolation, synthetic
sensitive values, empty and undefined values, .env.test overrides, and failures
for an invalid number or missing required value. They use no real credentials or
external services.
These tests were verified locally with Varlock 1.23.0 and Ruby 3.3.5.
To try the fixture directly from this checkout:
ruby -Ilib -rvarlock -e 'Varlock.load_and_set!(path: "test/fixtures/schema"); puts ENV.fetch("VARLOCK_REAL_URL")'
# http://localhost:3000The Ruby wrapper is available under the MIT license. The bundled Varlock CLI retains its upstream MIT license. See RELEASING.md for validation and publishing instructions.
ruby script/build.rb arm64-darwin
ruby script/build.rb x86_64-linux-gnuYou can run either build command on either supported host, but the resulting
executable only runs on its matching platform. The build writes packages to pkg/.
Each contains only its platform's executable and upstream encryption helpers, plus the upstream MIT
license in licenses/VARLOCK-MIT.txt. Downloads are cached under vendor/downloads
by CLI version and checked against pinned SHA-256 hashes in lib/varlock/release.json.
Source maps are omitted. Binaries and built gems are ignored by Git.
ruby script/update_binaries.rb 1.23.0 # Replace with the desired release version
RAILS_VERSION='~> 7.2.0' VARLOCK_INTEGRATION=1 bundle exec rake test
ruby script/smoke.rbThe updater downloads the release's checksums.txt, both platform archives, and
its license. It verifies both archives before updating the version/checksum pins
in lib/varlock/release.json, refreshes the license, and rebuilds both gems in
pkg/. A failed download or checksum check leaves the existing pins and binaries
unchanged. If a build fails after the pins are updated, rerun script/build.rb
for that platform. You must specify a stable release version. The script does not
select the latest version or publish packages.
Review the manifest and license changes and run runtime tests on both hosts.
The gem version matches the embedded CLI version. The updater generates
lib/varlock/version.rb from the verified release version before building both
packages, so you do not need to bump the version manually. The download cache
keeps versions separate to prevent reuse of older archives.