DEV Community


What the heck is XDG_DATA_HOME?

captainsafia profile image Safia Abdalla ・2 min read

Over the past day, I've been working on creating a new @nteract/fs-kernels package within the nteract monorepo. Implementation specific details aside, this new package consolidates the functionality of multiple packages into one and adds a neat little API layer on top of all of them.

To get this working, I had to bring in the source code from the jupyter-paths.

// TODO: respect XDG_DATA_HOME
return home(".local/share/jupyter");

Um. What the heck is XDG_DATA_HOME and why do I have to respect it? I did what every developer might do in the case: I did a quick Google search. After looking through a couple of the results on the first page, I came to a conclusion on what this to-do comment was about.

XDG_DATA_HOME is a directory used to store user-specific files on some Unix operating systems.

Before diving into this, I should show you the implementation for the home function invoked above.

function home(subDir?: string) {
  const baseDir = homedir();
  return subDir ? path.join(baseDir, subDir) : baseDir;

The function essentially concatenates a directory path to the home directory of the user. The homedir function that is invoked in the function above is the homedir function defined in the os module in Node.

So as it turns out, XDG_DATA_HOME defaults to $HOME/.local/share, which is what the code snippet above returns. So the fix for the to-do above ended up being pretty simple, I updated the code like so.

return process.env.XDG_DATA_HOME || home(".local/share/jupyter");

Woot! Time to stage and commit.

Anyway, I thought it would be interesting to write a blog post that was just showcasing some of the daily things I run into while coding. There were arguably more interesting things that I worked on today but by the time I had this idea, I had already worked on the more interesting things.

See you in the next blog post!

Discussion (0)

Editor guide