Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mise tools in env template #1982

Closed
abatilo opened this issue Apr 28, 2024 · 10 comments · Fixed by #3598
Closed

Use mise tools in env template #1982

abatilo opened this issue Apr 28, 2024 · 10 comments · Fixed by #3598

Comments

@abatilo
Copy link
Contributor

abatilo commented Apr 28, 2024

I'd love to be able to use mise installed tools to execute env exec templates.

If I have the following example .mise.toml

[tools]
go = "latest"

[env]
EXAMPLE = "{{exec(command='go version')}}"

I'd hope that EXAMPLE would get populated with the output of go version.

Instead I see the following error:

bash: go: command not found
mise failed to parse template: '{{exec(command='go version')}}'
mise Failed to render '__tera_one_off'
mise Function call 'exec' failed
mise Io error while writing rendered value to output: Other
mise command ["bash", "-c", "go version"] exited with code 127
mise Run with --verbose or MISE_VERBOSE=1 for more information

I tried also using mise exec to execute the command but that didn't work either.

@jdx
Copy link
Owner

jdx commented Apr 28, 2024

the trouble is people also need some env vars to be loaded before tools. I wonder if maybe we could use [env.post] as a way to mark env vars that should be processed after tools are loaded or something.

@jdx
Copy link
Owner

jdx commented Apr 28, 2024

another idea:

[env]
EXAMPLE = {value="{{exec(command='go version')}}", when="after_tools"}

@abatilo
Copy link
Contributor Author

abatilo commented Apr 28, 2024

Oh, that's an interesting idea. I hadn't considered the idea of putting hooks in with the env execution also but in hindsight it makes a lot of sense!

@richdawe-cio
Copy link

richdawe-cio commented Jun 3, 2024

An example of where this would be helpful at my work:

We're using mise to manage our local development environments. We're using FoundationDB with some go bindings that are use cgo to wrap the FoundationDB client libraries. We have foundationdb in the tools config section, and we're defining CGO_CPPFLAGS and other CGO_xxx environment variables in the env config section. These are all dependent on the install path for foundationdb, e.g.: ~/.local/share/mise/installs/foundationdb/x.y.z.

Right now we're using something like this:

[env]
# Needed for FoundationDB.
FDB_LOCATION = "{{exec(command='mise where foundationdb 2>/dev/null || true')}}"
CGO_CPPFLAGS = '-I{{ env.FDB_LOCATION }}/usr/local/include'

But it would be better if we could use something like

FDB_LOCATION = "{{tools(name='foundationdb').install_path}}"

or something like that.

Side note: Use of "{{exec(command='mise where ...')}}" seems to cause a fork-bomb with 2024.6.0 (mise calling mise calling mise etc.), which didn't happen with 2024.5.2.

@fxsalazar
Copy link
Contributor

So the doc exemple:

[alias.node]
current = "{{exec(command='node --version')}}"

is a bit misleading in the sense that node must not be a mise installed version. I tried it and my terminal just freezes.

Should there exists a note in the docs?

@erickguan
Copy link
Contributor

I like this feature.

Mise supports env._.source (#1447) and env._.run (#1448), so this improvement should consider them.

I prefer [env.post] for structure. Roughly:

[env]
SETUP_A_VARIABLE = 1
_.source = "./script-to-import-env.sh"
# or run a program to import env
_.run = "./import-env"

[env.post]
# Tools and environment variables are available here
ADDITIONAL_VARIABLE = "30"
ANOTHER_VARIABLE = "{{exec(command='node --version')}}"

# Import additional environment variables or execute commands
_.source = "./run-script-with-more-env.sh"
# or run a program
_.run = "./run-program"

@sincraianul
Copy link

sincraianul commented Nov 8, 2024

I think this could be achieved by just using _.source and giving us the option to mise x without loading the [env] block in the current directory, so just calling the binary as-is. This will prevent the infinite recursion that's happening now when trying to source a script that invokes a tool managed by mise.

IMO this is a pretty common use case where people use something like Doppler to manage their secrets and would like to implement direnv-like behavior with mise, while also managing the Doppler CLI version.

Another approach could be to more clearly separate toolchain environments from "directory" environments. Maybe keep [env] for the latter and introduce something like [tools._env] for the former 🤷🏻‍♂️, though this would be sort of a breaking change.

@erickguan
Copy link
Contributor

IMO this is a pretty common use case where people use something like Doppler to manage their secrets and would like to implement direnv-like behavior with mise, while also managing the Doppler CLI version.

I don't understand it. Perhaps because I don't know Doppler. Can you explain a bit more? Or some samples of how you do it know and what you think how mise should manage Doppler.
FYI, mise can replace the direnv.

@sincraianul
Copy link

IMO this is a pretty common use case where people use something like Doppler to manage their secrets and would like to implement direnv-like behavior with mise, while also managing the Doppler CLI version.

I don't understand it. Perhaps because I don't know Doppler. Can you explain a bit more? Or some samples of how you do it know and what you think how mise should manage Doppler. FYI, mise can replace the direnv.

For example, when using Doppler with direnv one would create an .envrc file that looks something like this:

export $(doppler secrets download --no-file --format env-no-quotes)

This is not possible with mise if we're also using mise to install doppler.

Hope this clears things up!

@erickguan
Copy link
Contributor

Thanks for the example. I understand using mise like direnv.
mise is certainly going with a direction to have _.source and _.run for many reasons.

@sandstrom sandstrom mentioned this issue Nov 28, 2024
@jdx jdx linked a pull request Dec 16, 2024 that will close this issue
@jdx jdx closed this as completed in #3598 Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants