Hacker News new | ask | show | jobs
by okasaki 1178 days ago
A couple of years ago I wanted to use xdg dirs in a Python app I was writing. There are seemingly two packages for this, neither of which I could get to work.

Overall I don't think xdg dirs on Linux are really as amazing as some people think. For example it is said that one can just back up the config dir to carry all the configuration over to the next install, but apps often dump the default configuration into the config dir, which you don't want to carry to the next install (which will have different versions, so probably a different default config) because that might break stuff. In the end you have to carefully look over all the configuration to see what's worthwhile to take with you, and it doesn't really matter if that's in ~/.appname or ~/.config/appname

2 comments

> For example it is said that one can just back up the config dir to carry all the configuration over to the next install, but apps often dump the default configuration into the config dir, which you don't want to carry to the next install (which will have different versions, so probably a different default config) because that might break stuff. In the end you have to carefully look over all the configuration to see what's worthwhile to take with you, and it doesn't really matter if that's in ~/.appname or ~/.config/appname

Depends really what you want. If you want to have a fresh system with some of your config then sure, you need to decide what you want. If you just want to keep the same setup then ... I have been using the same $HOME for over 15 years (same for the rest of the system too). Some big upgrades (e.g. KDE3->4) required some manual intervention but most programs handle old config well enough in my experience.

> There are seemingly two packages for this, neither of which I could get to work.

I am really confused as to how that happened so I will show examples of all the three options I know of and how to use them to get a path of a config to load:

https://pypi.org/project/appdirs/ - simple, even works on windows

    from appdirs import user_config_dir
    import os
    # Company is optional for windows support to work the way windows users expect it.
    # You can also specify version to version the dirs.
    conf_dir = user_config_dir("program", "company")
    conf_path = os.path.join(conf_dir, "config.toml")
https://pypi.org/project/xdg-base-dirs/ - just bare bones XDG support with fall backs and pathlib support, no special windows support, but will still work for user configs

    from xdg_base_dirs import xdg_config_home
    conf_path = xdg_config_home() / "program" / "config.toml"
https://pypi.org/project/pyxdg/ - from freedesktop, includes more than just the base directory specification - a bit of a weird interface but technically can support more complex situations

    from xdg.BaseDirectory import load_first_config
    import os
    conf_dir = load_first_config("program")
    # conf_dir will be none if there is no "program" dir/file in any of the paths XDG_CONFIG_HOME or XDG_CONFIG_DIRS
    if conf_dir:
        conf_path = os.path.join(conf_dir, "config.toml")
Yes there are 3 options (but what modern language doesn't have this problem for everything?) and you have to pick one. It's not that hard to evaluate these, the only caveats you have to consider are: do I care about system-wide configurations and properly handling overlapping configurations, or do I just want a user local config directory. I have written the above code with consideration for the latter. If you are allergic to unnecessary dependencies (and I am so I can sympathize) then for the simple use case I outlined (with the same level of cross-platform support as xdg-base-dirs and pyxdg) you can just use this code:

    from pathlib import Path
    import os

    conf_home = os.environ.get('XDG_CONFIG_HOME')
    conf_home = Path(conf_home) if conf_home else Path.home() / ".config"
    conf_path = conf_home / "program" / "config.toml"
I hereby release all code (within this comment) under CC0 (I doubt you could even claim copyright on something so simple anyway, but let's avoid issues in advance).

> but apps often dump the default configuration into the config dir

This is not an XDG dir related problem, and not even that common of a problem. You are right that it IS a problem (and applications should be encouraged to avoid this, just as they should be encouraged to avoid littering the homedir) but strictly speaking, using .config while littering it with auto-generated configs is better than not using config while doing the same. It really does make life easier when configuration is generally kept separate from other types of data by following the XDG base directory specification.