Page MenuHomePhabricator

[Nix] Add better prompt default for nix shell
ClosedPublic

Authored by jon on Aug 9 2022, 2:27 PM.
Tags
None
Referenced Files
F3530073: D4790.id15516.diff
Tue, Dec 24, 11:34 PM
F3529012: D4790.diff
Tue, Dec 24, 11:58 AM
Unknown Object (File)
Wed, Dec 18, 7:42 AM
Unknown Object (File)
Mon, Dec 16, 6:43 PM
Unknown Object (File)
Mon, Dec 16, 6:43 PM
Unknown Object (File)
Mon, Dec 16, 6:43 PM
Unknown Object (File)
Mon, Dec 16, 6:43 PM
Unknown Object (File)
Mon, Dec 16, 6:43 PM
Subscribers

Details

Summary

Provide better prompt default so that it doesn't look
hideous for users which don't have a configured ~/.bashrc

First run

Screen Shot 2022-08-10 at 10.50.46 AM.png (318×1 px, 76 KB)

After changing the font
Screen Shot 2022-08-10 at 10.54.58 AM.png (620×1 px, 157 KB)

After reverting back to my normal setup
Screen Shot 2022-08-10 at 11.00.00 AM.png (160×1 px, 46 KB)

Test Plan
# Ensure that ~/.bashrc won't affect logic
# Should only be necessary if personally configured
[[ -f ~/.bashrc ]] && mv ~/.bashrc ~/.bashrc.bak

# from any branch
nix develop 'github.com:commE2E/comm?ref=jonringer/better-prompt'

# Prompt should look decent

# restore old bashrc
[[ -f ~/.bashrc.bak ]] && mv ~/.bashrc{.bak,}

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes

Make executable, although not necessary

ashoat requested changes to this revision.Aug 10 2022, 6:48 AM
  1. I continue to dislike this pattern of putting bash inline in .nix files, especially here where it fully circumvents Shellcheck. Is there any way to check in this bash to the repo as a .sh file? Any way at all?
  2. Please provide a video or several screenshots showing what this prompt looks like in a variety of different contexts.
  3. What do you think of using Powerline? In my opinion it looks pretty cool...
This revision now requires changes to proceed.Aug 10 2022, 6:48 AM

I continue to dislike this pattern of putting bash inline in .nix files, especially here where it fully circumvents Shellcheck. Is there any way to check in this bash to the repo as a .sh file? Any way at all?

I agree with @ashoat here. Circumventing the ShellCheck CI is a problem since this diff will make it through 3 layers of CI (lint-staged, Buildkite, and GitHub Actions) without running a single ShellCheck pass over any of the code. That definitely can't be good.

To be fair, though, you explained that writeShellApplication runs ShellCheck on the code anyways. And I assume the checkPhase included in this diff is to also run ShellCheck. But it'd be nice if everyone could see the output of that ShellCheck run through something like Buildkite/Harbormaster logs, since it's accessible by all reviewers and is part of our CI.

Beyond this, reviewing a shell script (or really any code) stored as a string variable can be hard because it decreases readability. Is there no way to store the code in a separate file and import it into the .nix file?

I continue to dislike this pattern of putting bash inline in .nix files, especially here where it fully circumvents Shellcheck.

It's still running shellcheck as part of the package build on line 59, nix develop would fail if it wasn't compliant to shellcheck. I'll never be able to get completely rid of writing some bash, it's the glue which bridges the nix and environment worlds.

For longer scripts in the future, I should be able to write most of the logic in a separate script though.

Please provide a video or several screenshots showing what this prompt looks like in a variety of different contexts.

Rgr

What do you think of using Powerline? In my opinion it looks pretty cool...

This was easier to do than I thought, I'll just stick powerline in here

Beyond this, reviewing a shell script (or really any code) stored as a string variable can be hard because it decreases readability. Is there no way to store the code in a separate file and import it into the .nix file?

The main motivation to use bash embedded in nix is that I can interpolate nix paths directly into the scripts, and not have to worry about "handing off" a bunch of logic around where things are.

Would love a response to my question above:

Is there any way to check in this bash to the repo as a .sh file? Any way at all?

@jon, would love if you could rack your brain to come up with some potential ways to make this happen. Are you 1000% sure that there is absolutely no possible way to do this at all?

@jon, would love if you could rack your brain to come up with some potential ways to make this happen. Are you 1000% sure that there is absolutely no possible way to do this at all?

There is, I'll update this diff with one workflow. But it will essentially be nix doing a bunch of <pkg_root>=${pkg} and then calling a script which assumes that pkg_root is defined.

Use powerline, refactor most of the logic into a separate script

Can you update your upstream branch so that the test plan works with the latest revision?

Yea, was getting the mac logged into phabricator so I could upload the screen shots

Avoid better-prompt being executable, as it should only be sourced

abosh requested changes to this revision.EditedAug 10 2022, 11:49 AM

Ran ShellCheck manually on the Bash code that was still stored in a string variable (like in better-prompt.nix and dev-shell.nix), and they had errors...I really think all shell code should be put into a .sh file so that at least you can have lint-staged or Buildkite lint them before reviewers have to take a look at it. I know it involves extra work on your end but it's more modular, cleaner to read, easier to review, and passes through our CI.

Haven't looked at anything else yet, but just put up the double quote errors that ShellCheck reported.

nix/better-prompt.nix
23 ↗(On Diff #15517)
27–29 ↗(On Diff #15517)
nix/dev-shell.nix
110 ↗(On Diff #15517)
This revision now requires changes to proceed.Aug 10 2022, 11:49 AM

Also it seems your files are still "Restricted", because I can't see them even though they're in the summary.

image.png (314×566 px, 27 KB)

I think to fix this, you just have to click on the files on the right sidebar:
image.png (1×650 px, 121 KB)

and allow permissions or something of that sort. Then your files will be public so reviewers can see what you're referencing. Normally, referenced files should be public by default if they're included in the diff summary, but maybe because they were added later their permissions are different.

ashoat requested changes to this revision.Aug 10 2022, 12:07 PM

To make the files visible to everybody, you'll need to "attach" them. @abosh has written some advice on how to do this before... @abosh, would you mind putting that info into a doc on Notion and then sharing it with @jon?

nix/start-powerline.sh
53–61 ↗(On Diff #15517)
  1. I'm guessing not, but is there a way to check the current font? Figured we could fall back to some other default if the Powerline font isn't selected.
  2. How bad do things look if the user fails to select a Powerline font? Can you share a screenshot?

To make the files visible to everybody, you'll need to "attach" them. @abosh has written some advice on how to do this before... @abosh, would you mind putting that info into a doc on Notion and then sharing it with @jon?

Done! Wasn't sure where to add it, so just put it under Unsorted in Dev Notes.

Ran ShellCheck manually on the Bash code that was still stored in a string variable (like in better-prompt.nix and dev-shell.nix), and they had errors...I really think all shell code should be put into a .sh file so that at least you can have lint-staged or Buildkite lint them before reviewers have to take a look at it. I know it involves extra work on your end but it's more modular, cleaner to read, easier to review, and passes through our CI.

@abosh I moved as much as I could into a separate .sh file. The remaining bash is largely just setting some variables when passing the logic onto the other file.

Haven't looked at anything else yet, but just put up the double quote errors that ShellCheck reported.

I think you're runnning it on the multiple line string which eventually gets turned into a shell script. The "built" shell script has the following contents:

[12:34:50] jon@jon-desktop /home/jon/comm/comm (jonringer/better-prompt)
$ nix build .#better-prompt
[12:35:02] jon@jon-desktop /home/jon/comm/comm (jonringer/better-prompt)
$ cat ./result/bin/better-prompt
#!/nix/store/qm2lv1gpbyn0rsfai40cbvj3h4gz69yc-bash-5.1-p16/bin/bash

PATH=/nix/store/2gfhqcjha69y0gpjryrxp0y55sn4jnx6-python3.10-powerline-2.8.2/bin:$PATH
powerline_fonts=/nix/store/g46c2x9wy5xbnwmr1x3rarp6hgnszb94-powerline-fonts-2018-11-11
powerline_root=/nix/store/2gfhqcjha69y0gpjryrxp0y55sn4jnx6-python3.10-powerline-2.8.2

source /nix/store/9bj8v4c0ji8hcrl97r28khms0m9669fa-start-powerline.sh

Which is why shellcheck is happy when ran as part of the "better-prompt" package build

nix/start-powerline.sh
53–61 ↗(On Diff #15517)

I'm guessing not, but is there a way to check the current font?

Everything I found used the GUI, if someone else can find another way, I would be happy to make this go away.

But the assumption here is, "If I had to add the powerline fonts for the user, then they likely didn't have the powerline fonts to begin with".

Figured we could fall back to some other default if the Powerline font isn't selected.

That's what the first photo shows, it just looks really weird as the glyphs help separate the components without adding noise.

How bad do things look if the user fails to select a Powerline font? Can you share a screenshot?

First one

jon marked 3 inline comments as done.

Use double quotes when possible

nix/start-powerline.sh
58–60 ↗(On Diff #15521)

Should we change these instructions to this instead?

jon added inline comments.
nix/start-powerline.sh
58–60 ↗(On Diff #15521)

I tried this on my iTerm2 using Space Mono, and it wasn't able to find the "branch" glyph or the left pointing triangle.

I think a Powerline font would be more appropriate

jon marked an inline comment as done.

Grammar

I think it should be good after this!

nix/dev-shell.nix
110 ↗(On Diff #15523)

Was there a reason this couldn't be double quoted?

nix/start-powerline.sh
7 ↗(On Diff #15523)

It works here, but isn't return usually used in functions? Should this be exit 0 instead if there's nothing we have left to do if the user already has an opinionated PS1?

8 ↗(On Diff #15523)

Do we need this else here if we're returning 0 in the if branch?

21 ↗(On Diff #15523)
31 ↗(On Diff #15523)

Why do we use test here? Couldn't this just be a string equality check? I know you're adapting this script from the GitHub link you wrote in the comment (thanks for that), and it seems the line was changed from a normal equality check to a test command here. I'm sure there's a reason, but I don't understand why it can't just be a normal string equality check (as shown in the suggested edit for clarity).

If there is no difference, I'd opt for the == operator since its more readable and what we use in other places in the codebase.

47–49 ↗(On Diff #15523)

I know find is super powerful, but sometimes reading a find command gives me a headache 😂

51 ↗(On Diff #15523)

This command is repeated twice in the document, here and on line 31. Maybe it'd be more readable if you created a boolean to determine if the user is on MacOS with the result of this command in it? Then you could just use that boolean variable in both these places instead of repeating the command. It's not necessary, but more readable.

55 ↗(On Diff #15523)

Up to you, but I think this sounds better than before because saying please do: implies that there will be an action verb right after, like "Select" or "Navigate to".

59 ↗(On Diff #15523)
This revision now requires changes to proceed.Aug 11 2022, 10:30 AM
jon marked 9 inline comments as done.

Address comments

nix/dev-shell.nix
110 ↗(On Diff #15523)

I thought I did, guess I passed over this file

nix/start-powerline.sh
7 ↗(On Diff #15523)

no, as noted in https://phab.comm.dev/D4659, when sourcing a file there's no child shell process being spawned, so using exit would cause the current shell to exit

8 ↗(On Diff #15523)

Guess not. Think I had some other logic before, then refactored it.

21 ↗(On Diff #15523)

looks like run is the correct past participle

31 ↗(On Diff #15523)

because that's what was in the original https://github.com/powerline/fonts/blob/master/install.sh

You could argue this is even more portable than using the extended [[ syntax

47–49 ↗(On Diff #15523)

it was in the original https://github.com/powerline/fonts/blob/master/install.sh and that has had the benefit of years of potential improvement.

All i did was chunk it up to fit within 80 character limits

51 ↗(On Diff #15523)

Yea, but in bash with set -o errexit, you still need to do something like

if test "$(uname)" = "Darwin"; then
  isDarwin=1
else
  isDarwin=
fi

then refer to it with a comparison anyway

if [[ -n "$isDarwin" ]]; then

Idk, bash doesn't really have a true boolean value, we just abuse strings in a convention which reflects a boolean-like paradigm

Thanks for addressing my feedback! Everything you said makes sense, so accepting.

This revision is now accepted and ready to land.Aug 11 2022, 12:57 PM