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

Allow manual reload of nix environment. #359

Merged
merged 4 commits into from
Jul 4, 2023

Conversation

pelme
Copy link
Contributor

@pelme pelme commented Jun 15, 2023

Adding nix_direnv_manual_reload to .envrc will avoid the automatic update of the nix environment on changes and allow the user to issue a command to update the nix environment explicitly.

Sometimes it takes a long time to rebuild the nix environment and it happens in times. Switching branches in or rebasing can be painful since nix will re-evaluate between nix file changes.

This blog post explains the problems with blocking the shell: https://dev.to/allenap/some-direnv-best-practices-actually-just-one-4864

Example usage:

$ cat .envrc
source nix-direnv/direnvrc

nix_direnv_manual_reload
use_flake

$ touch flake.nix
direnv: loading ~/tst/.envrc
direnv: nix-direnv: cache is out of date. use "nix-direnv-reload" to reload.
direnv: export +AR +AS <... snip ...>

 $ nix-direnv-reload
direnv: loading ~/tst/.envrc
direnv: nix-direnv: renewed cache

What do you think about this?

@pelme
Copy link
Contributor Author

pelme commented Jun 15, 2023

As for improved UX, what about creating a script at .direnv/bin/nix-direnv-refresh and using PATH_add .direnv/bin? The script would contain:

#!/usr/bin/env bash
nix_direnv_refresh=t direnv exec $direnvpath true

To refresh the cache, the command would then be:

$ nix-direnv-refresh

@pelme pelme changed the title Allow manual refresh of nix environment. Allow manual reload of nix environment. Jun 16, 2023
@pelme
Copy link
Contributor Author

pelme commented Jun 16, 2023

As for improved UX, what about creating a script at .direnv/bin/nix-direnv-refresh and using PATH_add .direnv/bin? The script would contain:

#!/usr/bin/env bash
nix_direnv_refresh=t direnv exec $direnvpath true

To refresh the cache, the command would then be:

$ nix-direnv-refresh

I have implemented this idea and updated the PR.

@phaer
Copy link
Member

phaer commented Jun 16, 2023

I have implemented this idea and updated the PR.

I like the idea, but I think nix-direnv-refresh could be a simple shell function instead of a script on disk, as it's only ever needed in a shell session where your init code already ran?

@Mic92
Copy link
Member

Mic92 commented Jun 16, 2023

I have implemented this idea and updated the PR.

I like the idea, but I think nix-direnv-refresh could be a simple shell function instead of a script on disk, as it's only ever needed in a shell session where your init code already ran?

You cannot have shell functions because direnv only exports environment variables back into the shell. This is because direnv wants to support non-posix shells like fish.

@Mic92
Copy link
Member

Mic92 commented Jun 16, 2023

This fixes #292

Copy link
Contributor

@bbenne10 bbenne10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the changes I've asked for, I'd be happy to merge this.

direnvrc Outdated Show resolved Hide resolved
direnvrc Outdated Show resolved Hide resolved
@bbenne10
Copy link
Contributor

Note that you'll also need to address the shellcheck failures before merge.

direnvrc Outdated Show resolved Hide resolved
direnvrc Outdated Show resolved Hide resolved
@pelme
Copy link
Contributor Author

pelme commented Jun 16, 2023

Thanks a lot for your feedback and I am glad you find it useful. Bash scripting is not my primary cup of tea and I appreciate the improvements you have suggested. Will go through all comments and update the PR! Will also make sure use_nix works too, I did only focus on use_flake to start of with.

@pelme
Copy link
Contributor Author

pelme commented Jun 16, 2023

I have updated the PR with these changes:

  • Always create the nix-direnv-reload script
  • Use 0/1 for booleans
  • Avoid crashing when profile_rc does not exist the first time
  • Use better internal variable name for manual mode
  • Make use_nix work the same as use_flakes
  • Avoid unneccessary forks where possible
  • Fix shellcheck failures
  • Update mtime on profile rc files to avoid confusion about if the files are up to date
  • Add instructions to README.md

@pelme pelme force-pushed the manual-refresh branch 2 times, most recently from 052c662 to b772628 Compare June 16, 2023 21:29
@pelme pelme requested review from Mic92 and bbenne10 June 16, 2023 21:29
Copy link
Contributor

@bbenne10 bbenne10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm. Since @Mic92 also requested changes, I'll wait till he approves as well to merge.

@Mic92
Copy link
Member

Mic92 commented Jul 4, 2023

@mergify queue

@mergify
Copy link
Contributor

mergify bot commented Jul 4, 2023

queue

🛑 The pull request has been removed from the queue default

Pull request #359 has been dequeued due to failing checks or checks timeout.

You can take a look at Queue: Embarked in merge train check runs for more details.

In case of a failure due to a flaky test, you should first retrigger the CI.
Then, re-embark the pull request into the merge queue by posting the comment
@mergifyio refresh on the pull request.

@Mic92
Copy link
Member

Mic92 commented Jul 4, 2023

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Jul 4, 2023

refresh

✅ Pull request refreshed

@Mic92
Copy link
Member

Mic92 commented Jul 4, 2023

@Mergifyio rebase

pelme added 4 commits July 4, 2023 19:50
Adding nix_direnv_manual_refresh to .envrc will avoid the automatic
update of the nix environment on changes and allow the user to issue a
command to update the nix environment explicitly.

Sometimes it takes a long time to rebuild the nix environment and it
happens in times. Switching branches in or rebasing can be painful since
nix will re-evaluate between nix file changes.

This blog post explains the problems with blocking the shell:
https://dev.to/allenap/some-direnv-best-practices-actually-just-one-4864

This is a first prototype, the UX can certainly be improved a bit.
- Always create the nix-direnv-reload script
- Use 0/1 for booleans
- Avoid crashing when profile_rc does not exist
- Use better internal variable name for manual mode
- Make use_nix work
- Avoid unneccessary forks
- Fix shellcheck failures
- Update mtime on profile rc files to avoid confusion about if the files
  are up to date
- Add instructions to README.md
@mergify
Copy link
Contributor

mergify bot commented Jul 4, 2023

rebase

✅ Branch has been successfully rebased

@Mic92
Copy link
Member

Mic92 commented Jul 4, 2023

@mergify queue

@mergify
Copy link
Contributor

mergify bot commented Jul 4, 2023

queue

✅ The pull request has been merged automatically

The pull request has been merged automatically at 3f9e573

@mergify mergify bot merged commit 3f9e573 into nix-community:master Jul 4, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants