# Adding a base directories class/module to the standard library

**URL:** https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975
**Category:** Crystal Contrib
**Created:** [June 27, 2024, 5:00pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975 "2024-06-27T17:00:23Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Snacks](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@Snacks](https://forum.crystal-lang.org/u/Snacks)
#### Post date: [June 27, 2024, 5:00pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/1 "2024-06-27T17:00:23Z")

</div>

Hey folks,

During a recent discussion on Discord, I raised the idea of having an alternative to [directories-rs](https://github.com/dirs-dev/directories-rs) in Crystal. @straight-shoota suggested that we discuss it further here and possibly include the newly added module in the standard library.

Benefits:

- Consistent behavior across different operating systems. Configuration, cache, data, etc., would be accessed through a unified API without worrying about platform-specific nuances. This might benefit the compiler and Shards as well.
- Promotion of best practices, whereas a Crystal developer might typically opt for a lazy solution, such as storing this data alongside the binary (which is not idiomatic on Linux and other operating systems).
- It is relatively easy to implement, and almost no maintenance should be required.

Caveats:

- Assuming this is going to be added, we would need to decide which directories are to be included. The bare minimum might be what’s described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).

What are the thoughts?

---

<div class="post-metadata">

### Author: ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)
#### Post date: [June 28, 2024, 11:12am UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/2 "2024-06-28T11:12:50Z")

</div>

We can probably take a lot of inspiration from the [dirs-dev](https://github.com/dirs-dev) implementations (including `directories-rs`). Their authors seem to have figured out a practical API and behaviour.

I’m a bit surprised though that the `XDG_*` environment variables seem to be only honored on Linux. I figure they should work on any operating system (if defined by the user).

---

<div class="post-metadata">

### Author: ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)
#### Post date: [June 28, 2024, 11:14am UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/3 "2024-06-28T11:14:38Z")

</div>

I’d even suggest to add this to the standard library. It provides functionality that could probably be useful for most CLI applications.

---

<div class="post-metadata">

### Author: ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)
#### Post date: [June 30, 2024, 2:17pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/4 "2024-06-30T14:17:43Z")

</div>

> [@straight-shoota](#):
>
> I’m a bit surprised though that the `XDG_*` environment variables seem to be only honored on Linux

I find that odd too. It’s a freedesktop specification part of [X.org](https://www.x.org/), and not tied to Linux in any way. Maybe they just don’t care or forgot about UNIX in general and \*BSD in particular.

---

<div class="post-metadata">

### Author: ![rob](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rob/32/1409_2.png) [@rob](https://forum.crystal-lang.org/u/rob)
#### Post date: [July 1, 2024, 7:06pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/5 "2024-07-01T19:06:38Z")

</div>

XDG\_\* has always felt like it just arrived too late to the party and has struggled for adoption.

Regardless, I think this is a great idea and would love to see it in stdlib.

---

<div class="post-metadata">

### Author: ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)
#### Post date: [October 19, 2024, 5:23am UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/6 "2024-10-19T05:23:31Z")

</div>

> [@straight-shoota](#):
>
> I’d even suggest to add this to the standard library. It provides functionality that could probably be useful for most CLI applications.

That’s very good！ any progress?

---

<div class="post-metadata">

### Author: ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)
#### Post date: [October 21, 2024, 10:08am UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/7 "2024-10-21T10:08:08Z")

</div>

Nowhere. Someone interested can start writing down a RFC to propose a Crystal API and how it each method would map to the different underlying system (UNIX, Windows, macOS) and maybe be fancy: iOS, Android…

---

<div class="post-metadata">

### Author: ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)
#### Post date: [February 21, 2025, 4:15pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/8 "2025-02-21T16:15:07Z")

</div>

I use the XDG spec on linux and windows. Most rust apps follow the spec - even on windows. What I would recommend as the strategy is if it is set , use it. If it is not set have this library use the recommended directories for the operating system as the default directories

---

<div class="post-metadata">

### Author: ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)
#### Post date: [July 6, 2026, 12:43pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/9 "2026-07-06T12:43:45Z")

</div>

There are a few shards based on XDG base directories, but nothing cross platforms, so I created a basic [dirs shard](https://github.com/ysbaddaden/dirs.cr) loosely based on the documentation of the [directories crate](https://crates.io/crates/directories). The overall interface is based on XDG, but the directories depend on the OS:

- UNIX: XDG base/user directories, with support for ENV vars and `$XDG_CONFIG_HOME/user-dirs.dirs` for localizations.
- Windows: Known Folders API
- macOS: Standard Directories guidelines

```crystal
require "dirs"

Dirs.config_home
# => Path[~/.config] (UNIX)

app = Dirs::Project.new(myapp)
app.config("local.json")
# => Path[~/.config/myapp/local.json] (UNIX)

```

> **[GitHub - ysbaddaden/dirs.cr: Cross-platform base and user directories (UNIX,...](https://github.com/ysbaddaden/dirs.cr)**
>
> Cross-platform base and user directories (UNIX, macOS, Windows)

---

<div class="post-metadata">

### Author: ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)
#### Post date: [July 6, 2026, 12:45pm UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/10 "2026-07-06T12:45:30Z")

</div>

API is still in flux. After it settles down, we may consider to integrate it into `Dir` directly, or not (some directories aren’t portable).

---

<div class="post-metadata">

### Author: ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)
#### Post date: [July 7, 2026, 6:42am UTC](https://forum.crystal-lang.org/t/adding-a-base-directories-class-module-to-the-standard-library/6975/11 "2026-07-07T06:42:25Z")

</div>

I used windows at work and always defaulted to XDG base directories if they were set and then use os specific directories iff they were not set
