Add env and env_file support to machine configuration#498
Open
tuler wants to merge 1 commit into
Open
Conversation
🦋 Changeset detectedLatest commit: a6f9068 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Coverage Report
📁 File Coverage (19 files)
|
brunomenezes
reviewed
Jul 1, 2026
| envMap[variable] = ""; | ||
| } else { | ||
| envMap[variable.slice(0, eq)] = variable.slice(eq + 1); | ||
| } |
Contributor
There was a problem hiding this comment.
@tuler,
These slices are odd. Could it be done by using a destructuring based on the assumptions of the variable content?
e.g.
const [key, value=""] = variable.split("=");
envMap[key] = value;
Contributor
|
The PR description is strongly on the "how", but I don't understand clearly the "why". Does it help solve an issue Lyno opened? |
Allow injecting environment variables into the Cartesi Machine build beyond the Dockerfile ENV controlled by use_docker_env: - env: an inline table of key/value pairs in [machine.env] - env_file: a path to a .env file Precedence (lowest to highest): docker image ENV (when use_docker_env is enabled), env_file, then the env table. Also fixes a latent bug where env values containing "=" were truncated at the first "=". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vnD51QAGwAvqaZuEZJypr
683a9eb to
a6f9068
Compare
Member
Author
Added a motivation section to the PR description. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Environment variables are typically used to differentiate between multiple build or test scenarios. One classic examples are testnet vs devnet vs mainnet, but there may be other scenarios.
Currently the build process only supports environment variables defined inside the Dockerfile used by the cartesi build process. But the same variables defined in the Dockerfile may be necessary outside the cartesi build process. For example in a host build process that is not related to the cartesi build process at all. So in this case there is a duplication of environment variables values, which hurts maintenance.
By having
.envfiles support we can maintain a single source of truth for environment variables and use it in both the cartesi build workflow and other workflows.Summary
This PR adds support for injecting environment variables into the Cartesi Machine through two new configuration options in the
[machine]section ofcartesi.toml: anenvtable for inline key/value pairs and anenv_fileoption pointing to a.envfile.Key Changes
env(Record<string, string>) andenvFile(optional string) toMachineConfigparseStringRecord()to parse TOML tables into environment variable records, with automatic coercion of non-string scalar values (numbers, booleans) to strings.envfile parsing: AddedparseEnvFile()function that parses.envfiles with support for:#commentsexportprefix=signs in valuesbootMachine()to merge environment variables with proper precedence (lowest to highest):ENV(whenuse_docker_envis enabled)env_fileenvtableInvalidEnvErrorexception class for invalid env configurationsImplementation Details
cartesi-machine, allowing later sources to override earlier ones.envfile parser is robust and handles common.envfile formatsenvtable are automatically converted to strings since environment variables are always stringshttps://claude.ai/code/session_016vnD51QAGwAvqaZuEZJypr