This repository was archived by the owner on Mar 13, 2026. It is now read-only.
Commit 7c5dba3
authored
Remove opam's pre-command shell hook (#14)
The `opam init` command adds some code to the user's shell config file
to set up the user's environment to be able to run programs installed by
opam. Firstly the user's PATH variable is prefixed with the bin
directory from the default opam switch. Additionally, opam installs a
pre-command hook into the shell so that before every command is run, the
user's PATH variable is _again_ prefixed with the path to the bin
directory from the current opam switch. This poses a problem for the
dune binary distro, because if the user's current opam switch contains
dune (which is very likely given dune is a dependency of the majority of
opam packages), the instance of dune from the opam switch will take
precedence over the dune instance from the binary distro. As long as
opam's pre-command shell hook is present, opam's bin directory will
always be at the front of PATH when commands are run regardless of any
additional PATH manipulation done by the install script in the user's
shell config, as the former is run before _every_ command, while the
latter is only run once when the shell starts.
This commit changes the install script to add logic to the user's shell
config file to uninstall opam's pre-command shell hook. Modifications to
the PATH performed by opam's setup will still take place when the shell
starts, so programs in the default opam switch will still be runnable
from the user's environment. Users will still also be able to change
their opam environment by running `eval $(opam env)` which will prefix
PATH with the current opam switch's bin directory. The only difference
is that opam's pre-command shell hook will no longer run before each
command is evaluated.
In order for the pre-command hook to be uninstalled, dune's setup must
take place after opam's setup in the user's shell config. For zsh and
fish this works because dune's setup is appended to the same file to
which opam adds its setup logic, so dune's setup is guaranteed to appear
after opam's setup. For bash it's more complicated as `opam init` adds
its setup to ~/.profile by default, but it's possible that users will
move this logic manually to other config files such as ~/.bashrc. This
means that appending dune's logic to the end of (say) ~/.bashrc doesn't
guarantee that it will run after opam's setup, as a user may have
configured their ~/.profile to source their ~/.bashrc and then run the
opam setup. The simplest way to ensure that dune is always initialized
after opam is to detect which bash config file contains opam's setup
logic, and append dune's setup logic to the same file. The install
script is modified to this effect, and an additional prompt is added so
users can override the choice of which config file to modify.
Signed-off-by: Stephen Sherratt <stephen@sherra.tt>1 parent ea14f57 commit 7c5dba3
File tree
5 files changed
+353
-23
lines changed5 files changed
+353
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
330 | 339 | | |
331 | 340 | | |
| 341 | + | |
332 | 342 | | |
333 | 343 | | |
| 344 | + | |
334 | 345 | | |
335 | | - | |
| 346 | + | |
336 | 347 | | |
337 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
338 | 375 | | |
339 | | - | |
| 376 | + | |
340 | 377 | | |
341 | 378 | | |
342 | 379 | | |
343 | | - | |
| 380 | + | |
| 381 | + | |
344 | 382 | | |
345 | 383 | | |
346 | 384 | | |
347 | | - | |
| 385 | + | |
| 386 | + | |
348 | 387 | | |
349 | 388 | | |
350 | 389 | | |
| |||
357 | 396 | | |
358 | 397 | | |
359 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
360 | 441 | | |
361 | 442 | | |
362 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
363 | 450 | | |
364 | 451 | | |
| 452 | + | |
| 453 | + | |
365 | 454 | | |
366 | 455 | | |
367 | 456 | | |
| |||
372 | 461 | | |
373 | 462 | | |
374 | 463 | | |
375 | | - | |
376 | 464 | | |
377 | 465 | | |
378 | 466 | | |
| |||
382 | 470 | | |
383 | 471 | | |
384 | 472 | | |
385 | | - | |
386 | 473 | | |
387 | 474 | | |
388 | 475 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | | - | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
25 | | - | |
| |||
0 commit comments