Skip to content

Conversation

@MrDowntempo
Copy link

Be warned. This is my first pull request EVAR. I'm not much of a coder, and I don't write my own bash scripts. I've tested this and it seeeems to work as a clean install, but I doubt it'll handle existing installs elegantly, if at all. Also this script is huuuge so I could've easily overlooked another place that needs an edit. If I've not gone about this the correct way, lemme know I'll try my best to do better next time.

This change should check if XDG_CONFIG_HOME exists, and if it does, create an nb subdirectoy in there for your .nbrc file (by way of the _ME variable)

I find having .nbrc in my home directory icky and want to tuck it away where it belongs. If this works, I'll try making XDG_STATE_HOME the default parent of the default notebook directory next.

This doesn't handle anything fancy like the preference-ordered XDG_CONFIG_DIRS variable.

@zachcran
Copy link

Great addition, I would also like something like this. Welcome to the world of GitHub submissions! It is a learning process for all of us, although be warned that some people forget that and get quite mean sometimes. I'm not a maintainer, but I'll leave a review momentarily to get more activity on this PR.

Since this is your first PR and you seem to be unsure on etiquette, I'll mention a few things that hopefully help! Just disregard if you already know this:

  • Regarding your posted issue (A pull request to put .nbrc in XDG_CONFIG_HOME #379), it can take maintainers a while to get to a PR sometimes, if they are even still actively maintaining the repo. Afaik nb is still being actively maintained since there were PRs merged in the last few months, but this likely isn't their full time job so you'll probably just have to wait for them to get to it. Making extra issues to get their attention could annoy some maintainers. For future submissions, I've seen it be more effective to leave a comment on your PR pinging the maintainer(s) with an @ mention letting them know that your PR is ready for review. Making an issue describing the feature you want beforehand also helps!
  • I'd recommend doing a quick search through existing issues and PRs on the repo (both open and closed) to get up to speed on any related discussions or progress that has already happened. Mention these related items on your PR to help the maintainers out and link together common items. For example, from a quick search of "xdg", it is worth mentioning that your PR would at least partially satisfy [Feature request] Automatic alternative NBRC_PATH #277. When possible, also use closing words to help automatically close an issue that your PR addresses fully, like "closes A pull request to put .nbrc in XDG_CONFIG_HOME #379" (I'm not sure if this will work from a comment).

Copy link

@zachcran zachcran left a comment

Choose a reason for hiding this comment

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

I've left feedback on some issues I saw and some potential options to discuss.

nb Outdated
Comment on lines 443 to 444
# Check if XDG_CONFIG_HOME exists
if [[ -n "${XDG_CONFIG_HOME}" ]]; then

Choose a reason for hiding this comment

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

Currently, I think this would always use XDG_CONFIG_HOME/nb/.nbrc if XDG_CONFIG_HOME is defined. It should probably still default to using $HOME/.nbrc if the file already exists so it doesn't break existing installations and setups.

Copy link
Author

Choose a reason for hiding this comment

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

Ah, I think you're right. I hadn't considered that.

nb Outdated
if [[ -n "${XDG_CONFIG_HOME}" ]]; then
# If XDG_CONFIG_HOME exists put .nbrc config file in a subdirectory.
mkdir -p "${XDG_CONFIG_HOME}/${_ME%%.*}" &&
export NBRC_PATH="${NBRC_PATH:-"${XDG_CONFIG_HOME}/${_ME%%.*}/.${_ME}rc"}"

Choose a reason for hiding this comment

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

I have commonly seen the main config file for programs installed under $XDG_CONFIG_DIR/subdir named config instead of something specific to the program like .nbrc. It may be worth considering here.
i.e.

Suggested change
export NBRC_PATH="${NBRC_PATH:-"${XDG_CONFIG_HOME}/${_ME%%.*}/.${_ME}rc"}"
export NBRC_PATH="${NBRC_PATH:-"${XDG_CONFIG_HOME}/${_ME%%.*}/config"}"

Copy link
Author

Choose a reason for hiding this comment

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

True. But I think there's value in knowing that weather its in a notebook, or in XDG_CONFIG_HOME, that the file is (mostly) the same name. I did get rid of the . though.

nb Outdated
export NBRC_PATH="${NBRC_PATH:-"${XDG_CONFIG_HOME}/${_ME%%.*}/.${_ME}rc"}"
else
# Otherwise, use the default path
export NBRC_PATH="${NBRC_PATH:-"${HOME}/.${_ME}rc"}"

Choose a reason for hiding this comment

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

Another option for the default config location is $HOME/.config/nb/ (the default value recommended in the XDG spec for XDG_CONFIG_HOME). Any thoughts from the maintainers/other users?

Copy link
Author

Choose a reason for hiding this comment

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

I wasn't sure if it made sense to conform to the spec, if XDG_CONFIG_HOME wasn't explicitly set, especially since you can have independent notebooks in folders withe their own .nbrc and that wasn't the current default. But I would prefer if nb followed the spec by default (Hence the patch)

Choose a reason for hiding this comment

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

I wasn't aware that each notebook could have its own .nbrc file! I'm struggling to find where this mentioned in the user guide website (https://xwmx.github.io/nb/), but I think I see the behavior defined starting on L564. Do you know if this behavior is documented in the user guide website yet?

Regardless, as far as I can tell the default for NBRC_PATH does not affect sourcing notebook-specific .nbrc files. The notebook-specific settings are hard-coded to be named .nbrc and NB_NOTEBOOK_PATH seems to depend on NB_DIR but not NBRC_PATH. NBRC_PATH is sourced on L455 prior to $NB_NOTEBOOK_PATH/.nbrc on L567, so I think regardless of default location they will both be sourced.

Copy link
Owner

Choose a reason for hiding this comment

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

@zachcran It's currently undocumented. If I recall correctly, the original use case where this came up was about using a specific editor for a notebook, which made me think about it as being more for user-specific settings, and therefore something that shouldn't be checked into the notebook repo. However, this would have to be .gitignored manually, so it seemed complicated and I decided to figure it out later. I'm open to suggestions.

Copy link
Owner

@xwmx xwmx Jun 28, 2025

Choose a reason for hiding this comment

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

Also, that is correct that the notebook .nbrc should be unaffected by $NBRC_PATH. The settings in notebook .nbrc files should override any matching global settings regardless of where they are set.

Choose a reason for hiding this comment

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

(Sorry for possible duplicate notifications, GitHub was behaving a bit strangely there) Thanks for the explanation! Is the ability to have notebook-specific .nbrc files something that you would like documented if someone is willing to do it? I could make an issue about it and label it with something like "Help Wanted" for now if you would like (and maybe work on it in the future, no promises though).

therefore something that shouldn't be checked into the notebook repo. However, this would have to be .gitignored manually, so it seemed complicated and I decided to figure it out later. I'm open to suggestions.

Would excluding the .nbrc file during the git add call be sufficient? After some digging, I learned that you can use "exclude" pathspecs with git add --all to ignore certain files. This would just require changing git -C "${_notebook_path}" add --all on L3768 to git -C "${_notebook_path}" add --all ':!.nbrc'. We can create a separate issue to discuss this further if needed, since it is a bit off topic here.

@MrDowntempo
Copy link
Author

MrDowntempo commented Jun 13, 2025

Thanks for the pointers, and for reviewing/improving the code, @zachcran I really appreciate it! Hopefully @xwmx pops in too.

@xwmx
Copy link
Owner

xwmx commented Jun 24, 2025

Thanks for exploring this. I've thought about this a few times and haven't tackled it yet because it gets complicated. We should have thorough test coverage to make sure everything works. The $NB_DIR location also has to be considered. It seems like that would probably have to exist in $XDG_DATA_HOME which defaults to $HOME/.local/share.

@zachcran
Copy link

zachcran commented Jun 29, 2025

The $NB_DIR location also has to be considered. It seems like that would probably have to exist in $XDG_DATA_HOME which defaults to $HOME/.local/share.

Does $NB_DIR need to be considered for this PR, or are you saying "in general for full XDG compliance"? If the latter, we could move the discussion on that part to a separate issue. From my interpretation of the XDG spec, you are correct on the location; notes would fall under $XDG_DATA_HOME/nb (defaulting to $HOME/.local/share/nb). Currently, mine specifically live in $XDG_DATA_HOME/nb/notes to leave space for other data there.

Copy link

@zachcran zachcran left a comment

Choose a reason for hiding this comment

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

@MrDowntempo I took another look through this, and had some further suggestions as I am learning more about how the inner nb code works.

nb Outdated
# Check if XDG_CONFIG_HOME exists
if [[ -n "${XDG_CONFIG_HOME}" ]]; then
# If XDG_CONFIG_HOME exists put .nbrc config file in a subdirectory.
mkdir -p "${XDG_CONFIG_HOME}/${_ME%%.*}" &&

Choose a reason for hiding this comment

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

There is an _init_create_rc_file function defined here already. Should creation of the config directory be part of that function instead? I think it would just require one line before writing the default nbrc file, something like this:

mkdir -p "$(dirname ${NBRC_PATH})"

Co-authored-by: Zachery Crandall <[email protected]>
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.

3 participants