Are the hooks working with flox 1.0.1?

I don’t know if I am doing anything wrong but the [hook] section seems to have 2 issues:

  1. The file attribute is not supported
ERROR: unrecognized manifest field 'hook.file'.
  1. When I try to put a simple echo hello inside the script field, I don’t anything printed on screen when my environment is activated

Thanks.

the hook.file isn’t yet in the software. We had it at one point but removed it so we could polish it up. Did you find it referenced somewhere we missed?

that’s surprising that the hook.script with an echo isn’t working. can you share the manifest?

The hook.file field is mentioned in one manifest comment, that’s why I tried it out:

#
# This is a flox environment manifest.
# Visit flox.dev/docs/concepts/manifest/
# or see flox-edit(1) for more information
#

# List packages you wish to install in your environment inside
# the 'install' section

[install]
# hello.pkg-path = "hello"
# nodejs = { version = "^18.4.2", pkg-path = "nodejs_18" }

# Set an environment variable.
# These variables may not reference once another
# and are added to the environment without expansion.

[vars]
# message = "Howdy"
# pass-in = "$some-env-var"

# An activation hook will be run when entering the environment.
# You can define one in the 'hook' table inline via the 'script' field,
# or provide a file using the 'file' field.
# If 'file' is provided, the path should be relative to this file.
# If both 'file' and 'script' are provided,
# only the hook defined in 'file' will be run .

[hook]
script = """
  echo hello
"""

# An environment that works on one system is guaranteed to work on the
# same type of system,
# but other systems may not have the same packages available, etc.
# In order to use the environment on a system you must explicitly
# add it to this list.
[options]
systems = ["aarch64-darwin"]

As you can see this is a bare bones environment. When I activate it, I don’t see “hello”.

Wow big miss on our behalf. fix: file option in manifest by ghudgins · Pull Request #1182 · flox/flox · GitHub

We will work to add file option in the future

On the echo hello I’m seeing that working so that’s very strange. I’m perplexed, will see if others have ideas as to what’s happening here

1 Like

I can’t reproduce hook.script not working, so maybe there’s something else going on?

> flox --version
Version: 1.0.1

> flox list -c
[hook]
script = """
  echo hello
"""

[options]
systems = ["aarch64-darwin"]

> flox activate
✅ You are now using the environment tmp at /private/tmp.
To stop using this environment, type 'exit'

hello

There is certainly something else going on. The question is what :-)? Whatever I do in the hook section is not executed, for example writing to a file or even trying to exit.

What can I do to diagnose the problem? (I tried flox activate -v but I don’t get any more information)

This looks like a shell activation issue. If I run eval "$(flox activate)" then I see “hello”, but if I just write flox activate nothing is actually activated (the binaries are not even on the PATH).

Can you do a flox list -c and then a flox -vvv activate?

flox list -c returns

#
# This is a flox environment manifest.
# Visit flox.dev/docs/concepts/manifest/
# or see flox-edit(1) for more information
#

# List packages you wish to install in your environment inside
# the 'install' section

[install]
coursier.pkg-path = "coursier"
jdk21.pkg-path = "jdk21"
maven.pkg-path = "maven"
pandoc.pkg-path = "pandoc"
nodejs.pkg-path = "nodejs"
sbt.pkg-path = "sbt"
visualvm.pkg-path = "visualvm"
scalafmt.pkg-path = "scalafmt"
# hello.pkg-path = "hello"
# nodejs = { version = "^18.4.2", path = "nodejs_18" }

# Set an environment variable.
# These variables may not reference once another
# and are added to the environment without expansion.

[vars]
# message = "Howdy"
# pass-in = "$some-env-var"

# An activation hook will be run when entering the environment.
# You can define one in the 'hook' table inline via the 'script' field,
# or provide a file using the 'file' field.
# If 'file' is provided, the path should be relative to this file.
# If both 'file' and 'script' are provided,
# only the hook defined in 'file' will be run .

[hook]
script = """
  echo hello
"""

# An environment that works on one system is guaranteed to work on the
# same type of system,
# but other systems may not have the same packages available, etc.
# In order to use the environment on a system you must explicitly
# add it to this list.
[options]
systems = ["aarch64-darwin"]

and flox -vvv activate returns:

2024-03-19T15:43:50.478230Z DEBUG flox::utils::init::sentry: Initializing Sentry with DSN: https://85c9526795f5047c99a5247aab616295:@o4506548203094016.ingest.us.sentry.io/4506548241825792
2024-03-19T15:43:50.480172Z DEBUG flox: set _FLOX_PKGDB_VERBOSITY=3
2024-03-19T15:43:50.480634Z DEBUG flox::config: `$FLOX_CONFIG_DIR` not set, using "/Users/etorreborre/.config/flox/"
2024-03-19T15:43:50.481405Z DEBUG flox::utils::init::metrics: Attempting to read own UUID from file
2024-03-19T15:43:50.481456Z TRACE flox_command: subcommand="activate"
2024-03-19T15:43:50.481816Z DEBUG flox::utils::metrics: pushing entry to metrics buffer: MetricEntry { subcommand: Some("activate"), extras: {}, timestamp: 2024-03-19 15:43:50.481591 +00:00:00, flox_version: "1.0.1", os_family: Some("Mac OS"), os_family_release: Some("23.2.0"), os: None, os_version: None, empty_flags: [] }
2024-03-19T15:43:50.482469Z DEBUG flox_rust_sdk::models::environment: looking for .flox: starting_path=/Users/etorreborre/.local/state/flox/scala/.flox
2024-03-19T15:43:50.482517Z DEBUG flox_rust_sdk::models::environment: .flox found: path=/Users/etorreborre/.local/state/flox/scala/.flox
2024-03-19T15:43:50.482523Z DEBUG flox::commands: detected concrete environment type: managed
2024-03-19T15:43:50.482536Z DEBUG flox_rust_sdk::models::floxmetav2: using configured FloxHub token
2024-03-19T15:43:50.482542Z DEBUG flox_rust_sdk::providers::git: attempting to open repo: path=/Users/etorreborre/.local/share/flox/meta/etorreborre
2024-03-19T15:43:50.482548Z DEBUG flox_rust_sdk::providers::git: running git command: /nix/store/p9i7qfwxgchxbvxprcdbjqiqx73kqz39-git-minimal-2.42.0/bin/git -C /Users/etorreborre/.local/share/flox/meta/etorreborre rev-parse --is-bare-repository
2024-03-19T15:43:50.487137Z DEBUG flox_rust_sdk::providers::git: determining path to git repo
2024-03-19T15:43:50.487155Z DEBUG flox_rust_sdk::providers::git: running git command: /nix/store/p9i7qfwxgchxbvxprcdbjqiqx73kqz39-git-minimal-2.42.0/bin/git -c 'credential.https://api.flox.dev/git.helper='\!'f(){ echo "username=oauth"; echo "password=$FLOX_FLOXHUB_TOKEN"; }; f' -c 'remote.dynamicorigin.url=https://api.flox.dev/git/etorreborre/floxmeta' -c 'user.email=floxuser@example.invalid' -c 'user.name=Flox User' -C /Users/etorreborre/.local/share/flox/meta/etorreborre rev-parse --absolute-git-dir
2024-03-19T15:43:50.491285Z DEBUG flox_rust_sdk::providers::git: got non-canonical path: path=/Users/etorreborre/.local/share/flox/meta/etorreborre
2024-03-19T15:43:50.491305Z DEBUG flox_rust_sdk::providers::git: canonicalized path: path=/Users/etorreborre/.local/share/flox/meta/etorreborre
2024-03-19T15:43:50.491317Z DEBUG flox_rust_sdk::providers::git: running git command: /nix/store/p9i7qfwxgchxbvxprcdbjqiqx73kqz39-git-minimal-2.42.0/bin/git -c 'credential.https://api.flox.dev/git.helper='\!'f(){ echo "username=oauth"; echo "password=$FLOX_FLOXHUB_TOKEN"; }; f' -c 'remote.dynamicorigin.url=https://api.flox.dev/git/etorreborre/floxmeta' -c 'user.email=floxuser@example.invalid' -c 'user.name=Flox User' -C /Users/etorreborre/.local/share/flox/meta/etorreborre show-ref --hash refs/heads/scala
2024-03-19T15:43:50.496781Z DEBUG flox_rust_sdk::providers::git: running git command: /nix/store/p9i7qfwxgchxbvxprcdbjqiqx73kqz39-git-minimal-2.42.0/bin/git -c 'credential.https://api.flox.dev/git.helper='\!'f(){ echo "username=oauth"; echo "password=$FLOX_FLOXHUB_TOKEN"; }; f' -c 'remote.dynamicorigin.url=https://api.flox.dev/git/etorreborre/floxmeta' -c 'user.email=floxuser@example.invalid' -c 'user.name=Flox User' -C /Users/etorreborre/.local/share/flox/meta/etorreborre rev-parse --quiet --verify '5657f44ffedd1277d48bfc8c7573c41e4c30fd22^{commit}'
2024-03-19T15:43:50.501390Z DEBUG flox_rust_sdk::providers::git: running git command: /nix/store/p9i7qfwxgchxbvxprcdbjqiqx73kqz39-git-minimal-2.42.0/bin/git -c 'credential.https://api.flox.dev/git.helper='\!'f(){ echo "username=oauth"; echo "password=$FLOX_FLOXHUB_TOKEN"; }; f' -c 'remote.dynamicorigin.url=https://api.flox.dev/git/etorreborre/floxmeta' -c 'user.email=floxuser@example.invalid' -c 'user.name=Flox User' -C /Users/etorreborre/.local/share/flox/meta/etorreborre show-ref --hash refs/heads/scala.db28f4563673748ecdb4ceda3dabbf1494a3f1ee5878e807fe4946e78aeb490a
⠁ Getting ready to use environment etorreborre/scala at /Users/etorreborre/.local/state/flox/scala...                                                                                                                                                                                                                                                                                                                                       2024-03-19T15:43:50.505613Z DEBUG flox_rust_sdk::models::environment::managed_environment: pointer_lock_modified_at: SystemTime { tv_sec: 1710863017, tv_nsec: 266770130 }
            out_link_modified_at: SystemTime { tv_sec: 1710863017, tv_nsec: 359741770 }
2024-03-19T15:43:50.505782Z DEBUG flox::commands::environment: running activation command: FLOX_ENV="/Users/etorreborre/.cache/flox/run/etorreborre/scala.db28f4563673748ecdb4ceda3dabbf1494a3f1ee5878e807fe4946e78aeb490a" FLOX_ENV_CACHE="/Users/etorreborre/.local/state/flox/scala/.flox/cache" FLOX_ENV_DIRS="/Users/etorreborre/.cache/flox/run/etorreborre/scala.db28f4563673748ecdb4ceda3dabbf1494a3f1ee5878e807fe4946e78aeb490a:/Users/etorreborre/.cache/flox/run/etorreborre/default.d62f59a10e91ce82ea934079c3dc5f362bd375b611bf25f216b3b8bf232db084:" FLOX_ENV_LIB_DIRS="/Users/etorreborre/.cache/flox/run/etorreborre/scala.db28f4563673748ecdb4ceda3dabbf1494a3f1ee5878e807fe4946e78aeb490a/lib:/Users/etorreborre/.cache/flox/run/etorreborre/default.d62f59a10e91ce82ea934079c3dc5f362bd375b611bf25f216b3b8bf232db084/lib:lib" FLOX_ENV_PROJECT="/Users/etorreborre/.local/state/flox/scala" FLOX_ORIG_ZDOTDIR="/Users/etorreborre/.config/zsh" FLOX_PATH_PATCHED="/Users/etorreborre/.cache/flox/run/etorreborre/default.d62f59a10e91ce82ea934079c3dc5f362bd375b611bf25f216b3b8bf232db084/bin:/Users/etorreborre/.cache/flox/run/etorreborre/default.d62f59a10e91ce82ea934079c3dc5f362bd375b611bf25f216b3b8bf232db084/sbin:/Users/etorreborre/Library/pnpm:target/debug:/Users/etorreborre/.nix-profile/bin:/Users/etorreborre/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/Users/etorreborre/.local/bin:/opt/homebrew/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Users/etorreborre/.cabal/bin:/Users/etorreborre/.ghcup/bin" FLOX_PROMPT_COLOR_1="99" FLOX_PROMPT_COLOR_2="141" FLOX_PROMPT_ENVIRONMENTS="etorreborre/scala etorreborre/default" FLOX_ZSH_INIT_SCRIPT="/Users/etorreborre/.cache/flox/run/etorreborre/scala.db28f4563673748ecdb4ceda3dabbf1494a3f1ee5878e807fe4946e78aeb490a/activate/zsh" NIX_COREFOUNDATION_RPATH="/nix/store/7aiqcq3qjyy3cx3c2qafcv1q559hgbbc-apple-framework-CoreFoundation-11.0.0/Library/Frameworks" NIX_SSL_CERT_FILE="/nix/store/d9qdiyphispfjnyj9lb5kwrz144as3dm-nss-cacert-3.95/etc/ssl/certs/ca-bundle.crt" PATH_LOCALE="/nix/store/3h3xsrfkadkm34f0316k18la5f1mchak-adv_cmds-119-locale/share/locale" ZDOTDIR="/nix/store/96jv5cxlfmhl63291sxm5hjw5m50lsq2-flox.zdotdir" _FLOX_ACTIVE_ENVIRONMENTS="[{\"type\":\"dot-flox\",\"path\":\"/Users/etorreborre/.local/state/flox/scala\",\"pointer\":{\"owner\":\"etorreborre\",\"name\":\"scala\",\"floxhub_url\":\"https://hub.flox.dev/\",\"version\":1}},{\"type\":\"dot-flox\",\"path\":\"/Users/etorreborre/.local/state/flox/default\",\"pointer\":{\"owner\":\"etorreborre\",\"name\":\"default\",\"floxhub_url\":\"https://hub.flox.dev/\",\"version\":1}}]" "/bin/zsh" "--no-globalrcs"
✅ You are now using the environment etorreborre/scala at /Users/etorreborre/.local/state/flox/scala.
To stop using this environment, type 'exit'

Since this seems to be about activation, here is what I have been doing lately to work around the issue when not using direnv: $SHELL -c "$(flox activate -r etorreborre/ockam) && $SHELL".
This runs the hook section and initializes a subshell properly.

Hmm nothing’s jumping out to me from those logs. I’m having trouble coming up with something unless you really want to do some deeper debugging.

You could run that activation command (everything from FLOX_ENV=… to “–no-globalrcs”) manually yourself just to confirm it’s not printing hello.

And then you could check if “$FLOX_ZSH_INIT_SCRIPT” has a line that sources “$FLOX_ENV/activate/hook.sh”, and then you could check if “$FLOX_ENV/activate/hook.sh” exists and contains echo hello.

1 Like

Thanks Matthew, I was able to finally debug it with your guidance.
Everything was there, the init script, the hook file, but I noticed something weird with the setting of ZDOTDIR. It turns out that I have a .zshrc that sets ZDOTDIR to a directory under version control. In that directory I have an init file which contained a line redefining ZDOTDIR one more time:

export ZDOTDIR="$HOME"/.config/zsh

Once I removed that line, everything worked ok.

Thanks!

2 Likes

Oh interesting. What’s the init file that redefines ZDOTDIR a second time? Curious to try reproduce.

What I have is probably overcomplicated but here we go:

  • ~/.zshrc which sources source $HOME/.config/zsh/.zshrc

In $HOME/.config/zsh/.zshrc I set the XDG variables and start my full zsh configuration:

export XDG_DATA_HOME=$HOME/.local/share
export XDG_CONFIG_HOME=$HOME/.config
export XDG_STATE_HOME=$HOME/.local/state
export XDG_CACHE_HOME=$HOME/.cache
export XDG_RUNTIME_DIR=/run/user/$UID

# start the zsh configuration in .dotfiles
export ZDOTDIR=$HOME/.dotfiles
exec zsh --no-globalrcs

($HOME/.dotfiles is versioned controlled)

$HOME/.dotfiles/.zshrc contains lots of initializations. In the pack, there was: export ZDOTDIR="$HOME"/.config/zsh. That line accidentally got there I guess.

I’d be happy if my mistake can help you in any way :slight_smile:.

I think I still have some issues with alias definitions when I put them in the hook.script section.
I will wait for the next release and its activation changes to retest and come back to you if I still have some issues.

1.0.2 is up! let us know how profile.common does or does not work with your custom aliases!

Thanks Graham,

The profile.common section triggers ok but my aliases still get wiped out on activation.
I tried to comment out various portions of my .zshrc file to understand what could be causing this but I couldn’t pinpoint the problem. I also tried to use on-activate but that didn’t work either.

Since my setup is a bit complicated, because I’m using both oh-my-zsh and scm-breeze, I wouldn’t be surprised if something in there would be wiping out aliases.

I will try to investigate more during the week-end but if you have any suggestion, please tell!

hmm, with a vanilla-ish setup an alias in the profile.* field should do the trick. could be an order of operations thing with your existing setup. not sure I have any specific advice except binary searching for the culprit (turn it all off, see it work, add things back one by one). :sweat_smile:

maybe someone else has a more elegant suggestion…

I think the exec zsh --no-globalrcs may be breaking our initializations, because we source your .zshrc but then expect to be able to source our initializations afterwards. Here’s an attempt at a more minimal reproducer. You can see the echo "it's gettin flox in here" runs before adding exec to the .zshrc

$ mkdir /tmp/zsh
$ mkdir /tmp/zsh2
$ echo "export ZDOTDIR=/tmp/zsh2" > /tmp/zsh/.zshrc
$ ZDOTDIR=/tmp/zsh flox activate
✅ You are now using the environment main at /Users/matthew/flox/main.
To stop using this environment, type 'exit'

it's gettin flox in here

$ echo "exec zsh --no-globalrcs" >> /tmp/zsh/.zshrc
$ ZDOTDIR=/tmp/zsh flox activate
✅ You are now using the environment main at /Users/matthew/flox/main.
To stop using this environment, type 'exit'

That’s a good idea.

I tried to replace exec zsh --no-globalrcs with source $ZDOTDIR/.zshrc but the aliases are still missing. Note that if I put an echo message, I still see it, so the profile.common section is working, but something is removing the aliases defined in the profile.common section.

Here are other results I’m getting:

  1. I open a new window and activate an environment with aliases
flox activate -d $FLOX/test

Then:

  • I see my echo message
  • The alias is available
  • My prompt settings present in $ZDOTDIR/.zshrc are missing
  1. I open an new window and navigate to a directory with a .envrc file containing eval "$(FLOX_SHELL=zsh flox activate -d $FLOX/test)

Then:

  • I see my echo message
  • The alias is not available
  • My prompt settings are present